Establish glob starting locations before wildcard discovery

This commit is contained in:
2026-12-05 20:46:47 +00:00
parent 9c02607c26
commit 30f04dd5e1
3 changed files with 140 additions and 3 deletions

View File

@@ -292,6 +292,11 @@ end to end with existing diagnostics, version processing and accounting.
## Stage 7 — Establish glob roots and literal starting locations
Completed: globs inspect their literal prefix before wildcard discovery, admitting
resolution ancestors of an explicit root while checking the actual starting
location. Absolute host/native, fully literal, missing-prefix and outside-root
cases pass alongside existing glob selection tests.
Implement rooted glob initialization in `glob.go` using stage 5's root parsing
and literal resolution. Establish the longest literal starting directory before
the first wildcard, decoding literal escapes through the existing glob helpers.

View File

@@ -4,6 +4,7 @@ import (
"errors"
"io/fs"
"path"
"path/filepath"
"slices"
"sort"
"strings"
@@ -26,9 +27,12 @@ func selectGlobImportCandidates(access *filesystemAccess, basePath string, spec
if accounting == nil {
accounting = &importAccounting{}
}
rootPath := normalizedImportCandidatePath(access, basePath)
candidates := []selectedImportCandidate{{path: rootPath, accessPath: basePath, site: site}}
for index, segment := range segments {
candidates, prefixLength, diagnostics := globStartingCandidates(access, basePath, spec.glob, segments, site)
if len(diagnostics) != 0 {
return importSelection{}, diagnostics
}
for index := prefixLength; index < len(segments); index++ {
segment := segments[index]
last := index == len(segments)-1
literal, patterned := globLiteralSegment(segment)
// nextByPath is the set union of candidates reached from each matching
@@ -294,3 +298,71 @@ func globFilesystemDiagnostic(err error, message string, location Location) Diag
}
return importDiagnostic(code, message, &location)
}
// globStartingCandidates inspects the literal prefix before wildcard discovery.
// Ancestors of an explicit root are resolution prefixes, never discovery roots.
func globStartingCandidates(access *filesystemAccess, base, pattern string, segments []string, site importFieldSite) ([]selectedImportCandidate, int, []Diagnostic) {
root, _ := splitImportSelectorRoot(pattern)
start := base
if root != "" {
start = root
if access.native != nil {
if root != "/" {
return nil, 0, []Diagnostic{*importSelectionDiagnostic(site, CodeImportOutsideRoot, "glob root is outside the selected namespace")}
}
start = "."
}
}
prefixAccess := *access
if access.native != nil {
prefixAccess.rootPath = "."
} else {
prefixAccess.rootPath, prefixAccess.root = "", nil
}
candidate := selectedImportCandidate{path: normalizedImportCandidatePath(access, start), accessPath: start, site: site}
consumed := 0
for index, segment := range segments {
literal, patterned := globLiteralSegment(segment)
if patterned {
break
}
lexical, lexicalErr := resolveLiteralImportSelector(&prefixAccess, candidate.path, literal)
physical, physicalErr := resolveLiteralImportSelector(&prefixAccess, candidate.accessPath, literal)
if lexicalErr != nil || physicalErr != nil {
return nil, 0, []Diagnostic{*importSelectionDiagnostic(site, CodeImportOutsideRoot, "glob prefix is outside the selected namespace")}
}
next, missing, diagnostics := explicitGlobCandidate(&prefixAccess, base, lexical, physical, site, index == len(segments)-1)
if len(diagnostics) != 0 {
return nil, 0, diagnostics
}
consumed++
if missing {
return nil, consumed, nil
}
if !globResolutionPrefixAllowed(access, next.accessPath) {
return nil, 0, []Diagnostic{*importSelectionDiagnostic(site, CodeImportOutsideRoot, "glob prefix is outside the configuration root")}
}
candidate = next
}
if !access.contains(candidate.accessPath) || !access.contains(candidate.path) {
return nil, 0, []Diagnostic{*importSelectionDiagnostic(site, CodeImportOutsideRoot, "glob discovery starts outside the configuration root")}
}
// Native links preserve their namespace spelling; inspect with the actual
// boundary once the literal prefix has entered the selected root.
if access.native != nil {
if _, err := access.importEntryTarget(candidate.accessPath, nil, true); err != nil {
return nil, 0, []Diagnostic{globCandidateDiagnostic(err, site.location, "glob starting location could not be inspected")}
}
}
return []selectedImportCandidate{candidate}, consumed, nil
}
func globResolutionPrefixAllowed(access *filesystemAccess, name string) bool {
if access.contains(name) {
return true
}
if access.native != nil {
return containsNativePath(name, access.rootPath)
}
return access.rootPath != "" && containedPath(filepath.FromSlash(name), access.rootPath)
}

View File

@@ -0,0 +1,60 @@
package engine
import (
"path/filepath"
"strconv"
"testing"
"testing/fstest"
)
func TestAbsoluteGlobLiteralStartingLocations(t *testing.T) {
t.Parallel()
hostRoot := canonicalTestPath(t, t.TempDir())
file := writeImportFile(t, hostRoot, "workspace/allowed/fragments/one.yml", "value: one\n")
boundary := filepath.Join(hostRoot, "workspace", "allowed")
host, err := openHostAccess(boundary)
if err != nil {
t.Fatal(err)
}
defer host.close()
native := newNativeAccess(fstest.MapFS{"workspace/allowed/fragments/one.yml": &fstest.MapFile{Data: []byte("value: one\n")}}, "workspace/allowed", &namespaceToken{})
for _, mode := range []struct {
name string
access *filesystemAccess
base, root, want string
}{
{"host", host, boundary, filepath.ToSlash(boundary), filepath.ToSlash(file)},
{"native", native, "workspace/allowed", "/workspace/allowed", "workspace/allowed/fragments/one.yml"},
} {
t.Run(mode.name, func(t *testing.T) {
for _, pattern := range []string{mode.root + "/fragments/*.yml", mode.root + "/fragments/one.yml"} {
selection, diagnostics := selectGlobForTestWithDiagnostics(t, mode.access, mode.base, " - glob: "+strconv.Quote(pattern)+"\n required: true\n")
if len(diagnostics) != 0 || len(selection.candidates) != 1 || selection.candidates[0].path != mode.want {
t.Fatalf("pattern %q: selection=%#v, diagnostics=%#v", pattern, selection, diagnostics)
}
}
for _, entry := range []struct {
pattern string
required bool
code Code
}{
{mode.root + "/missing/*.yml", false, ""},
{mode.root + "/missing/*.yml", true, CodeImportMissing},
{mode.root + "/missing/../fragments/*.yml", true, CodeImportMissing},
{mode.root + "/../../*/*.yml", true, CodeImportOutsideRoot},
} {
declaration := " - glob: " + strconv.Quote(entry.pattern) + "\n required: " + strconv.FormatBool(entry.required) + "\n"
selection, diagnostics := selectGlobForTestWithDiagnostics(t, mode.access, mode.base, declaration)
if entry.code == "" {
if len(diagnostics) != 0 || len(selection.candidates) != 0 {
t.Fatalf("optional missing=%#v, %#v", selection, diagnostics)
}
continue
}
if len(diagnostics) == 0 || diagnostics[0].Code != entry.code {
t.Fatalf("pattern %q diagnostics=%#v", entry.pattern, diagnostics)
}
}
})
}
}