( ctx context.Context, dag *dagql.Server, path string, filterInputs []dagql.NamedInput, )
| 1313 | } |
| 1314 | |
| 1315 | func (src *ModuleSource) loadContextFromSource( |
| 1316 | ctx context.Context, |
| 1317 | dag *dagql.Server, |
| 1318 | path string, |
| 1319 | filterInputs []dagql.NamedInput, |
| 1320 | ) (inst dagql.ObjectResult[*Directory], err error) { |
| 1321 | query, err := CurrentQuery(ctx) |
| 1322 | if err != nil { |
| 1323 | return inst, err |
| 1324 | } |
| 1325 | switch src.Kind { |
| 1326 | case ModuleSourceKindLocal: |
| 1327 | localSourceClientMetadata, err := query.NonModuleParentClientMetadata(ctx) |
| 1328 | if err != nil { |
| 1329 | return inst, fmt.Errorf("failed to get client metadata: %w", err) |
| 1330 | } |
| 1331 | localSourceCtx := engine.ContextWithClientMetadata(ctx, localSourceClientMetadata) |
| 1332 | |
| 1333 | // Retrieve the absolute path to the context directory (.git or dagger.json) |
| 1334 | // and the module root directory (dagger.json) |
| 1335 | ctxPath := src.Local.ContextDirectoryPath |
| 1336 | modPath := filepath.Join(ctxPath, src.SourceRootSubpath) |
| 1337 | |
| 1338 | // If path is not absolute, it's relative to the module root directory. |
| 1339 | // If path is absolute, it's relative to the context directory. |
| 1340 | if !filepath.IsAbs(path) { |
| 1341 | path = filepath.Join(modPath, path) |
| 1342 | } else { |
| 1343 | path = filepath.Join(ctxPath, path) |
| 1344 | } |
| 1345 | |
| 1346 | // We just check if the path is relative to the context directory, |
| 1347 | // if not, that means it's a path that target an outside directory |
| 1348 | // which is not allowed. |
| 1349 | relativePathToCtx, err := filepath.Rel(ctxPath, path) |
| 1350 | if err != nil { |
| 1351 | return inst, fmt.Errorf("failed to get relative path to context: %w", err) |
| 1352 | } |
| 1353 | |
| 1354 | // If the relative path is outisde of the context directory, throw an error. |
| 1355 | if strings.HasPrefix(relativePathToCtx, "..") { |
| 1356 | return inst, fmt.Errorf("path %q is outside of context directory %q, path should be relative to the context directory", path, ctxPath) |
| 1357 | } |
| 1358 | |
| 1359 | err = dag.Select(localSourceCtx, dag.Root(), &inst, |
| 1360 | dagql.Selector{ |
| 1361 | Field: "host", |
| 1362 | }, |
| 1363 | dagql.Selector{ |
| 1364 | Field: "directory", |
| 1365 | Args: append([]dagql.NamedInput{ |
| 1366 | {Name: "path", Value: dagql.String(path)}, |
| 1367 | {Name: "noCache", Value: dagql.Boolean(true)}, |
| 1368 | }, filterInputs...), |
| 1369 | }, |
| 1370 | ) |
| 1371 | if err != nil { |
| 1372 | return inst, fmt.Errorf("failed to select host directory: %w", err) |
no test coverage detected