diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 5746ba6..6438c69 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -324,6 +324,11 @@ starting directory successfully. ## Stage 8 — Preserve wildcard traversal before parent normalization +Completed: parent segments advance from inspected matched directories, including +physical link targets, while lexical candidates remain separate. Wildcard/parent, +permission failure before exclusion, missing branches and host/native link cases +pass with the existing glob policy and grammar checks. + Continue segment-by-segment traversal from stage 7's starting candidates. Update literal-segment handling and `explicitGlobCandidate` so a `..` after a wildcard advances from each inspected matched directory. Keep namespace diff --git a/internal/engine/glob.go b/internal/engine/glob.go index 2b4adea..b1520db 100644 --- a/internal/engine/glob.go +++ b/internal/engine/glob.go @@ -41,7 +41,20 @@ func selectGlobImportCandidates(access *filesystemAccess, basePath string, spec for _, parent := range candidates { if !patterned { childPath := access.join(parent.path, literal) - childAccessPath := access.join(parent.accessPath, literal) + parentAccessPath := parent.accessPath + if literal == ".." { + target, err := access.importEntryTarget(parent.accessPath, nil, true) + if err != nil { + return importSelection{}, []Diagnostic{globCandidateDiagnostic(err, site.location, "glob parent directory could not be inspected")} + } + if !target.dir { + continue + } + if target.identity != "" { + parentAccessPath = target.identity + } + } + childAccessPath := access.join(parentAccessPath, literal) candidate, missing, diagnostics := explicitGlobCandidate(access, basePath, childPath, childAccessPath, site, last) if len(diagnostics) != 0 { return importSelection{}, diagnostics diff --git a/internal/engine/glob_selection_test.go b/internal/engine/glob_selection_test.go index c32ca92..be64f8d 100644 --- a/internal/engine/glob_selection_test.go +++ b/internal/engine/glob_selection_test.go @@ -189,8 +189,9 @@ func selectGlobForTestWithDiagnostics(t *testing.T, access *filesystemAccess, ba } type observedGlobFilesystem struct { - files fstest.MapFS - opened []string + files fstest.MapFS + opened []string + failStat string } func (filesystem *observedGlobFilesystem) Open(name string) (fs.File, error) { @@ -199,5 +200,68 @@ func (filesystem *observedGlobFilesystem) Open(name string) (fs.File, error) { } func (filesystem *observedGlobFilesystem) Stat(name string) (fs.FileInfo, error) { + if name == filesystem.failStat { + return nil, fs.ErrPermission + } return fs.Stat(filesystem.files, name) } + +func TestSelectGlobInspectsWildcardBeforeParent(t *testing.T) { + t.Parallel() + filesystem := &observedGlobFilesystem{files: fstest.MapFS{ + "services/unit/marker": &fstest.MapFile{}, + "services/shared/one.yml": &fstest.MapFile{Data: []byte("value: one\n")}, + "services/irrelevant": &fstest.MapFile{}, + }} + access := newNativeAccess(filesystem, ".", &namespaceToken{}) + declaration := " - glob: services/*/../shared/*.yml\n required: true\n" + selection, diagnostics := selectGlobForTestWithDiagnostics(t, access, ".", declaration) + if len(diagnostics) != 0 || !equalStrings(selectedPaths(selection), []string{"services/shared/one.yml"}) { + t.Fatalf("selection=%#v, diagnostics=%#v", selection, diagnostics) + } + filesystem.failStat = "services/unit" + _, diagnostics = selectGlobForTestWithDiagnostics(t, access, ".", declaration+" exclude: '*.yml'\n") + if len(diagnostics) == 0 || diagnostics[0].Code != CodeImportPermission { + t.Fatalf("matched directory failure=%#v", diagnostics) + } + missing := selectGlobForTest(t, access, ".", " - glob: services/absent*/../shared/*.yml\n") + if len(missing.candidates) != 0 { + t.Fatalf("unmatched wildcard was cleaned away: %#v", missing) + } +} + +func TestSelectGlobParentUsesMatchedLinkTarget(t *testing.T) { + root := canonicalTestPath(t, t.TempDir()) + writeImportFile(t, root, "targets/unit/marker", "marker") + file := writeImportFile(t, root, "targets/shared/one.yml", "value: one\n") + if err := os.MkdirAll(filepath.Join(root, "services"), 0700); err != nil { + t.Fatal(err) + } + if err := os.Symlink("../targets/unit", filepath.Join(root, "services", "alias")); err != nil { + t.Skipf("symlinks unavailable: %v", err) + } + host, err := openHostAccess(root) + if err != nil { + t.Fatal(err) + } + defer host.close() + native := newNativeAccess(os.DirFS(root), ".", &namespaceToken{}) + for _, mode := range []struct { + name string + access *filesystemAccess + base, want string + }{ + {"host", host, root, file}, {"native", native, ".", "targets/shared/one.yml"}, + } { + t.Run(mode.name, func(t *testing.T) { + selection, diagnostics := selectGlobForTestWithDiagnostics(t, mode.access, mode.base, " - glob: services/*/../shared/*.yml\n required: true\n") + if len(diagnostics) != 0 || len(selection.candidates) != 1 || selection.candidates[0].accessPath != mode.want { + t.Fatalf("selection=%#v, diagnostics=%#v", selection, diagnostics) + } + selection = selectGlobForTest(t, mode.access, mode.base, " - glob: services/*/../shared/*.yml\n follow: false\n") + if len(selection.candidates) != 0 { + t.Fatalf("unfollowed link selected=%#v", selection) + } + }) + } +}