fix(workspace-fs): re-attach the chokidar watch when the gitignored set grows
Fix 1 from plans/20261117-host-service-memory.md (#9868): stop host-service watching gitignored directories that appear after a watch attaches.
What changed
On Linux, defaultWatchBackend() is chokidar, which holds one fs.watch per file and per directory. Gitignored dirs were pruned only if they existed at attach time. FsWatcherManager.refreshIgnores swapped the subscription only when the ignored set shrank, so a node_modules / .next / dist created later was watched file by file until the last client unwatched.
NativeWatchBackendgets an optionalwatchesEachEntryflag. Only the chokidar backend sets it.refreshIgnoresnow records a changed ignored set on every backend. A dir that is pruned but not yet in the native subscription (filteredRelPrefixes) has its events dropped before they reach clients, andisPathPrunedcounts it. So a gitignored dir created after attach looks the same to clients on parcel and chokidar. Parcel never re-attaches for such a dir, either when it becomes ignored or when it is un-ignored again (no FSEvents re-subscribe, see the "deaf after subscribe" note inwatch.ts).refreshIgnoresalso re-attaches when the set grew, but only on a backend withwatchesEachEntry, because only there does the prune free memory. Growth re-attaches are limited to one per 30 s per watch root (growthReattachMinMs). If a growth comes in inside that window, one trailing timer retries at the end of the window, so the last growth is not lost (git-watcher filters events under a dir it already knows is ignored, so nothing else would trigger the refresh again). Shrink swaps stay immediate, as before.- Each change of the pruned set emits one root
overflowevent: a re-attach emits it after the swap, and a filter-only change emits it at once. Clients already handle this event at attach and after an FSEvents overflow: the file tree and open documents refetch, and git-watcher does a broad status refresh. This covers events written during the swap gap. GitWatcher.refreshIgnoredDirscallsrefreshWatcherIgnoreswhen its own listing grew, not only when ignore rules changed. It is still rate-limited by the existing 5 s floor.
Why re-attach, not pruning in the chokidar ignored callback
The callback option needs a synchronous "does git ignore this?" answer. A basename check against DEFAULT_IGNORE_DIR_NAMES stops watching a dist that is tracked in git. The callback also only stops new entries: dropping watches already made for a half-written tree needs chokidar's private _closers, because unwatch(dir) closes only that one path. A dir pruned that way would also not be in prunedRelPrefixes, so a later un-ignore would not be detected. Re-attaching uses the existing, tested swap path and has none of these gaps. Its cost is one re-crawl of the non-ignored tree, at most once per 30 s.
Requirements
- A gitignored dir created after attach becomes unwatched (Linux/chokidar): measured below, 65,656 watches → 24.
- A dir that is not gitignored, created after attach, is still watched and its events reach clients:
create:…/newdir/b.tswas delivered over/eventsin both runs. - Un-ignore still works: that is the existing shrink path, unchanged. Its test (
watch-gitignored-prune.test.ts›refreshIgnores) passes. I did not repeat it on the second instance. - No unbounded re-crawl: growth re-attaches happen at most once per 30 s per root. The unit test checks this with an injected clock.
- macOS/Windows: parcel does not set
watchesEachEntry, so it never re-attaches on growth. Its client-visible behaviour now matches Linux (table below). That is a change on macOS/Windows: events under a gitignored dir created after attach no longer reachfs:watchclients, which is what already happens to the same dir when it exists before attach. isPathPrunedand the new filter compare POSIX-style relative paths, so nested prefixes such aspackages/a/node_modulesalso match on Windows (before,path.relativegave\separators there). Also shared: git-watcher now callsrefreshWatcherIgnoreswhen the listing grows, which runs one extragit ls-filesand nested-repo scan per growth (at most once per 5 s) and then returnsfalse. This also happens once at watch start, because the first listing grows from empty.- Backend choice is unchanged.
Parity: parcel (macOS/Windows) and chokidar (Linux)
watch-ignore-growth.test.ts runs each case on a fake backend in both modes (watchesEachEntry true and false). The fake drops events with the same ignore matcher the real backends use. Each case asserts that both modes give the same transcript, and that it is the expected one. The transcript lists the events the client got and the isPathPruned answer.
| Case | Events the client gets (both backends) | isPathPruned (both) | chokidar | parcel |
|---|---|---|---|---|
| (a) ignored dir present before attach | none from inside it | true | pruned at attach | pruned at attach |
| (b) ignored dir created after attach | its events until the refresh, then one overflow, then none | true after the refresh | re-attach (at most 1 per 30 s); filter in between | filter only, no re-attach |
| (c) dir un-ignored later (ignored at attach, or ignored after attach) | one overflow per set change, then its events | false | re-attach | re-attach if it was pruned at attach (as before); filter only if it was pruned after attach |
| (d) non-ignored dir created after attach | its events | false | no change | no change |
Linux delay: when a growth comes in during the 30 s window, the events are dropped at once, but the memory comes back only at the trailing re-attach. That re-attach sends one more overflow (it covers the swap gap). On parcel there is no gap and no second event. That extra event is the only difference, and it comes up to 30 s later.
Confirmed first that the previous commit had this gap: on that code the parity test failed for parcel in (b) (create:node_modules/b.js delivered, pruned=false) and in (c') (no overflow), while chokidar passed.
Measured saving
Measured on the first commit, then repeated on the final commit: after 45 s, 22 watches; at +3 min, 195 MB and 24 watches.
Second host-service instance on port 4979, SUPERSET_HOST_RUN_MODE=local, its own HOME, run dir and copy of host.db, SUPERSET_API_URL pointed at a closed port. Bundles built with bun run build.ts from main (before) and from this branch (after), run on the released runtime's node. A tmpfs git repo (/dev/shm, .gitignore = node_modules/) was the only workspace. A WebSocket client on /events sent git:watch + fs:watch. The live host-service of this workspace was not touched. Watches = inotify wd: lines in /proc/<pid>/fdinfo. heapUsed is from the [host-service:vitals] line, logged once per minute, so it lags by up to 60 s.
| Step | before RSS | before watches | before heapUsed | after RSS | after watches | after heapUsed |
|---|---|---|---|---|---|---|
| Idle, before attach | 176 MB | 0 | 176 MB | 0 | ||
git:watch + fs:watch attached (3-entry repo) | 229 MB | 22 | 229 MB | 22 | ||
node_modules/ created after attach (60,000 files, 6,061 dirs), +0 s | 485 MB | 48,774 | 491 MB | 57,461 | ||
| same, +45 s | 529 MB | 65,654 | 282 MB | 513 MB | 22 | 42 MB |
new non-ignored newdir/b.ts created and written | 559 MB | 65,656 | 282 MB | 524 MB | 24 | 42 MB |
— event delivered to the /events client | create:…/newdir/b.ts | create:…/newdir/b.ts | ||||
| same, +3 min | 371 MB | 65,656 | 189 MB | 192 MB | 24 | 33 MB |
At +3 min the after instance is back to its attached baseline (192 MB, 24 watches), and the before instance still holds 65,656 watches. RSS lags the watch count because V8 gives back freed pages slowly. Using the report's 6.5 KB per entry, the expected saving for one bun install of this repo after attach is about 1.5 GB. During the burst the watch count still climbs until the next git-watcher refresh (5 s floor) re-attaches. The +0 s row shows that transient.
Both runs logged the existing throttler buffer full — dropping events warning during the 60k-file burst. That behaviour is unchanged by this PR.
Checks
Ran:
bun testinpackages/workspace-fs: 198 pass, 2 skip, 0 fail. This includeswatch-ignore-growth.test.ts: 5 parity cases × 2 backend modes, plus the rate-limit case, all on a fake backend with an injected clock (about 100 ms).bun testinpackages/host-service: 2896 pass, 23 fail. The 23 arehealth.check › serialises the current boot's stamps…and 22session: …tests intest/integration/background-naming.integration.test.ts. The same tests fail onmainwithout this change in a full-suite run. Run alone, the session tests pass on both, andhealth.checkfails on both. None of them touch the watcher.bun run lint:fix: one file reformatted, no remaining issues.- Typecheck:
bun run typecheck(turbo, concurrency 3) was OOM-killed (exit 137 inhost-service#build:types) on this 8 GB box with the dev stack running. I re-ran the same turbo task asturbo typecheck --concurrency=1: 42/42 successful on the first commit. On the final commit, I ran it forworkspace-fsandhost-serviceonly: 6/6 successful. - Second-instance measurement above.
Not run:
- Un-ignore on the second instance (covered by the existing unit test only).
- macOS/Windows (the real parcel backend). Parcel parity is shown only with the fake backend. A real
bun installwas not run either (synthetic tree, as in the report). - Docker / image tests: not needed for this change.
Summary by CodeRabbit
- Bug Fixes
- File watching now updates when newly ignored directories appear, helping prevent changes beneath those directories from triggering unnecessary notifications.
- Watcher refreshes for growing ignored-directory lists are rate-limited to avoid repeated reattachments. Existing ignored-directory changes that require a refresh continue to be applied promptly.
- Filtering of ignored paths now works consistently across platforms, including Windows.