Skip to content

Commit f1fefcb

Browse files
committed
fs: skip watching ignored paths in recursive watcher
Instead of just filtering events, skip watching ignored paths entirely to avoid kernel resource pressure from unnecessary file watchers. This is especially important for large directories like node_modules.
1 parent fa14e42 commit f1fefcb

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/internal/fs/recursive_watch.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,15 @@ class FSWatcher extends EventEmitter {
130130
}
131131

132132
const f = pathJoin(folder, file.name);
133+
const relativePath = pathRelative(this.#rootPath, f);
134+
135+
// Skip watching ignored paths entirely to avoid kernel resource pressure
136+
if (this.#ignoreMatcher?.(relativePath)) {
137+
continue;
138+
}
133139

134140
if (!this.#files.has(f)) {
135-
this.#emitChange('rename', pathRelative(this.#rootPath, f));
141+
this.#emitChange('rename', relativePath);
136142

137143
if (file.isSymbolicLink()) {
138144
this.#symbolicFiles.add(f);

test/parallel/test-fs-watch-ignore-recursive.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ tmpdir.refresh();
130130

131131
const watcher = fs.watch(testDirectory, {
132132
recursive: true,
133-
// Use array to match both the directory itself and files inside it
134-
// On macOS, FSEvents may report events for the directory when files change inside it
133+
// On Linux, matching the directory skips watching it entirely.
134+
// On macOS, the native watcher still needs to filter file events inside.
135135
ignore: ['**/node_modules/**', '**/node_modules'],
136136
});
137137

0 commit comments

Comments
 (0)