From 8b18d09a4f2ad57b3a6e8ab91ca13490888ebd54 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 21 Nov 2026 03:45:35 +0000 Subject: [PATCH 1/3] fix(scanner): skip files that vanish mid-walk instead of failing (#302) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SessionStart:resume could die with "Error walking tree: lstat …: no such file or directory" when a short-lived entry (e.g. watch.transition) was removed between ReadDir and Lstat. Treat that ENOENT as skip; keep a missing walk root fatal. .codemap/ was already in IgnoredDirs; note why. Co-authored-by: Jordan Coin Jackson --- scanner/walker.go | 19 +++++++++++++++++-- scanner/walker_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ topology/provider.go | 9 +++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/scanner/walker.go b/scanner/walker.go index 945ca2e7..8f7876d2 100644 --- a/scanner/walker.go +++ b/scanner/walker.go @@ -257,8 +257,9 @@ var IgnoredDirs = map[string]bool{ "node_modules": true, "vendor": true, // codemap's and docmap's own state and caches are never project source - // (#196, #184). Hiding them only through a gitignore rule left them in - // the tree of any repo whose rule was never written. + // (#196, #184, #302). Hiding them only through a gitignore rule left them + // in the tree of any repo whose rule was never written; skipping the dir + // also avoids races on short-lived files like watch.transition. ".codemap": true, ".docmap": true, // Go's own toolchain ignores testdata, and fixture repositories inside it @@ -493,6 +494,11 @@ func ScanFiles(ctx context.Context, root string, cache *GitIgnoreCache, only []s return files, err } +// walkDuringTest, when set, runs at the start of each ScanFilesBounded walk +// callback. Tests use it to delete a later sibling and prove a vanished entry +// is skipped rather than fatal (#302). +var walkDuringTest func(path string) + // ScanFilesBounded walks like ScanFiles but keeps at most the configured // number of files (limits.ScanFileBound) and reports how many matched in // total, so a caller can say "scanned N of M" instead of presenting the @@ -513,7 +519,16 @@ func ScanFilesBounded(ctx context.Context, root string, cache *GitIgnoreCache, f if ctxErr := ctx.Err(); ctxErr != nil { return ctxErr } + if walkDuringTest != nil { + walkDuringTest(path) + } + // A directory listing can include an entry that another process + // removes before Lstat: treat that race as "skip", not fatal (#302). + // Missing walk root still fails so callers learn the path is gone. if err != nil { + if os.IsNotExist(err) && path != absRoot { + return nil + } return err } diff --git a/scanner/walker_test.go b/scanner/walker_test.go index 814ca9d1..390ce010 100644 --- a/scanner/walker_test.go +++ b/scanner/walker_test.go @@ -39,6 +39,46 @@ func TestScanFilesSkipsClaudeWorktreesByPath(t *testing.T) { } } +// TestScanFilesSkipsVanishedEntry removes a later sibling while the walk is +// on an earlier file. filepath.Walk Lstats after ReadDir, so that delete +// surfaces as ENOENT; the walk must skip it and still return the survivors +// (#302: SessionStart hook died on lstat of a short-lived watch.transition). +func TestScanFilesSkipsVanishedEntry(t *testing.T) { + root := t.TempDir() + keep := filepath.Join(projectpath.CanonicalPath(root), "a-keep.go") + vanish := filepath.Join(projectpath.CanonicalPath(root), "z-vanish.txt") + for _, path := range []string{keep, vanish} { + if err := os.WriteFile(path, []byte("x\n"), 0o644); err != nil { + t.Fatal(err) + } + } + + walkDuringTest = func(path string) { + if path == keep { + if err := os.Remove(vanish); err != nil { + t.Errorf("remove vanished sibling: %v", err) + } + } + } + t.Cleanup(func() { walkDuringTest = nil }) + + files, err := ScanFiles(context.Background(), root, nil, nil, nil) + if err != nil { + t.Fatalf("ScanFiles: %v (vanished mid-walk entry must not be fatal)", err) + } + if len(files) != 1 || files[0].Path != "a-keep.go" { + t.Fatalf("files = %v, want [{Path:a-keep.go}]", files) + } +} + +func TestScanFilesMissingRootStillErrors(t *testing.T) { + missing := filepath.Join(t.TempDir(), "no-such-dir") + _, err := ScanFiles(context.Background(), missing, nil, nil, nil) + if err == nil || !os.IsNotExist(err) { + t.Fatalf("missing root err = %v, want IsNotExist", err) + } +} + func TestIgnoredDirs(t *testing.T) { // Verify common directories are in the ignored list expectedIgnored := []string{ diff --git a/topology/provider.go b/topology/provider.go index 7ebd1907..906d4eab 100644 --- a/topology/provider.go +++ b/topology/provider.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io/fs" + "os" "path/filepath" "sort" "strings" @@ -222,7 +223,12 @@ func discoverInventoryWithLimit(ctx context.Context, root string, providers []Pr if err := ctx.Err(); err != nil { return err } + // A directory listing can include an entry removed before its + // Info/Lstat: skip it rather than aborting the whole inventory (#302). if walkErr != nil { + if os.IsNotExist(walkErr) && path != absRoot { + return nil + } return walkErr } if len(names) > 0 { @@ -270,6 +276,9 @@ func discoverInventoryWithLimit(ctx context.Context, root string, providers []Pr } info, err := entry.Info() if err != nil { + if os.IsNotExist(err) { + return nil + } return err } files = append(files, scanner.FileInfo{Path: filepath.Clean(rel), Size: info.Size(), Ext: ext}) From a5056f0fb136929359188d80798f4df0f27aa11b Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 21 Nov 2026 03:49:37 +0000 Subject: [PATCH 2/3] test(topology): cover mid-walk ENOENT skips in inventory (#302) Codecov flagged the topology walk's IsNotExist branches; exercise both entry.Info and directory ReadDir races the same way as the scanner test. Co-authored-by: Jordan Coin Jackson --- topology/provider.go | 8 +++++++ topology/provider_test.go | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/topology/provider.go b/topology/provider.go index 906d4eab..ba55c535 100644 --- a/topology/provider.go +++ b/topology/provider.go @@ -187,6 +187,11 @@ func discoverInventory(ctx context.Context, root string, providers []Provider, c return discoverInventoryWithLimit(ctx, root, providers, cfg, includeFiles, maxManifestWalkEntries) } +// inventoryWalkDuringTest, when set, runs at the start of each inventory +// WalkDir callback. Tests use it to delete a later sibling or the current +// directory and prove ENOENT is skipped rather than fatal (#302). +var inventoryWalkDuringTest func(path string, entry fs.DirEntry, walkErr error) + func discoverInventoryWithLimit(ctx context.Context, root string, providers []Provider, cfg config.ProjectConfig, includeFiles bool, maxEntries int) ([]scanner.FileInfo, []string, error) { if err := ctx.Err(); err != nil { return nil, nil, err @@ -223,6 +228,9 @@ func discoverInventoryWithLimit(ctx context.Context, root string, providers []Pr if err := ctx.Err(); err != nil { return err } + if inventoryWalkDuringTest != nil { + inventoryWalkDuringTest(path, entry, walkErr) + } // A directory listing can include an entry removed before its // Info/Lstat: skip it rather than aborting the whole inventory (#302). if walkErr != nil { diff --git a/topology/provider_test.go b/topology/provider_test.go index cbc01859..c3d38a0d 100644 --- a/topology/provider_test.go +++ b/topology/provider_test.go @@ -340,6 +340,53 @@ func TestDiscoverInventoryReportsManifestWalkLimit(t *testing.T) { } } +// TestDiscoverInventorySkipsVanishedEntries deletes a later file and the +// current directory while WalkDir is in progress. That surfaces as ENOENT +// from entry.Info and from the directory's ReadDir; both must be skipped +// rather than failing the inventory (#302). +func TestDiscoverInventorySkipsVanishedEntries(t *testing.T) { + root := t.TempDir() + abs := projectpath.CanonicalPath(root) + keep := filepath.Join(abs, "a-keep.go") + vanish := filepath.Join(abs, "z-vanish.go") + vanishDir := filepath.Join(abs, "z-dir") + for _, path := range []string{keep, vanish} { + if err := os.WriteFile(path, []byte("package p\n"), 0o644); err != nil { + t.Fatal(err) + } + } + if err := os.Mkdir(vanishDir, 0o755); err != nil { + t.Fatal(err) + } + + inventoryWalkDuringTest = func(path string, entry os.DirEntry, walkErr error) { + if walkErr != nil || entry == nil { + return + } + switch entry.Name() { + case "a-keep.go": + if err := os.Remove(vanish); err != nil { + t.Errorf("remove vanished file: %v", err) + } + case "z-dir": + if err := os.RemoveAll(path); err != nil { + t.Errorf("remove vanished dir: %v", err) + } + } + } + t.Cleanup(func() { inventoryWalkDuringTest = nil }) + + files, _, err := discoverInventory(context.Background(), root, []Provider{stubProvider{ + name: "go", version: "1", languages: []string{"go"}, + }}, config.ProjectConfig{}, true) + if err != nil { + t.Fatalf("discoverInventory: %v (vanished mid-walk entry must not be fatal)", err) + } + if len(files) != 1 || filepath.ToSlash(files[0].Path) != "a-keep.go" { + t.Fatalf("files = %#v, want [{Path:a-keep.go}]", files) + } +} + func TestRegisteredProvidersAreNameSorted(t *testing.T) { providerRegistryMu.Lock() original := append([]Provider(nil), providerRegistry...) From 2a17a35249b803e37a6a67c96d400e57f314b974 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 21 Nov 2026 03:57:12 +0000 Subject: [PATCH 3/3] fix(topology): Lstat inventory entries so Windows sees mid-walk deletes (#302) DirEntry.Info can return FindFirstFile-cached metadata after a remove, so TestDiscoverInventorySkipsVanishedEntries kept z-vanish.go on windows-latest. Co-authored-by: Jordan Coin Jackson --- topology/provider.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/topology/provider.go b/topology/provider.go index ba55c535..250c16b2 100644 --- a/topology/provider.go +++ b/topology/provider.go @@ -282,7 +282,10 @@ func discoverInventoryWithLimit(ctx context.Context, root string, providers []Pr if !languages[language] && !languages[strings.TrimPrefix(strings.ToLower(ext), ".")] { return nil } - info, err := entry.Info() + // Lstat rather than entry.Info: on Windows, DirEntry.Info can return + // FindFirstFile-cached metadata after the file was deleted, so a + // mid-walk remove would still look present (#302). + info, err := os.Lstat(path) if err != nil { if os.IsNotExist(err) { return nil