fix(workspace-fs): drop a new-dir ignore answer that a full listing overtook

Merged
SA
7d ago
+76-3

What & why

Follow-up to #9912, from three CodeRabbit comments read after the merge. I checked each against the code. Two are real and fixed here; one is not reachable.

1. Stale scoped new-dir answer (real, fixed). checkNewDirsIgnored checked only aborted and subscription after its scoped git ls-files. Suppose a full refreshIgnores lists while that query runs and finds the dir not ignored. The older scoped answer was then still applied: the dir went back into prunedRelPrefixes and filteredRelPrefixes, its events were dropped, and chokidar skipped its new entries until the next growth re-attach (up to 30 s).

Fix: ignoreListingEpoch is bumped whenever a full listing is applied, at attach and in refreshIgnores. A scoped answer whose epoch is older is discarded, and its dirs are queued for a new check.

2. Flaky first test in watch-new-dir-prune.test.ts (real, fixed). The early check schedules an immediate growth re-attach. That re-attach resets filteredRealPaths, which firstRequest.isPruned reads, so the 2 ms poll could miss the short pruned window. The test now holds the full listing behind a gate until its assertions are done, then releases it and checks the replacement subscription.

3. Queued refreshIgnores running at the same time as another (hardened). Today a caller cannot reach this: refreshIgnores is async, so the promise callers hold resolves at least one microtask hop after the queued continuation (run.catch().then()) has started its run. Correctness rested on that one-hop margin, though. The continuation now goes back through the same per-state serialized entry (refreshIgnoresSerialized) instead of starting a run directly, so it queues behind any run that started first. It uses the WatcherState it was queued for, not a path lookup, so it never acts on a re-created watcher. The regression test runs a queued refresh after a refresh started as the active one settles makes two queued calls plus first.then(() => refreshIgnores()) and asserts that at most one full listing is in flight. It passes on main as well, because of the margin above, so it guards the ordering against future edits rather than reproducing a current failure.

How I tested it

  • bun test in packages/workspace-fs: 203 pass, 2 skip, 0 fail.
  • The new test rechecks a new dir whose scoped answer predates a full refresh fails on main, and with only the epoch guard disabled (no recheck happens; the stale result prunes the dir). It passes with the fix.
  • watch-new-dir-prune.test.ts was run repeatedly (9 runs over both commits): 0 failures.
  • tsc --noEmit in packages/workspace-fs passes, and biome check reports no issues.

Not run: host-service tests, because no host-service code changed; and no real-install measurement, because the change only touches the stale-answer path.

Summary by CodeRabbit

  • Bug Fixes
    • Improved file-watching consistency for newly created directories that match ignore rules, including when ignore checks and refreshes happen at the same time.
    • Prevented overlapping full ignore-rule refreshes, helping ensure directory changes are handled reliably.