From 323728870c21b76b0c33e8bd83ccc1fa57755a88 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sat, 5 Dec 2026 20:41:30 +0000 Subject: [PATCH] Parse import selector roots and share literal path resolution --- docs/roadmap/implementation.md | 6 +- internal/engine/directives.go | 5 +- internal/engine/glob.go | 3 +- internal/engine/import_selection.go | 33 +++++++++ internal/engine/import_selector_path_test.go | 77 ++++++++++++++++++++ internal/engine/import_spec.go | 2 + 6 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 internal/engine/import_selector_path_test.go diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index ef69c10..919dfaf 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -1,6 +1,6 @@ # Implementation plan: absolute and parent-relative imports -Status: ready for implementation. +Status: implementation in progress. Planning baseline: `8c04dfd`; runtime baseline: `a441347`. ## Scope and execution @@ -240,6 +240,10 @@ second link-resolution framework or host fallback from a native namespace. ## Stage 5 — Parse selector roots and share literal path resolution +Completed: selector validation accepts absolute spellings while retaining scheme +and glob grammar checks; glob declarations retain root information separately. +A shared literal resolver passes host/native coordinate and boundary tests. + Read `directives.go`, `import_spec.go`, `import_selection.go` and `glob.go`. Separate path spelling validation from selected-filesystem interpretation. Accept new absolute spellings while preserving existing nonempty/string-set, diff --git a/internal/engine/directives.go b/internal/engine/directives.go index ef5d7ad..289e20e 100644 --- a/internal/engine/directives.go +++ b/internal/engine/directives.go @@ -2,7 +2,6 @@ package engine import ( "os" - "path" "path/filepath" "strings" @@ -225,10 +224,10 @@ func fragmentVersionClaimWithIndex(root *document.Node, rule *VersionRule, sourc } func validImportPath(value string) bool { - if value == "" || strings.TrimSpace(value) != value || path.IsAbs(value) || strings.HasPrefix(value, "//") { + if value == "" || strings.TrimSpace(value) != value { return false } - return !hasImportScheme(value) + return filepath.IsAbs(value) || !hasImportScheme(value) } func hasImportScheme(value string) bool { diff --git a/internal/engine/glob.go b/internal/engine/glob.go index e7f3002..91b4d6d 100644 --- a/internal/engine/glob.go +++ b/internal/engine/glob.go @@ -237,7 +237,8 @@ func validGlobPattern(pattern string) ([]string, bool) { if !validImportPath(pattern) { return nil, false } - segments := strings.Split(pattern, "/") + _, remainder := splitImportSelectorRoot(pattern) + segments := strings.Split(remainder, "/") for _, segment := range segments { if segment == "" || hasRecursiveStars(segment) { return nil, false diff --git a/internal/engine/import_selection.go b/internal/engine/import_selection.go index 3acc2a4..6e6f5c5 100644 --- a/internal/engine/import_selection.go +++ b/internal/engine/import_selection.go @@ -403,3 +403,36 @@ func importSelectionDiagnostic(site importFieldSite, code Code, message string) Message: message, Primary: &location, } } + +// splitImportSelectorRoot separates a path root without cleaning wildcard segments. +func splitImportSelectorRoot(selector string) (string, string) { + if filepath.IsAbs(selector) { + root := selector[:len(filepath.VolumeName(selector))+1] + return root, selector[len(root):] + } + if strings.HasPrefix(selector, "/") { + return "/", strings.TrimPrefix(selector, "/") + } + return "", selector +} + +// resolveLiteralImportSelector interprets YAML paths in the selected namespace. +// Link resolution and source admission remain with the existing target owners. +func resolveLiteralImportSelector(access *filesystemAccess, base, selector string) (string, error) { + var name string + if access.native != nil { + if strings.HasPrefix(selector, "/") { + name = path.Clean(strings.TrimPrefix(selector, "/")) + } else { + name = path.Join(base, selector) + } + } else if filepath.IsAbs(selector) { + name = filepath.Clean(selector) + } else { + name = access.join(base, selector) + } + if !access.contains(name) { + return "", errImportPathOutsideRoot + } + return name, nil +} diff --git a/internal/engine/import_selector_path_test.go b/internal/engine/import_selector_path_test.go new file mode 100644 index 0000000..1a915f2 --- /dev/null +++ b/internal/engine/import_selector_path_test.go @@ -0,0 +1,77 @@ +package engine + +import ( + "errors" + "path/filepath" + "testing" + "testing/fstest" +) + +func TestLiteralImportSelectorCoordinates(t *testing.T) { + t.Parallel() + anchor := canonicalTestPath(t, t.TempDir()) + external := canonicalTestPath(t, t.TempDir()) + host, err := openHostAccess("") + if err != nil { + t.Fatal(err) + } + defer host.close() + bounded, err := openHostAccess(anchor) + if err != nil { + t.Fatal(err) + } + defer bounded.close() + native := newNativeAccess(fstest.MapFS{}, ".", &namespaceToken{}) + nativeBounded := newNativeAccess(fstest.MapFS{}, "allowed", &namespaceToken{}) + for _, entry := range []struct { + name string + access *filesystemAccess + base, selector, want string + outside bool + }{ + {name: "host absolute", access: host, base: anchor, selector: filepath.Join(external, "part.yml"), want: filepath.Join(external, "part.yml")}, + {name: "host parent", access: host, base: filepath.Join(anchor, "nested"), selector: "../part.yml", want: filepath.Join(anchor, "part.yml")}, + {name: "host bounded", access: bounded, base: anchor, selector: filepath.Join(external, "part.yml"), outside: true}, + {name: "host literal", access: host, base: anchor, selector: "literal*.yml", want: filepath.Join(anchor, "literal*.yml")}, + {name: "native root", access: native, base: "app", selector: "/shared/part.yml", want: "shared/part.yml"}, + {name: "native parent", access: native, base: "app", selector: "../shared/part.yml", want: "shared/part.yml"}, + {name: "native escape", access: native, base: "app", selector: "../../part.yml", outside: true}, + {name: "native bounded root", access: nativeBounded, base: "allowed/app", selector: "/allowed/part.yml", want: "allowed/part.yml"}, + {name: "native bounded escape", access: nativeBounded, base: "allowed/app", selector: "/shared/part.yml", outside: true}, + } { + t.Run(entry.name, func(t *testing.T) { + got, err := resolveLiteralImportSelector(entry.access, entry.base, entry.selector) + if entry.outside { + if !errors.Is(err, errImportPathOutsideRoot) { + t.Fatalf("outside error = %v", err) + } + return + } + if err != nil || got != entry.want { + t.Fatalf("resolved = %q, %v; want %q", got, err, entry.want) + } + }) + } +} + +func TestImportSelectorRootGrammar(t *testing.T) { + t.Parallel() + for _, pattern := range []string{"/shared/*.yml", "../shared/*.yml", "/shared/literal\\*.yml"} { + if _, ok := validGlobPattern(pattern); !ok { + t.Errorf("rejected %q", pattern) + } + } + for _, pattern := range []string{"", "https://example.test/*.yml", "/shared/**/*.yml", "/shared//*.yml", "/shared/[broken"} { + if _, ok := validGlobPattern(pattern); ok { + t.Errorf("accepted %q", pattern) + } + } + root, rest := splitImportSelectorRoot("/shared/*/../part.yml") + if root != "/" || rest != "shared/*/../part.yml" { + t.Fatalf("root=%q, rest=%q", root, rest) + } + literal, patterned := globLiteralSegment("literal\\*.yml") + if patterned || literal != "literal*.yml" { + t.Fatalf("escaped literal=%q, patterned=%v", literal, patterned) + } +} diff --git a/internal/engine/import_spec.go b/internal/engine/import_spec.go index 1855510..df659e2 100644 --- a/internal/engine/import_spec.go +++ b/internal/engine/import_spec.go @@ -28,6 +28,7 @@ type importDeclarationSpec struct { selectorSites []importFieldSite glob string globSegments []string + globRoot string required *bool recursive *bool follow *bool @@ -327,6 +328,7 @@ func parseImportSelectorValue(spec *importDeclarationSpec, field string, node *d return &diagnostic } spec.glob, spec.globSegments = node.Text, segments + spec.globRoot, _ = splitImportSelectorRoot(node.Text) } return nil }