Skip to content

Commit 3b08f15

Browse files
fix: only exclude an output path nested inside the watched directory
Matching a filesystem-root output path correctly turned out to be worse than matching it not at all. `output.path` defaults to `/` under an in-memory filesystem and is routinely left there — test/fixtures/client-config does exactly that — and `/` contains every static directory there is, so the watcher stopped reporting anything. CI caught it: "should work and log static changes" waited 120s for a reload that no longer came. Exclude an output path only where it is strictly inside the directory being watched, which is the case the change is for: a build emitting into a folder that is also served statically. An output path equal to or containing that folder is left alone, because the user pointed static at it deliberately. The matcher is now built per watched directory rather than once for the server, and the new regression test fails against the previous version.
1 parent 7284aba commit 3b08f15

4 files changed

Lines changed: 94 additions & 27 deletions

File tree

‎lib/Server.js‎

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,10 +1962,10 @@ class Server {
19621962
const watchFiles = /** @type {NormalizedStatic[]} */ (this.options.static);
19631963

19641964
if (watchFiles.length > 0) {
1965-
const ignoreOutputPath = this.getOutputPathMatcher();
1966-
19671965
for (const item of watchFiles) {
19681966
if (item.watch) {
1967+
const ignoreOutputPath = this.getOutputPathMatcher(item.directory);
1968+
19691969
await this.watchFiles(item.directory, {
19701970
...item.watch,
19711971
ignored: [
@@ -1974,7 +1974,7 @@ class Server {
19741974
: Array.isArray(item.watch.ignored)
19751975
? item.watch.ignored
19761976
: [item.watch.ignored]),
1977-
ignoreOutputPath,
1977+
...(ignoreOutputPath ? [ignoreOutputPath] : []),
19781978
],
19791979
});
19801980
}
@@ -1983,34 +1983,49 @@ class Server {
19831983
}
19841984

19851985
/**
1986-
* A static directory often contains the compiler's own output, either because
1987-
* `output.path` sits inside it or because `writeToDisk` puts it there. Every
1986+
* A static directory often contains the compiler's own output, because
1987+
* `output.path` sits inside it and `writeToDisk` puts the build there. Every
19881988
* build then rewrites those files, and watching them turns one compilation
19891989
* into a reload — or, when a plugin copies them back in, into an endless one.
1990-
* The compilation already reaches the client through the middleware, so the
1991-
* output directory is never worth watching.
1990+
* The compilation already reaches the client through the middleware, so an
1991+
* output directory nested in a watched one is never worth watching.
1992+
*
1993+
* Only a strictly nested output path is excluded. `output.path` defaults to
1994+
* `/` under an in-memory filesystem and is routinely left there, and that
1995+
* path contains every static directory there is: treating it as output would
1996+
* silently stop watching all of them.
19921997
* @private
1993-
* @returns {(targetPath: string) => boolean} true when a path is the output directory or inside it
1998+
* @param {string} directory the static directory being watched
1999+
* @returns {((targetPath: string) => boolean) | undefined} a matcher for paths inside a nested output directory, or nothing to exclude
19942000
*/
1995-
getOutputPathMatcher() {
2001+
getOutputPathMatcher(directory) {
19962002
const compilers =
19972003
/** @type {MultiCompiler} */
19982004
(this.compiler).compilers || [this.compiler];
1999-
const outputPaths = compilers
2005+
// a path already ending in the separator is a filesystem root, and
2006+
// appending a second one would match nothing below it
2007+
const asPrefix = (/** @type {string} */ aPath) =>
2008+
aPath.endsWith(path.sep) ? aPath : aPath + path.sep;
2009+
const directoryPrefix = asPrefix(directory);
2010+
const prefixes = compilers
20002011
.map((compiler) => compiler.outputPath)
2001-
.filter(Boolean);
2012+
.filter(
2013+
(outputPath) =>
2014+
outputPath &&
2015+
outputPath !== directory &&
2016+
outputPath.startsWith(directoryPrefix),
2017+
)
2018+
.map((outputPath) => asPrefix(outputPath));
20022019

2003-
// an output path already ending in the separator is a filesystem root, and
2004-
// appending a second one would match nothing below it
2005-
const prefixes = outputPaths.map((outputPath) =>
2006-
outputPath.endsWith(path.sep) ? outputPath : outputPath + path.sep,
2007-
);
2020+
if (prefixes.length === 0) {
2021+
return undefined;
2022+
}
20082023

20092024
return (targetPath) =>
2010-
outputPaths.some((outputPath, index) =>
2011-
targetPath === outputPath
2012-
? true
2013-
: targetPath.startsWith(prefixes[index]),
2025+
prefixes.some(
2026+
(prefix) =>
2027+
targetPath === prefix.slice(0, -path.sep.length) ||
2028+
targetPath.startsWith(prefix),
20142029
);
20152030
}
20162031

‎test/ports-map.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const listOfTests = {
8181
"api-plugin": 1,
8282
"api-plugin-multi": 2,
8383
"harness-server-cleanup": 1,
84-
"static-watch-output-path": 1,
84+
"static-watch-output-path": 2,
8585
};
8686

8787
let startPort = 8089;

‎test/server/static-watch-output-path.test.js‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import webpack from "webpack";
88
import Server from "../../lib/Server.js";
99
import portsMap from "../ports-map.js";
1010

11-
const port = portsMap["static-watch-output-path"];
11+
const [port, rootOutputPort] = portsMap["static-watch-output-path"];
1212

1313
// The ignored file is rewritten this many times, this far apart, so that the
1414
// window spans several seconds. A single write could land while chokidar is
@@ -156,6 +156,52 @@ describe("static watching and output.path", () => {
156156
expect(reloads).toHaveLength(0);
157157
});
158158

159+
it("should keep watching when output.path contains the static directory", async () => {
160+
// `output.path` defaults to `/` under an in-memory filesystem and is
161+
// routinely left there, and every static directory is inside it. Treating
162+
// that as output would stop watching everything.
163+
const { root } = path.parse(tempDirectory);
164+
const rootOutputCompiler = webpack({
165+
mode: "development",
166+
context: tempDirectory,
167+
entry: "./entry.js",
168+
output: { path: root },
169+
infrastructureLogging: { level: "none" },
170+
stats: "none",
171+
});
172+
const rootOutputServer = new Server(
173+
{
174+
static: { directory: tempDirectory, watch: true },
175+
port: rootOutputPort,
176+
},
177+
rootOutputCompiler,
178+
);
179+
180+
/** @type {string[]} */
181+
const rootOutputReloads = [];
182+
const sendMessage = rootOutputServer.sendMessage.bind(rootOutputServer);
183+
184+
rootOutputServer.sendMessage = (clients, type, data) => {
185+
if (type === "static-changed") {
186+
rootOutputReloads.push(String(data));
187+
}
188+
189+
return sendMessage(clients, type, data);
190+
};
191+
192+
await rootOutputServer.start();
193+
194+
try {
195+
await waitUntilWatching(rootOutputReloads, outsideOutput);
196+
197+
expect(rootOutputReloads).toContain(outsideOutput);
198+
} finally {
199+
await rootOutputServer.stop();
200+
// the first server watches the same directory, so it saw those writes too
201+
reloads.length = 0;
202+
}
203+
});
204+
159205
it("should still reload for the rest of the static directory", async () => {
160206
await fsPromises.writeFile(outsideOutput, "asset again");
161207
await waitForReload(reloads, outsideOutput);

‎types/lib/Server.d.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,14 +1637,20 @@ declare class Server<
16371637
*/
16381638
private setupWatchStaticFiles;
16391639
/**
1640-
* A static directory often contains the compiler's own output, either because
1641-
* `output.path` sits inside it or because `writeToDisk` puts it there. Every
1640+
* A static directory often contains the compiler's own output, because
1641+
* `output.path` sits inside it and `writeToDisk` puts the build there. Every
16421642
* build then rewrites those files, and watching them turns one compilation
16431643
* into a reload — or, when a plugin copies them back in, into an endless one.
1644-
* The compilation already reaches the client through the middleware, so the
1645-
* output directory is never worth watching.
1644+
* The compilation already reaches the client through the middleware, so an
1645+
* output directory nested in a watched one is never worth watching.
1646+
*
1647+
* Only a strictly nested output path is excluded. `output.path` defaults to
1648+
* `/` under an in-memory filesystem and is routinely left there, and that
1649+
* path contains every static directory there is: treating it as output would
1650+
* silently stop watching all of them.
16461651
* @private
1647-
* @returns {(targetPath: string) => boolean} true when a path is the output directory or inside it
1652+
* @param {string} directory the static directory being watched
1653+
* @returns {((targetPath: string) => boolean) | undefined} a matcher for paths inside a nested output directory, or nothing to exclude
16481654
*/
16491655
private getOutputPathMatcher;
16501656
/**

0 commit comments

Comments
 (0)