Skip to content

fix: NativeWatchFileSystem should handle inputFileSystem when files change #11164

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
13 changes: 10 additions & 3 deletions crates/rspack_fs/src/watcher/disk_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,16 @@ impl DiskWatcher {
for pattern in already_watched_paths.difference(&current_should_watch_paths) {
// If the path is no longer in the patterns to watch, unwatch it
if let Some(watcher) = &mut self.inner {
watcher
.unwatch(pattern)
.map_err(|e| rspack_error::error!(e))?;
// FIXME:
// we will unwatch the path is unnecessary, but we don't have a way to check if the path is still in the patterns
// notify will remove the watch path when path is removed in it's inner.
// If we unwatch the path again, it will return a error.
// So we just ignore the error here.
if let Err(e) = watcher.unwatch(pattern) {
if !matches!(e.kind, notify::ErrorKind::WatchNotFound) {
return Err(rspack_error::error!(e));
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_fs/src/watcher/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ fn create_execute_aggregate_task(
let mut files = files.lock().await;
if files.is_empty() {
running.store(false, Ordering::Relaxed);
return;
continue;
}
std::mem::take(&mut *files)
};
Expand Down
30 changes: 28 additions & 2 deletions packages/rspack/src/NativeWatchFileSystem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import binding from "@rspack/binding";
import type Watchpack from "watchpack";
import type { FileSystemInfoEntry, Watcher, WatchFileSystem } from "./util/fs";
import type {
FileSystemInfoEntry,
InputFileSystem,
Watcher,
WatchFileSystem
} from "./util/fs";

/**
* The following code is modified based on
Expand Down Expand Up @@ -36,6 +41,11 @@ const toJsWatcherIgnored = (

export default class NativeWatchFileSystem implements WatchFileSystem {
#inner: binding.NativeWatcher | undefined;
#inputFileSystem: InputFileSystem;

constructor(inputFileSystem: InputFileSystem) {
this.#inputFileSystem = inputFileSystem;
}

watch(
files: Iterable<string> & {
Expand Down Expand Up @@ -95,7 +105,23 @@ export default class NativeWatchFileSystem implements WatchFileSystem {
[Array.from(directories.added!), Array.from(directories.removed!)],
[Array.from(missing.added!), Array.from(missing.removed!)],
(err: Error | null, result) => {
const { changedFiles, removedFiles } = result;
if (!err) {
// pause emitting events (avoids clearing aggregated changes and removals on timeout)
nativeWatcher.pause();
}
// !!if there is an error, result maybe a undefined value
const changedFiles = result?.changedFiles || [];
const removedFiles = result?.removedFiles || [];
if (this.#inputFileSystem?.purge) {
const fs = this.#inputFileSystem;
for (const item of changedFiles) {
fs.purge?.(item);
}
for (const item of removedFiles) {
fs.purge?.(item);
}
}

// TODO: add fileTimeInfoEntries and contextTimeInfoEntries
callback(
err,
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/node/NodeEnvironmentPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class NodeEnvironmentPlugin {
compiler.intermediateFileSystem = null;

if (compiler.options.experiments.nativeWatcher) {
compiler.watchFileSystem = new NativeWatchFileSystem();
compiler.watchFileSystem = new NativeWatchFileSystem(inputFileSystem);
} else {
compiler.watchFileSystem = new NodeWatchFileSystem(inputFileSystem);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/webpack-test/NativeWatcherTestCases.longtest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { describeCases } = require("./WatchTestCases.template");

describeCases({
name: "NativeWatcherTestCases",
experiments: {
nativeWatcher: true,
}
});
1 change: 1 addition & 0 deletions tests/webpack-test/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ module.exports = {
// "<rootDir>/WatchDetection.test.js",
// "<rootDir>/WatchSuspend.test.js",
"<rootDir>/WatchTestCases.longtest.js",
"<rootDir>/NativeWatcherTestCases.longtest.js",
// "<rootDir>/WatcherEvents.test.js",
// "<rootDir>/WebpackError.unittest.js",
// "<rootDir>/cleverMerge.unittest.js",
Expand Down
Loading