Skip to content

Commit c612b19

Browse files
committed
test: pin that webpack never re-points a user's own watchFileSystem
The guard skipping the update when `watchFileSystem` was replaced never did anything: the tap only ever writes to the instance this plugin holds, so a watcher the user installed is out of reach either way. Drop it, and cover the contract against the rewrite that would break it — assigning through `compiler.watchFileSystem` instead. `fs.rmSync` needs Node.js 14.14 and the matrix goes down to 10.x, so the fixture teardown uses unlink + rmdir.
1 parent 4f0e14d commit c612b19

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎lib/node/NodeEnvironmentPlugin.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,9 @@ class NodeEnvironmentPlugin {
6666
// The watcher purges the filesystem it was constructed with, so a
6767
// replaced `inputFileSystem` would keep serving stale sources to watch.
6868
compiler.hooks.watchRun.tap(PLUGIN_NAME, (compiler) => {
69-
if (compiler.watchFileSystem === watchFileSystem) {
70-
watchFileSystem.inputFileSystem =
71-
/** @type {InputFileSystem} */
72-
(compiler.inputFileSystem);
73-
}
69+
watchFileSystem.inputFileSystem =
70+
/** @type {InputFileSystem} */
71+
(compiler.inputFileSystem);
7472
});
7573
// Symmetric to the beforeRun purge below: the stat/readdir/readFile caches
7674
// would otherwise outlive `close()` for as long as the compiler is held.

‎test/WatchInputFileSystem.test.js‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const fs = require("graceful-fs");
66
const { Volume, createFsFromVolume } = require("memfs");
77

88
const webpack = require("..");
9+
const NodeWatchFileSystem = require("../lib/node/NodeWatchFileSystem");
910
const expectNoDeprecations = require("./helpers/expectNoDeprecations");
1011

1112
expectNoDeprecations();
@@ -25,7 +26,17 @@ describe("WatchInputFileSystem", () => {
2526

2627
afterEach((done) => {
2728
setTimeout(() => {
28-
fs.rmSync(fixturePath, { recursive: true, force: true });
29+
// `fs.rmSync` needs Node.js 14.14, below the supported baseline
30+
try {
31+
fs.unlinkSync(filePath);
32+
} catch (_err) {
33+
// empty
34+
}
35+
try {
36+
fs.rmdirSync(fixturePath);
37+
} catch (_err) {
38+
// empty
39+
}
2940
done();
3041
}, 100); // cool down a bit
3142
});
@@ -198,4 +209,31 @@ describe("WatchInputFileSystem", () => {
198209
)
199210
);
200211
});
212+
213+
// Webpack only ever re-points the watcher it installed itself: plugins pair
214+
// their own filesystem with their own watcher, and that pairing must hold.
215+
it("leaves a user-supplied watchFileSystem alone", (done) => {
216+
const compiler = /** @type {Compiler} */ (webpack(config));
217+
useMemoryOutput(compiler);
218+
219+
const ownFileSystem = createInputFileSystem();
220+
const ownWatchFileSystem = new NodeWatchFileSystem(ownFileSystem);
221+
compiler.watchFileSystem = ownWatchFileSystem;
222+
compiler.inputFileSystem = createInputFileSystem();
223+
224+
let finished = false;
225+
const watching = compiler.watch(watchOptions, (err) => {
226+
if (finished) return;
227+
finished = true;
228+
if (err) return done(err);
229+
/** @type {Error | undefined} */
230+
let error;
231+
try {
232+
expect(ownWatchFileSystem.inputFileSystem).toBe(ownFileSystem);
233+
} catch (err2) {
234+
error = /** @type {Error} */ (err2);
235+
}
236+
watching.close(() => done(error));
237+
});
238+
});
201239
});

0 commit comments

Comments
 (0)