Parse import selector roots and share literal path resolution

This commit is contained in:
2026-12-05 20:41:30 +00:00
parent ae1578494a
commit 323728870c
6 changed files with 121 additions and 5 deletions

View File

@@ -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,

View File

@@ -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 {

View File

@@ -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

View File

@@ -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
}

View File

@@ -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)
}
}

View File

@@ -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
}