Skip to content

Commit 72135a8

Browse files
committed
fix: handle potential undefined result in NativeWatchFileSystem and improve error handling in DiskWatcher
1 parent 85b90ec commit 72135a8

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

crates/rspack_fs/src/watcher/disk_watcher.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,16 @@ impl DiskWatcher {
8888
for pattern in already_watched_paths.difference(&current_should_watch_paths) {
8989
// If the path is no longer in the patterns to watch, unwatch it
9090
if let Some(watcher) = &mut self.inner {
91-
watcher
92-
.unwatch(pattern)
93-
.map_err(|e| rspack_error::error!(e))?;
91+
// FIXME:
92+
// we will unwatch the path is unnecessary, but we don't have a way to check if the path is still in the patterns
93+
// notify will remove the watch path when path is removed in it's inner.
94+
// If we unwatch the path again, it will return a error.
95+
// So we just ignore the error here.
96+
if let Err(e) = watcher.unwatch(pattern) {
97+
if !matches!(e.kind, notify::ErrorKind::WatchNotFound) {
98+
return Err(rspack_error::error!(e));
99+
}
100+
}
94101
}
95102
}
96103

packages/rspack/src/NativeWatchFileSystem.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,17 @@ export default class NativeWatchFileSystem implements WatchFileSystem {
105105
[Array.from(directories.added!), Array.from(directories.removed!)],
106106
[Array.from(missing.added!), Array.from(missing.removed!)],
107107
(err: Error | null, result) => {
108-
const { changedFiles, removedFiles } = result;
109-
110-
const fs = this.#inputFileSystem;
111-
for (const item of changedFiles) {
112-
fs.purge?.(item);
113-
}
114-
for (const item of removedFiles) {
115-
fs.purge?.(item);
108+
// !!if there is an error, result maybe a undefined value
109+
const changedFiles = result?.changedFiles || [];
110+
const removedFiles = result?.removedFiles || [];
111+
if (this.#inputFileSystem?.purge) {
112+
const fs = this.#inputFileSystem;
113+
for (const item of changedFiles) {
114+
fs.purge?.(item);
115+
}
116+
for (const item of removedFiles) {
117+
fs.purge?.(item);
118+
}
116119
}
117120

118121
// TODO: add fileTimeInfoEntries and contextTimeInfoEntries

0 commit comments

Comments
 (0)