Skip to content

Commit e5036e6

Browse files
committed
fix: guard per-watcher error emit with listenerCount (#46)
EventEmitter throws "Unhandled error" when "error" is emitted on a Watcher with no handler attached. The previous commit unconditionally emitted on every subscribed Watcher, which broke existing tests (test/DirectoryWatcher.test.js "should log errors emitted from watcher to stderr") that exercise onWatcherError without subscribing. Mirror the listenerCount guard already in Watchpack._onError so the emit is a no-op when nothing is listening; the existing console.error fallback still surfaces the error. https://claude.ai/code/session_0146XgwrLMQwsLYN4WuT2c9n
1 parent ead223e commit e5036e6

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

‎lib/DirectoryWatcher.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,15 @@ class DirectoryWatcher extends EventEmitter {
694694
console.error(`Watchpack Error (watcher): ${err}`);
695695
}
696696
// Surface the error to subscribers so callers can react instead of
697-
// silently treating the directory as removed (fixes #46).
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.
698701
for (const set of this.watchers.values()) {
699702
for (const w of set) {
700-
w.emit("error", /** @type {Error | NodeJS.ErrnoException} */ (err));
703+
if (w.listenerCount("error") > 0) {
704+
w.emit("error", /** @type {Error | NodeJS.ErrnoException} */ (err));
705+
}
701706
}
702707
}
703708
this.onDirectoryRemoved("watch error");

0 commit comments

Comments
 (0)