( ctx context.Context, dag *dagql.Server, path string, )
| 1485 | } |
| 1486 | |
| 1487 | func (src *ModuleSource) LoadContextFile( |
| 1488 | ctx context.Context, |
| 1489 | dag *dagql.Server, |
| 1490 | path string, |
| 1491 | ) (inst dagql.ObjectResult[*File], err error) { |
| 1492 | query, err := CurrentQuery(ctx) |
| 1493 | if err != nil { |
| 1494 | return inst, err |
| 1495 | } |
| 1496 | |
| 1497 | switch src.Kind { |
| 1498 | case ModuleSourceKindLocal: |
| 1499 | localSourceClientMetadata, err := query.NonModuleParentClientMetadata(ctx) |
| 1500 | if err != nil { |
| 1501 | return inst, fmt.Errorf("failed to get client metadata: %w", err) |
| 1502 | } |
| 1503 | localSourceCtx := engine.ContextWithClientMetadata(ctx, localSourceClientMetadata) |
| 1504 | |
| 1505 | // Retrieve the absolute path to the context directory (.git or dagger.json) |
| 1506 | // and the module root directory (dagger.json) |
| 1507 | ctxPath := src.Local.ContextDirectoryPath |
| 1508 | modPath := filepath.Join(ctxPath, src.SourceRootSubpath) |
| 1509 | |
| 1510 | // If path is not absolute, it's relative to the module root directory. |
| 1511 | // If path is absolute, it's relative to the context directory. |
| 1512 | if !filepath.IsAbs(path) { |
| 1513 | path = filepath.Join(modPath, path) |
| 1514 | } else { |
| 1515 | path = filepath.Join(ctxPath, path) |
| 1516 | } |
| 1517 | |
| 1518 | // We just check if the path is relative to the context directory, |
| 1519 | // if not, that means it's a path that target an outside directory |
| 1520 | // which is not allowed. |
| 1521 | relativePathToCtx, err := filepath.Rel(ctxPath, path) |
| 1522 | if err != nil { |
| 1523 | return inst, fmt.Errorf("failed to get relative path to context: %w", err) |
| 1524 | } |
| 1525 | |
| 1526 | // If the relative path is outisde of the context directory, throw an error. |
| 1527 | if strings.HasPrefix(relativePathToCtx, "..") { |
| 1528 | return inst, fmt.Errorf("path %q is outside of context directory %q, path should be relative to the context directory", path, ctxPath) |
| 1529 | } |
| 1530 | |
| 1531 | err = dag.Select(localSourceCtx, dag.Root(), &inst, |
| 1532 | dagql.Selector{ |
| 1533 | Field: "host", |
| 1534 | }, |
| 1535 | dagql.Selector{ |
| 1536 | Field: "file", |
| 1537 | Args: []dagql.NamedInput{ |
| 1538 | {Name: "path", Value: dagql.String(path)}, |
| 1539 | {Name: "noCache", Value: dagql.Boolean(true)}, |
| 1540 | }, |
| 1541 | }, |
| 1542 | ) |
| 1543 | if err != nil { |
| 1544 | return inst, fmt.Errorf("failed to select file: %w", err) |
no test coverage detected