Repository navigation
Description
On Linux the host-service watches each worktree with chokidar 4, which opens an fs.watch (one inotify watch) on every file and every directory, and crawls the whole tree on the host event loop with two lstat calls per entry. An inotify watch on a directory already reports changes to the files in it (writes, chmod, create, delete, rename), with the child's name. The per-file watches only add a duplicate copy of those events. Watching directories only is what @parcel/watcher (and so VS Code) does on Linux, and what chokidar's unreleased v6 does ("files never own handles", docs/architecture.md on its v6 branch).
For a large repository the cost is high. An LLVM worktree (16.9k directories, 186k files), measured on the same 32-core machine under the same load, alternating runs:
| chokidar 4.0.3 (current) | directory-only prototype | |
|---|---|---|
| time to attach | 15.0–15.9 s | 1.8–2.2 s |
| max event-loop stall during attach | 840–900 ms | 26–27 ms |
| inotify watches | 203,384 | 16,927 |
| RSS / JS heap | 1.2 GB / ~880 MB | 140 MB / ~58 MB |
In production, with several LLVM workspaces, the host-service's vitals log shows FSEventWrap going between ~100 and 420k, event-loop busyShare at 1.0, and delays of up to 4.4 s. While that happens, terminal create/attach and every other request to that host stall. Each attach also uses ~200k of the user's fs.inotify.max_user_watches (default 524,288 on many distributions), so two or three such workspaces are enough to get ENOSPC.
#7878 and #7879 reduce how often a worktree is crawled. This issue is about how much each crawl costs, so the two fixes complement each other.
Equivalence. I ran chokidar (configured as in chokidar-backend.ts) and a directory-only prototype side by side over 21 file operations: create, append, truncate, atomic save, chmod, touch, mkdir -p followed by an immediate write, file and directory renames, rm -rf, file replaced by a directory, symlinks, a burst of 200 files, an ignored .git write, and a directory moved in from outside the tree. After coalesceWatchEvents, the two produce the same events, with one exception: hard links. A per-file watch follows the inode, so it reports a write made through a link outside the tree, or through a second link inside it. A directory watch does not (inotify(7): "events are not generated for the files inside the directory when the events are performed via a pathname (i.e., a link) that lies outside the monitored directory"). Working-tree files that git checks out are never hard links, and node_modules is already ignored.
Proposal. Replace the Linux chokidar backend with a small directory-only backend (one fs.watch per directory, lstat on each event to classify it as create, update or delete), and ideally run it in a worker, as git-directory-worker.ts already does since #7865. That worker uses fs.watch(path, { recursive: true }), which on Linux is Node's JS emulation and also watches every file (lib/internal/fs/recursive_watch.js), so it could use the same backend.
A directory-only backend has to get these right:
- watch a new directory before scanning it, so files created in between are not missed;
- emit deletes for the descendants when a directory is moved away;
- re-attach after an atomic replace of a directory.
libuv drops IN_Q_OVERFLOW, so neither the current backend nor a fs.watch-based one can detect lost events. That is not a regression.
Activity
github-actions commentedon Oct 8, 2026
Triage: perf(workspace-fs): watch only directories on Linux
Summary. On Linux the host-service watches each workspace worktree with chokidar 4. Chokidar opens one
fs.watch(one inotify watch) for every file and every directory that is not ignored, and builds that set by crawling the whole tree on the host-service main event loop. In a very large repo (LLVM, about 203k entries), attaching takes about 15 s and blocks the event loop for up to about 900 ms at a time. It uses about 1.2 GB RSS and about 200k of the user'sfs.inotify.max_user_watches. Requests to the host, such as terminal create/attach, stall while this runs. Two or three such workspaces are enough to hitENOSPC. The issue proposes a directory-only backend: one inotify watch per directory, withlstaton each event to classify it. Ideally it would run in a worker. I read the code and the report fits it. This is a design and performance issue, not a logic bug.Affected code
packages/workspace-fs/src/watch-backend/index.ts:21-25:defaultWatchBackend()pickschokidarWatchBackendon Linux. The comment there says why@parcel/watcheris not used on Linux: its inotify thread crashes on EINTR. So the directory-only approach that parcel uses is currently not available on Linux.packages/workspace-fs/src/watch-backend/chokidar-backend.ts:22-30:watch(rootPath, { ignoreInitial: true, followSymlinks: false, atomic: false, ignored }). Chokidar 4 adds anfs.watchfor each file it finds while crawling.ignoreInitialonly stops the initialaddevents from being sent; the crawl, thelstatcalls and the per-file watches still happen. The returned promise resolves only onready, at the end of the full crawl (:39-43), so the attach time is exactly the crawl time.packages/workspace-fs/src/watch.ts:668:FsWatcherManagercallsthis.backend.subscribe(...)on the host-service event loop. TheENOSPChandling atwatch.ts:523-533logs only once and does not fall back to anything.packages/host-service/src/workers/git-directory-worker.ts:15-21: usesfs.watch(path, { recursive: true }). On Linux, Node implements recursive watch in JS (lib/internal/fs/recursive_watch.js), which also opens a watch per file. This one already runs off the main thread (started fromworkers/host-worker.ts:53, driven byevents/git-directory-watcher.ts:56), so its cost is inotify watches and memory, not event-loop stalls.Root cause
fs.watchfor every non-ignored file in addition to every directory. On Linux an inotify watch on a directory already reports create, modify, attrib, delete and move for its children, so the extra watches only duplicate events. That explains the watch count: about 203k watches against about 17k directories.lstatcalls run on the host-service main loop, which explains the stalls.watch.ts:641-647, compiled byignore-matcher.ts) reduces the tree. It does nothing for source-heavy trees like LLVM, where most files are tracked.Reproduction
Existing benchmark.
packages/workspace-fs/src/watch-backend/chokidar-crawl.bench.tsalready measuresattachMs, peak RSS and event-loop max lag forchokidarWatchBackend. Its fixture is small (100 packages × 40 files per root). To reproduce the report:packageCountandvisibleFilesPerPackage(or point it at an LLVM checkout);find /proc/$PID/fdinfo -type f | xargs grep -c '^inotify' | awk -F: '{s+=$2} END {print s}'.Expect watches ≈ files + directories with chokidar, against ≈ directories with a directory-only backend.
Quick manual check. Open a large repo as a workspace on Linux. Watch the host-service vitals log for
FSEventWrapcount andbusyShare, and time a terminal attach while the watcher is attaching.Suggested fix direction
New backend. Add something like
watch-backend/directory-backend.tsthat implementsNativeWatchBackend(types.ts):readdir({ withFileTypes: true })walk that prunes withcreateIgnoreMatcher. That gives directory-ness without a separatelstatper entry.fs.watch(dir)per directory.lstatdir/filenameto emitcreate,updateordelete.defaultWatchBackend()on Linux.Edge cases. The ones the issue lists are the right ones:
createfor whatever the scan finds.deletefor its known descendants. This needs an in-memory directory→children index.With
atomic: falsethe current backend lets the rename half reachwatch-event-coalescing.ts; the new backend should keep that behaviour.Tests.
watch-backend.test.tsandchokidar-ignore.test.tsshould run against both backends, and the 21-operation equivalence matrix from the issue would be a good test fixture. PerAGENTS.md, keep it to one test per behaviour and use no real timeouts.Worker. Moving the walk into a worker (as
git-directory-worker.tsdoes) removes the remaining stall. That worker'srecursive: truewatch could then use the same backend.Hard links. A directory watch does not see writes made through a hard link that lives outside the watched tree. Accepting that is reasonable for git checkouts, but it should be written down next to the backend.
AlexGodard commentedon Oct 24, 2026
A data point from two Linux hosts running the standalone host service, 1.40.0 (one native Arch, one WSL2 Ubuntu, 32 vCPUs), on a TypeScript monorepo.
On both, the
[host-service:vitals]line holdsFSEventWrapat about 13.1k. One worktree of the repo has 12,235 files in 987 directories, 13,222 paths, so the count matches one watch per file and per directory, as described here. Directory-only watching would put it near 1k, about 13× fewer.On the WSL2 host the count also jumped from about 13.1k to 20.3k within one minute, at a time no workspace was being created there.
We haven't tied our own event-loop stalls to the watcher (ours came from memory pressure), so this is the watch count only.
perf(workspace-fs): watch only directories on Linux
maleadt commentedon Nov 14, 2026
I'm moving away from Paradev because of issues like this, so closing this.