Skip to content

Commit 4f0e14d

Browse files
committed
fix: follow a replaced inputFileSystem when purging in watch
NodeWatchFileSystem captures the input filesystem it is constructed with and purges that one on every change. Assigning `compiler.inputFileSystem` afterwards — as MultiCompiler's setter does for every child, and as virtual-filesystem plugins do — left the watcher purging an orphaned cache, so watch kept rebuilding from stale sources indefinitely. Re-point the watcher at the compiler's current filesystem on watchRun, and only while the watcher is still the one this plugin installed.
1 parent 788ffe0 commit 4f0e14d

3 files changed

Lines changed: 217 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Keep watch rebuilds correct when `compiler.inputFileSystem` is replaced.

‎lib/node/NodeEnvironmentPlugin.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ class NodeEnvironmentPlugin {
6161
(compiler.inputFileSystem);
6262
compiler.outputFileSystem = fs;
6363
compiler.intermediateFileSystem = fs;
64-
compiler.watchFileSystem = new NodeWatchFileSystem(inputFileSystem);
64+
const watchFileSystem = new NodeWatchFileSystem(inputFileSystem);
65+
compiler.watchFileSystem = watchFileSystem;
66+
// The watcher purges the filesystem it was constructed with, so a
67+
// replaced `inputFileSystem` would keep serving stale sources to watch.
68+
compiler.hooks.watchRun.tap(PLUGIN_NAME, (compiler) => {
69+
if (compiler.watchFileSystem === watchFileSystem) {
70+
watchFileSystem.inputFileSystem =
71+
/** @type {InputFileSystem} */
72+
(compiler.inputFileSystem);
73+
}
74+
});
6575
// Symmetric to the beforeRun purge below: the stat/readdir/readFile caches
6676
// would otherwise outlive `close()` for as long as the compiler is held.
6777
compiler.hooks.shutdown.tap(PLUGIN_NAME, () => {

‎test/WatchInputFileSystem.test.js‎

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
const { CachedInputFileSystem } = require("enhanced-resolve");
5+
const fs = require("graceful-fs");
6+
const { Volume, createFsFromVolume } = require("memfs");
7+
8+
const webpack = require("..");
9+
const expectNoDeprecations = require("./helpers/expectNoDeprecations");
10+
11+
expectNoDeprecations();
12+
13+
/** @typedef {import("../").Compiler} Compiler */
14+
/** @typedef {import("../").InputFileSystem} InputFileSystem */
15+
/** @typedef {import("memfs").IFs} IFs */
16+
17+
describe("WatchInputFileSystem", () => {
18+
const fixturePath = path.join(__dirname, "fixtures", "temp-watch-input-fs");
19+
const filePath = path.join(fixturePath, "file.js");
20+
21+
beforeEach(() => {
22+
fs.mkdirSync(fixturePath, { recursive: true });
23+
fs.writeFileSync(filePath, "module.exports = 'original';", "utf8");
24+
});
25+
26+
afterEach((done) => {
27+
setTimeout(() => {
28+
fs.rmSync(fixturePath, { recursive: true, force: true });
29+
done();
30+
}, 100); // cool down a bit
31+
});
32+
33+
if (process.env.NO_WATCH_TESTS) {
34+
// eslint-disable-next-line jest/no-disabled-tests
35+
it.skip("long running tests excluded", () => {});
36+
37+
return;
38+
}
39+
40+
/**
41+
* @param {Compiler} compiler compiler whose output is read back
42+
* @returns {IFs} the memory filesystem it writes to
43+
*/
44+
const useMemoryOutput = (compiler) => {
45+
const memfs = createFsFromVolume(new Volume());
46+
compiler.outputFileSystem =
47+
/** @type {import("../").OutputFileSystem} */
48+
(/** @type {unknown} */ (memfs));
49+
return /** @type {IFs} */ (/** @type {unknown} */ (memfs));
50+
};
51+
52+
/** @type {import("../").Configuration} */
53+
const config = {
54+
mode: "development",
55+
entry: filePath,
56+
output: { path: "/directory", filename: "bundle.js" }
57+
};
58+
59+
const watchOptions = {
60+
aggregateTimeout: 50,
61+
// Deno's node:fs.watch compat drops change events; poll for a
62+
// deterministic pickup.
63+
...(process.versions.deno ? { poll: 100 } : {})
64+
};
65+
66+
/** @typedef {{ close: (callback: () => void) => void }} Closable */
67+
68+
/**
69+
* The filesystem NodeEnvironmentPlugin builds, made again by hand.
70+
* @returns {InputFileSystem} a fresh cached input filesystem
71+
*/
72+
const createInputFileSystem = () =>
73+
/** @type {InputFileSystem} */
74+
(
75+
/** @type {unknown} */
76+
(
77+
new CachedInputFileSystem(
78+
/** @type {ConstructorParameters<typeof CachedInputFileSystem>[0]} */
79+
(/** @type {unknown} */ (fs)),
80+
60000
81+
)
82+
)
83+
);
84+
85+
/**
86+
* The handler runs before `Watching` re-arms its watcher a tick later, so
87+
* writing right away races the change against nothing watching for it.
88+
* @param {Compiler[]} compilers compilers that must be watching
89+
* @param {() => void} callback called once every one of them is
90+
* @returns {void}
91+
*/
92+
const whenWatcherReady = (compilers, callback) => {
93+
if (
94+
compilers.every(
95+
(compiler) => compiler.watching && compiler.watching.watcher
96+
)
97+
) {
98+
callback();
99+
return;
100+
}
101+
setTimeout(() => whenWatcherReady(compilers, callback), 10);
102+
};
103+
104+
/**
105+
* Drives one watch cycle: build, rewrite the entry, build again.
106+
* @param {Compiler[]} compilers compilers under test
107+
* @param {() => Closable} getWatching the watcher, read once it exists
108+
* @param {() => boolean} sawChange whether every bundle carries the new source
109+
* @param {(err?: Error) => void} done jest callback
110+
* @returns {(err: Error | null) => void} the watch handler
111+
*/
112+
const expectRebuildToSeeTheChange = (
113+
compilers,
114+
getWatching,
115+
sawChange,
116+
done
117+
) => {
118+
let builds = 0;
119+
120+
return (err) => {
121+
if (err) return done(err);
122+
builds++;
123+
if (builds === 1) {
124+
whenWatcherReady(compilers, () => {
125+
fs.writeFile(
126+
filePath,
127+
"module.exports = 'changed';",
128+
"utf8",
129+
(err2) => {
130+
if (err2) done(err2);
131+
}
132+
);
133+
});
134+
return;
135+
}
136+
if (builds > 2) return;
137+
/** @type {Error | undefined} */
138+
let error;
139+
try {
140+
expect(sawChange()).toBe(true);
141+
} catch (err2) {
142+
error = /** @type {Error} */ (err2);
143+
}
144+
getWatching().close(() => done(error));
145+
};
146+
};
147+
148+
// Replacing `inputFileSystem` used to leave the watcher purging the
149+
// filesystem it captured at construction, so watch served stale sources.
150+
it("rebuilds from a replaced inputFileSystem", (done) => {
151+
const compiler = /** @type {Compiler} */ (webpack(config));
152+
const memfs = useMemoryOutput(compiler);
153+
154+
compiler.inputFileSystem = createInputFileSystem();
155+
156+
const watching = compiler.watch(
157+
watchOptions,
158+
expectRebuildToSeeTheChange(
159+
[compiler],
160+
() => /** @type {Closable} */ (watching),
161+
() =>
162+
memfs
163+
.readFileSync("/directory/bundle.js")
164+
.toString()
165+
.includes("changed"),
166+
done
167+
)
168+
);
169+
});
170+
171+
it("rebuilds every child from a replaced inputFileSystem", (done) => {
172+
const compiler = /** @type {import("../").MultiCompiler} */ (
173+
webpack([
174+
{ ...config, name: "a" },
175+
{ ...config, name: "b" }
176+
])
177+
);
178+
const filesystems = compiler.compilers.map((child) =>
179+
useMemoryOutput(child)
180+
);
181+
182+
// The MultiCompiler setter hands one filesystem to every child.
183+
compiler.inputFileSystem = createInputFileSystem();
184+
185+
const watching = compiler.watch(
186+
watchOptions,
187+
expectRebuildToSeeTheChange(
188+
compiler.compilers,
189+
() => /** @type {Closable} */ (watching),
190+
() =>
191+
filesystems.every((memfs) =>
192+
memfs
193+
.readFileSync("/directory/bundle.js")
194+
.toString()
195+
.includes("changed")
196+
),
197+
done
198+
)
199+
);
200+
});
201+
});

0 commit comments

Comments
 (0)