diff --git a/scanner/walker.go b/scanner/walker.go index 945ca2e..8f7876d 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 814ca9d..390ce01 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 7ebd190..250c16b 100644 --- a/topology/provider.go +++ b/topology/provider.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io/fs" + "os" "path/filepath" "sort" "strings" @@ -186,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 @@ -222,7 +228,15 @@ 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 { + if os.IsNotExist(walkErr) && path != absRoot { + return nil + } return walkErr } if len(names) > 0 { @@ -268,8 +282,14 @@ 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 + } return err } files = append(files, scanner.FileInfo{Path: filepath.Clean(rel), Size: info.Size(), Ext: ext}) diff --git a/topology/provider_test.go b/topology/provider_test.go index cbc0185..c3d38a0 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...)