Skip to content

Commit b88ac30

Browse files
committed
fix: don't emit "error" for EPERM/ENOENT on watched dir (#46)
Match the existing console.error suppression: pre-Node-v22 Windows synthesizes EPERM when the watched directory is renamed, and Linux fires ENOENT when it is deleted. Both already surface through the remove events that onDirectoryRemoved triggers, so emitting an additional "error" event would be noisy. Real errors (ENOSPC, EMFILE, etc.) still propagate. Add a regression test asserting EPERM/ENOENT do not propagate to subscriber error listeners; expand the changeset to spell out the cross-platform behavior. https://claude.ai/code/session_0146XgwrLMQwsLYN4WuT2c9n
1 parent e5036e6 commit b88ac30

3 files changed

Lines changed: 51 additions & 10 deletions

File tree

‎.changeset/surface-watcher-errors.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ through `errno` tables.
2020
The change is additive: the existing `console.error` log is preserved,
2121
and `Watchpack` only emits `error` when a listener is attached, so
2222
consumers that don't subscribe see no behavior change.
23+
24+
Cross-platform: `EPERM` / `ENOENT` are excluded from the new `error`
25+
event for the same reason they were already excluded from the
26+
`console.error` log. On Windows pre-Node-v22 `fs.watch` synthesizes
27+
`EPERM` when the watched directory is renamed; on Linux `ENOENT`
28+
fires when it is deleted. Both are already represented by the
29+
`remove` events that follow, so re-reporting them as `error` would be
30+
noisy. macOS uses recursive FSEvents and surfaces real errors (e.g.
31+
`EMFILE`) through the same path. Polling mode does not exercise
32+
`fs.watch`, so the new event only fires for native-watcher failures.

‎lib/DirectoryWatcher.js‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -692,16 +692,26 @@ class DirectoryWatcher extends EventEmitter {
692692
if (code !== "EPERM" && code !== "ENOENT") {
693693
// eslint-disable-next-line no-console
694694
console.error(`Watchpack Error (watcher): ${err}`);
695-
}
696-
// Surface the error to subscribers so callers can react instead of
697-
// silently treating the directory as removed (fixes #46). Skip
698-
// watchers without an "error" listener: EventEmitter throws when
699-
// "error" is emitted with no handler attached, which would be a
700-
// breaking change for callers that never subscribed.
701-
for (const set of this.watchers.values()) {
702-
for (const w of set) {
703-
if (w.listenerCount("error") > 0) {
704-
w.emit("error", /** @type {Error | NodeJS.ErrnoException} */ (err));
695+
// Surface the error to subscribers so callers can react instead
696+
// of silently treating the directory as removed (fixes #46).
697+
// EPERM/ENOENT are excluded here for the same reason the
698+
// console log is: on Windows pre-Node-v22, fs.watch synthesizes
699+
// EPERM when the watched directory itself is renamed, and on
700+
// Linux ENOENT fires when it is deleted. Both already surface
701+
// through the `remove` events emitted by onDirectoryRemoved
702+
// below, so re-reporting them as `error` would be noisy.
703+
// Skip watchers without an "error" listener: EventEmitter
704+
// throws when "error" is emitted with no handler attached,
705+
// which would be a breaking change for callers that never
706+
// subscribed.
707+
for (const set of this.watchers.values()) {
708+
for (const w of set) {
709+
if (w.listenerCount("error") > 0) {
710+
w.emit(
711+
"error",
712+
/** @type {Error | NodeJS.ErrnoException} */ (err),
713+
);
714+
}
705715
}
706716
}
707717
}

‎test/DirectoryWatcherExtra.test.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,27 @@ describe("DirectoryWatcher extra coverage", () => {
193193
});
194194
});
195195

196+
it("onWatcherError does not emit 'error' for EPERM or ENOENT (matches console suppression on Windows dir-rename / Linux dir-delete)", (done) => {
197+
const dw = new DirectoryWatcher(
198+
getWatcherManager(EMPTY_OPTIONS),
199+
fixtures,
200+
EMPTY_OPTIONS,
201+
);
202+
const errorSpy = jest.spyOn(console, "error").mockImplementation(() => {});
203+
const w = dw.watch(path.join(fixtures, "a"));
204+
/** @type {NodeJS.ErrnoException[]} */
205+
const received = [];
206+
w.on("error", (err) => received.push(err));
207+
dw.onWatcherError(Object.assign(new Error("perm"), { code: "EPERM" }));
208+
dw.onWatcherError(Object.assign(new Error("ent"), { code: "ENOENT" }));
209+
expect(received).toHaveLength(0);
210+
errorSpy.mockRestore();
211+
testHelper.tick(100, () => {
212+
dw.close();
213+
done();
214+
});
215+
});
216+
196217
it("onWatcherError emits 'error' to subscribed watchers", (done) => {
197218
const dw = new DirectoryWatcher(
198219
getWatcherManager(EMPTY_OPTIONS),

0 commit comments

Comments
 (0)