fix(workspace-fs): keep generated dirs out of the watch when git's ignored-dir listing fails or overflows its cap

PG
18h ago
+316-75

What & why

With useDefaultIgnores: false (host-service), the only thing keeping node_modules, build, .gradle and friends out of the recursive watch is git's ignored-dir listing (listGitIgnoredDirs). On Linux each dir it misses costs chokidar a crawl and one fs.watch per entry. Two gaps are still open on main after #9896, #9912 and #9916. They are two commits here.

1. A failed listing looks like "nothing ignored". listGitIgnoredDirs returns [] on any failure, including a 3 s timeout while many workspaces attach at once. A worker-pool rejection takes the same route in computeGitIgnoredRelDirs. The attach then crawls the whole tree, node_modules included, and nothing is logged. We saw one worktree hold ~300k inotify watches that way, while git ls-files answered in 0.3 s once the host was idle. #9896 can recover on a later git:changed event, but only after the crawl has already happened, and an idle worktree never emits one. On top of that, refreshIgnores turns a failed listing into [], reads it as every dir being un-ignored, and re-attaches with no gitignored prune at all.

  • listGitIgnoredDirs still returns [] for a non-git or missing root, and rejects on any other failure. git runs with LC_ALL=C so that check holds. git-watcher already logs a rejection and retries on its next emit.
  • A failed listing at attach prunes the static generated-dir globs (DEFAULT_IGNORE_PATTERNS) provisionally, only when default ignores are off. isPathPruned reports those paths as pruned, so targeted file watches still cover them.
  • The listing is retried through refreshIgnores with backoff (5 s, doubling to 5 min). Once git answers, the root re-attaches with git's list. That releases the provisional prune, so tracked build/vendor files are visible again, and listeners get an overflow.
  • A refresh whose listing fails keeps the current prune.

2. The 200-dir cap drops generated dirs. The listing keeps the first 200 dirs in git's sorted order. On a real monorepo checkout git lists 268 fully ignored dirs, and 222 of them are generated (node_modules, build, .gradle, dist). The cut drops 68 dirs, 13 of them node_modules, including the root node_modules, which sorts after every nested path. No ranking can fit 222 generated dirs under a cap of 200. #9912 does not reach these dirs: it reacts to create events, and chokidar runs with ignoreInitial, so a dir that already exists never produces one. The git-watcher refresh compares one capped list with another, sees no growth, and never re-attaches.

  • Every fully ignored dir named like generated output (DEFAULT_IGNORE_DIR_NAMES) is kept, with no cap. The 200 cap now bounds only the other ignored dirs. Only dirs git reports as fully ignored are pruned, as before, so tracked build/vendor files stay observable.
  • DEFAULT_IGNORE_PATTERNS and DEFAULT_IGNORE_DIR_NAMES move unchanged from search.ts into a dependency-free ignore-defaults.ts with its own package entry. The watcher worker that runs the listing can then import them without loading the search code.

This is independent of #7878 (the idle keep-alive). It targets main, and the two merge in either order.

How I tested it

  • New watch-gitignored-listing-failure.test.ts (fake backend, retry delay passed in as 1 ms, waits on events rather than time):
    • an attach whose listing fails prunes **/node_modules/**, then re-attaches with git's list once a retry answers;
    • a refresh whose listing fails keeps the current prune and doesn't re-attach.
  • ignored-dirs.test.ts, against real git repos:
    • a corrupt index rejects instead of returning [];
    • with 205 build dirs, 205 other ignored dirs and a node_modules that sorts last, every generated dir is kept and only the others are capped at 200.
  • Fork CI restores main's watch.ts, search.ts and ignored-dirs.ts under the new tests: all 4 fail. Against the first commit, the 3 listing-failure tests pass and the cap test still fails. Then the workspace-fs suite (211 pass, 2 skipped, 0 fail), the host-service suite (3026 pass, 1 skipped, 0 fail), the workspace-fs, host-service and desktop typechecks, and Biome all pass: https://github.com/patiently/superset/actions/runs/45042224364

Checklist

  • PR title follows conventional commits (type(scope): subject)
  • Tests, typecheck and lint pass (fork CI run above)
  • "Allow edits from maintainers": not available on a PR from an organization-owned fork. Happy to push any changes you'd like.

Summary by cubic

Fixes two gaps in the git ignored-dir listing that let watches crawl generated directories (node_modules, build, …) and balloon inotify usage when useDefaultIgnores is off.

  • A failed listing now rejects instead of returning [], so attach timeouts or worker rejections no longer look like “nothing ignored”. The attach provisionally prunes generated dirs and retries with backoff (5 s doubling to 5 min) until git answers, then re-attaches with git’s list and releases the provisional prune.
  • The 200-dir cap no longer applies to generated dirs – they’re always kept. Other ignored dirs stay capped. A refresh that fails keeps the current prune rather than clearing it.
  • DEFAULT_IGNORE_PATTERNS and DEFAULT_IGNORE_DIR_NAMES move to a dependency-free ignore-defaults module so the listing worker can import them without loading search code.

Summary by CodeRabbit

  • Bug Fixes
    • File watching now distinguishes Git ignore-listing failures from an empty list and retries failed listings automatically. Existing ignored paths remain in effect during failed refreshes.
    • Common generated directories continue to be excluded while Git ignore information is unavailable.
    • Git-ignored directory results are no longer capped for common default-ignore names; other results remain limited to 200.