Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null-proofing in files_watcher #47694

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1667,7 +1667,7 @@ added:
should stop.
* Returns: {AsyncIterator} of objects with the properties:
* `eventType` {string} The type of change
* `filename` {string|Buffer} The name of the file changed.
* `filename` {string|Buffer|null} The name of the file changed.

Returns an async iterator that watches for changes on `filename`, where `filename`
is either a file or a directory.
Expand Down Expand Up @@ -4489,7 +4489,7 @@ changes:
* `signal` {AbortSignal} allows closing the watcher with an AbortSignal.
* `listener` {Function|undefined} **Default:** `undefined`
* `eventType` {string}
* `filename` {string|Buffer}
* `filename` {string|Buffer|null}
* Returns: {fs.FSWatcher}

Watch for changes on `filename`, where `filename` is either a file or a
Expand Down
11 changes: 9 additions & 2 deletions lib/internal/watch_mode/files_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,20 @@ class FilesWatcher extends EventEmitter {
return [...this.#watchers.keys()];
}

[Symbol.for('emitChangeEventWithoutFilename')]() {
this.#watchers.values().forEach(watcher => watcher.emit('change', 'change', null));
}
sosoba marked this conversation as resolved.
Show resolved Hide resolved

watchPath(path, recursive = true) {
if (this.#isPathWatched(path)) {
return;
}
const watcher = watch(path, { recursive });
watcher.on('change', (eventType, fileName) => this
.#onChange(recursive ? resolve(path, fileName) : path));
watcher.on('change', (eventType, fileName) => {
if (fileName != null) {
this.#onChange(recursive ? resolve(path, fileName) : path);
}
});
this.#watchers.set(path, { handle: watcher, recursive });
if (recursive) {
this.#removeWatchedChildren(path);
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-watch-mode-files_watcher.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,12 @@ describe('watch mode file watcher', () => {
}
assert.deepStrictEqual(watcher.watchedPaths, expected);
});

it('should ignore watch event without filename', async () => {
watcher.on('changed', common.mustNotCall());
watcher.watchPath(tmpdir.path);
watcher[Symbol.for('emitChangeEventWithoutFilename')]();
// Wait for this long to make sure changes are not triggered
await setTimeout(1000);
});
});