Represent import boundaries independently of context anchors

This commit is contained in:
2026-12-05 20:37:05 +00:00
parent f75553a7fb
commit 1003a3749a
5 changed files with 112 additions and 23 deletions

View File

@@ -166,6 +166,10 @@ permission switch, sandbox promise, execution sequence or new approval gate.
## Stage 2 — Represent and validate independent context facts
Completed: resolved contexts now retain a separate import boundary; host
BaseDir-only contexts are accepted without changing native requirements or
selected-file base ownership. Focused context, filesystem and discovery tests pass.
Read `filesystem_context.go`, `filesystem.go`, `options.go`, `load_input.go` and
the context tables above. Add the private import-boundary state while retaining
the legacy anchor/base meanings. Update host selected, synthetic, absent and

View File

@@ -12,7 +12,7 @@ func validateFilesystemContext(source Source, context *FilesystemContext, main p
if diagnostic != nil {
// Keep absent-input validation's existing diagnostic while sharing
// root/base derivation with overlay and resource-context handling.
if source.kind == sourceNone && context.FS == nil && diagnostic.Code == CodeImportOutsideRoot {
if source.kind == sourceNone && context != nil && context.FS == nil && diagnostic.Code == CodeImportOutsideRoot {
diagnostic.Message = "import base must stay inside the configuration root"
}
return resolvedFilesystemContext{}, diagnostic
@@ -27,7 +27,7 @@ func validateResolvedFilesystemContext(resolved resolvedFilesystemContext) *Diag
if diagnostic := validateResolvedFilesystemDirectories(resolved); diagnostic != nil {
return diagnostic
}
if resolved.native != nil {
if resolved.native != nil || resolved.rootPath == "" {
return nil
}
root, err := os.OpenRoot(resolved.rootPath)
@@ -51,8 +51,8 @@ func filesystemOptionDiagnostics(source Source, context *FilesystemContext) []Di
if context.FS != nil {
return absentFilesystemOptionDiagnostics(context)
}
// A selected host file can derive its root; absence requires an
// explicit root. Check that compatibility only after selection.
// A selected host file derives its base; absence can use an explicit
// base or root. Check compatibility after selection.
if context.BaseDir != "" && !filepath.IsAbs(context.BaseDir) {
return []Diagnostic{optionDiagnostic(CodeOptionsInvalid, "filesystem base directory must be an absolute host path")}
}
@@ -74,14 +74,14 @@ func filesystemOptionDiagnostics(source Source, context *FilesystemContext) []Di
return []Diagnostic{optionDiagnostic(CodeOptionsInvalid, "filesystem root must be a slash-relative valid path")}
}
case sourceBytes, sourceReader, sourceMapping:
if context.BaseDir == "" || context.Root == "" {
if context.BaseDir == "" || context.FS != nil && context.Root == "" {
return []Diagnostic{optionDiagnostic(CodeOptionsInvalid, "byte and reader filesystem context requires base and root directories")}
}
if context.FS != nil {
if context.FS.namespace == nil || !fs.ValidPath(context.BaseDir) || !fs.ValidPath(context.Root) {
return []Diagnostic{optionDiagnostic(CodeOptionsInvalid, "filesystem base and root must be slash-relative valid paths")}
}
} else if !filepath.IsAbs(context.BaseDir) || !filepath.IsAbs(context.Root) {
} else if !filepath.IsAbs(context.BaseDir) || context.Root != "" && !filepath.IsAbs(context.Root) {
return []Diagnostic{optionDiagnostic(CodeOptionsInvalid, "byte and reader filesystem base and root directories must be absolute")}
}
}
@@ -93,7 +93,7 @@ func absentFilesystemOptionDiagnostics(context *FilesystemContext) []Diagnostic
if context.FS.namespace == nil || nilFilesystem(context.FS.namespace.filesystem) || context.BaseDir == "" || context.Root == "" || !fs.ValidPath(context.BaseDir) || !fs.ValidPath(context.Root) {
return []Diagnostic{optionDiagnostic(CodeOptionsInvalid, "native filesystem context requires a namespace, base, and root directory")}
}
} else if context.Root == "" || !filepath.IsAbs(context.Root) || context.BaseDir != "" && !filepath.IsAbs(context.BaseDir) {
} else if context.Root == "" && context.BaseDir == "" || context.Root != "" && !filepath.IsAbs(context.Root) || context.BaseDir != "" && !filepath.IsAbs(context.BaseDir) {
return []Diagnostic{optionDiagnostic(CodeOptionsInvalid, "absent-input filesystem root and base directories must be absolute host paths")}
}
return nil

View File

@@ -9,11 +9,12 @@ import (
)
type resolvedFilesystemContext struct {
native *nativeNamespace
hostToken *namespaceToken
rootPath string
basePath string
mainPath string
native *nativeNamespace
hostToken *namespaceToken
rootPath string
importRoot string
basePath string
mainPath string
}
func resolveFilesystemContext(source Source, context *FilesystemContext, main parsedSource, hostToken *namespaceToken) (resolvedFilesystemContext, *Diagnostic) {
@@ -57,24 +58,35 @@ func resolveAbsentFilesystemContext(context *FilesystemContext) (resolvedFilesys
if context.FS != nil {
return resolveNativeFilesystemContext(Source{kind: sourceNone}, context)
}
rootPath, err := canonicalExistingPath(context.Root)
if context.Root == "" && context.BaseDir == "" {
value := optionDiagnostic(CodeOptionsInvalid, "absent-input filesystem context requires a base or root directory")
return resolvedFilesystemContext{}, &value
}
anchor := context.Root
if anchor == "" {
anchor = context.BaseDir
}
rootPath, err := canonicalExistingPath(anchor)
if err != nil {
value := importDiagnostic(CodeImportInvalid, "configuration root could not be canonicalized", nil)
return resolvedFilesystemContext{}, &value
}
basePath := rootPath
resolved := resolvedFilesystemContext{rootPath: rootPath, basePath: rootPath}
if context.Root != "" {
resolved.importRoot = rootPath
}
if context.BaseDir != "" {
basePath, err = canonicalExistingPath(context.BaseDir)
resolved.basePath, err = canonicalExistingPath(context.BaseDir)
if err != nil {
value := importDiagnostic(CodeImportInvalid, "import base directory could not be canonicalized", nil)
return resolvedFilesystemContext{}, &value
}
}
if !containedPath(rootPath, basePath) {
if resolved.importRoot != "" && !containedPath(resolved.importRoot, resolved.basePath) {
value := importDiagnostic(CodeImportOutsideRoot, "selected source and import base must stay inside the configuration root", nil)
return resolvedFilesystemContext{}, &value
}
return resolvedFilesystemContext{rootPath: rootPath, basePath: basePath}, nil
return resolved, nil
}
func resolveNativeFilesystemContext(source Source, context *FilesystemContext) (resolvedFilesystemContext, *Diagnostic) {
@@ -96,6 +108,7 @@ func resolveNativeFilesystemContext(source Source, context *FilesystemContext) (
return resolvedFilesystemContext{}, &value
}
resolved.native = namespace
resolved.importRoot = resolved.rootPath
if !containsNativePath(resolved.rootPath, resolved.basePath) || resolved.mainPath != "" && !containsNativePath(resolved.rootPath, resolved.mainPath) {
value := importDiagnostic(CodeImportOutsideRoot, "selected source and import base must stay inside the configuration root", nil)
return resolvedFilesystemContext{}, &value
@@ -133,13 +146,14 @@ func resolveHostFilesystemContext(source Source, context *FilesystemContext, mai
return resolvedFilesystemContext{}, &value
}
resolved.rootPath = rootPath
resolved.importRoot = rootPath
}
} else {
if context == nil || context.BaseDir == "" || context.Root == "" {
value := optionDiagnostic(CodeOptionsInvalid, "byte and reader imports require absolute base and root directories")
if context == nil || context.BaseDir == "" {
value := optionDiagnostic(CodeOptionsInvalid, "synthetic imports require an absolute base directory")
return resolvedFilesystemContext{}, &value
}
if !filepath.IsAbs(context.BaseDir) || !filepath.IsAbs(context.Root) {
if !filepath.IsAbs(context.BaseDir) || context.Root != "" && !filepath.IsAbs(context.Root) {
value := optionDiagnostic(CodeOptionsInvalid, "byte and reader import base and root directories must be absolute")
return resolvedFilesystemContext{}, &value
}
@@ -149,7 +163,11 @@ func resolveHostFilesystemContext(source Source, context *FilesystemContext, mai
value := importDiagnostic(CodeImportInvalid, "import base directory could not be canonicalized", nil)
return resolvedFilesystemContext{}, &value
}
resolved.rootPath, err = canonicalExistingPath(context.Root)
resolved.rootPath = resolved.basePath
if context.Root != "" {
resolved.rootPath, err = canonicalExistingPath(context.Root)
resolved.importRoot = resolved.rootPath
}
if err != nil {
value := importDiagnostic(CodeImportInvalid, "configuration root could not be canonicalized", nil)
return resolvedFilesystemContext{}, &value
@@ -171,6 +189,9 @@ func containsNativePath(root, name string) bool {
func validateResolvedFilesystemDirectories(resolved resolvedFilesystemContext) *Diagnostic {
for _, directory := range []string{resolved.rootPath, resolved.basePath} {
if directory == "" {
continue
}
var info fs.FileInfo
var err error
if resolved.native != nil {

View File

@@ -0,0 +1,64 @@
package engine
import (
"path/filepath"
"testing"
)
func TestHostFilesystemContextFacts(t *testing.T) {
t.Parallel()
base := canonicalTestPath(t, t.TempDir())
other := canonicalTestPath(t, t.TempDir())
mainPath := writeImportFile(t, base, "main.yml", "value: main\n")
for _, entry := range []struct {
name string
source Source
context *FilesystemContext
main parsedSource
root, lookup, bound string
code Code
}{
{name: "selected default", source: File(mainPath), main: parsedSource{source: &SourceInfo{Path: mainPath}}, root: base, lookup: base},
{name: "selected bounded", source: File(mainPath), context: &FilesystemContext{Root: base}, main: parsedSource{source: &SourceInfo{Path: mainPath}}, root: base, lookup: base, bound: base},
{name: "synthetic base", source: Bytes("main", nil), context: &FilesystemContext{BaseDir: base}, main: parsedSource{source: &SourceInfo{}}, root: base, lookup: base},
{name: "absent base", source: NoSource(), context: &FilesystemContext{BaseDir: base}, root: base, lookup: base},
{name: "absent root", source: NoSource(), context: &FilesystemContext{Root: base}, root: base, lookup: base, bound: base},
{name: "outside base", source: NoSource(), context: &FilesystemContext{BaseDir: other, Root: base}, code: CodeImportOutsideRoot},
} {
t.Run(entry.name, func(t *testing.T) {
if entry.context != nil {
if diagnostics := filesystemOptionDiagnostics(entry.source, entry.context); len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
}
got, diagnostic := validateFilesystemContext(entry.source, entry.context, entry.main, nil)
if entry.code != "" {
if diagnostic == nil || diagnostic.Code != entry.code {
t.Fatalf("diagnostic = %v", diagnostic)
}
return
}
if diagnostic != nil {
t.Fatal(diagnostic)
}
if got.rootPath != entry.root || got.basePath != entry.lookup || got.importRoot != entry.bound {
t.Fatalf("context = %#v", got)
}
})
}
}
func TestSearchBaseOnlyContextDependsOnSelection(t *testing.T) {
t.Parallel()
base := t.TempDir()
main := writeImportFile(t, base, "main.yml", "value: main\n")
options := Options[discoveryLoadConfig]{Filesystem: &FilesystemContext{BaseDir: base}}
if _, err := testLoad(SearchConfig("", Paths(filepath.Join(base, "missing.yml"))), options); err != nil {
t.Fatal(err)
}
// Once a file is selected, its directory owns the base. Selection is terminal.
if loaded, err := testLoad(SearchConfig("", Paths(main)), options); err == nil || loaded != nil {
t.Fatalf("selected = %v, %v", loaded, err)
}
}

View File

@@ -253,7 +253,7 @@ func TestFilesystemContextIsValidatedWithoutImportsOrActiveOverlay(t *testing.T)
if err != nil || result.Config.Items["root"] != "memory" {
t.Fatalf("byte filesystem context = %#v, %v", result, err)
}
requireLoadCode(t, Bytes("memory.yml", []byte("{}\n")), Options[importTestConfig]{Filesystem: &FilesystemContext{BaseDir: directory}}, CodeOptionsInvalid)
requireLoadCode(t, Bytes("memory.yml", []byte("{}\n")), Options[importTestConfig]{Filesystem: &FilesystemContext{Root: directory}}, CodeOptionsInvalid)
defaultsOnly, err := testLoad[importTestConfig](NoSource(), Options[importTestConfig]{Overlay: "default"})
if err != nil || defaultsOnly == nil {
t.Fatalf("default overlay with NoSource = %#v, %v", defaultsOnly, err)
@@ -301,7 +301,7 @@ func TestNoSourceImportsAndFilesystemContext(t *testing.T) {
context FilesystemContext
code Code
}{
{name: "missing root", context: FilesystemContext{BaseDir: directory}, code: CodeOptionsInvalid},
{name: "empty context", context: FilesystemContext{}, code: CodeOptionsInvalid},
{name: "relative root", context: FilesystemContext{Root: "."}, code: CodeOptionsInvalid},
{name: "missing root directory", context: FilesystemContext{Root: filepath.Join(directory, "missing")}, code: CodeImportInvalid},
{name: "base outside root", context: FilesystemContext{BaseDir: outside, Root: directory}, code: CodeImportOutsideRoot},