Skip to content

Commit bb2680f

Browse files
alexander-akaitOskarEichlerclaude
authored
fix: watch future glob matches without mutating options (#5800)
* fix: watch future glob matches without mutating options Co-authored-by: Oskar Eichler <62393985+OskarEichler@users.noreply.github.com> * fix: batch glob rescans and defer pre-ready unwatch in watchFiles Paths no glob matched are now re-checked in one rescan per burst of adds instead of one synchronous rescan each, and adds reported before ready wait for the rescan that follows the initial scan, so they are no longer unwatched against stale matches. Tests use the public ready event. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013FhutSnStkMpV2mZSNb8M9 * fix: do not unwatch pending paths that were removed unwatch() keeps a path in chokidar's ignored set, so a matching file removed before the batch re-check would stay ignored once recreated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013FhutSnStkMpV2mZSNb8M9 --------- Co-authored-by: Oskar Eichler <62393985+OskarEichler@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7068046 commit bb2680f

3 files changed

Lines changed: 449 additions & 19 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack-dev-server": patch
3+
---
4+
5+
Watch files created after startup that match a `watchFiles` glob, match ignored globs against them too, and stop mutating the passed watch options.

‎lib/Server.js‎

Lines changed: 140 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4130,38 +4130,159 @@ class Server {
41304130
const isWin = path.sep === "\\";
41314131
const toPosix = (/** @type {string} */ filePath) =>
41324132
isWin ? filePath.split(path.sep).join("/") : filePath;
4133-
const toNative = (/** @type {string} */ filePath) =>
4134-
isWin ? filePath.split("/").join(path.sep) : filePath;
41354133

41364134
const cwd = watchOptions.cwd ? toPosix(watchOptions.cwd) : undefined;
4137-
4138-
const expand = (/** @type {string} */ item) => {
4135+
const toAbsolute = (/** @type {string} */ filePath) =>
4136+
path.resolve(watchOptions.cwd || process.cwd(), filePath);
4137+
4138+
// Globs are watched through their static parent directory, so files that
4139+
// only appear later are seen too; events are then filtered by the globs.
4140+
/** @type {string[]} */
4141+
const roots = [];
4142+
/** @type {string[]} */
4143+
const patterns = [];
4144+
/** @type {string[]} */
4145+
const literals = [];
4146+
4147+
for (const item of Array.isArray(watchPath) ? watchPath : [watchPath]) {
41394148
const posix = toPosix(item);
4140-
return isDynamicPattern(posix)
4141-
? globSync(posix, { cwd, absolute: true }).map(toNative)
4142-
: item;
4143-
};
41444149

4145-
const resolveGlobs = (/** @type {string | string[]} */ input) =>
4146-
(Array.isArray(input) ? input : [input]).flatMap((item) =>
4147-
typeof item === "string" ? expand(item) : item,
4150+
if (!isDynamicPattern(posix)) {
4151+
roots.push(item);
4152+
literals.push(toAbsolute(item));
4153+
continue;
4154+
}
4155+
4156+
patterns.push(posix);
4157+
4158+
if (!posix.startsWith("!")) {
4159+
const segments = posix.split("/");
4160+
const base = segments
4161+
.slice(
4162+
0,
4163+
segments.findIndex((segment) => isDynamicPattern(segment)),
4164+
)
4165+
.join("/");
4166+
4167+
roots.push(toAbsolute(base || (posix.startsWith("/") ? "/" : ".")));
4168+
}
4169+
}
4170+
4171+
/** @type {string[]} */
4172+
const ignoredPatterns = [];
4173+
/** @type {import("chokidar").Matcher[]} */
4174+
const ignoredRest = [];
4175+
4176+
for (const item of /** @type {import("chokidar").Matcher[]} */ (
4177+
[watchOptions.ignored ?? []].flat()
4178+
)) {
4179+
if (typeof item === "string" && isDynamicPattern(toPosix(item))) {
4180+
ignoredPatterns.push(toPosix(item));
4181+
} else {
4182+
ignoredRest.push(item);
4183+
}
4184+
}
4185+
4186+
/**
4187+
* @param {string[]} globs globs
4188+
* @returns {Set<string>} matched absolute paths
4189+
*/
4190+
const expand = (globs) =>
4191+
new Set(
4192+
globs.length > 0
4193+
? globSync(globs, { cwd, absolute: true, onlyFiles: false }).map(
4194+
(filePath) => path.resolve(filePath),
4195+
)
4196+
: [],
41484197
);
41494198

4150-
const resolvedPaths = resolveGlobs(watchPath);
4199+
let matchedPaths = expand(patterns);
4200+
let ignoredPaths = expand(ignoredPatterns);
4201+
4202+
// Work on a copy, the passed options must stay as they are.
4203+
const options =
4204+
ignoredPatterns.length > 0
4205+
? {
4206+
...watchOptions,
4207+
ignored: [
4208+
...ignoredRest,
4209+
(/** @type {string} */ filePath) =>
4210+
ignoredPaths.has(toAbsolute(filePath)),
4211+
],
4212+
}
4213+
: watchOptions;
4214+
4215+
const isWatched = (/** @type {string} */ item) => {
4216+
const filePath = toAbsolute(item);
41514217

4152-
if (typeof watchOptions.ignored === "string") {
4153-
watchOptions.ignored = resolveGlobs(watchOptions.ignored);
4154-
} else if (Array.isArray(watchOptions.ignored)) {
4155-
watchOptions.ignored = watchOptions.ignored.flatMap((item) =>
4156-
typeof item === "string" ? expand(item) : item,
4218+
return (
4219+
!ignoredPaths.has(filePath) &&
4220+
(matchedPaths.has(filePath) ||
4221+
literals.some(
4222+
(literal) =>
4223+
filePath === literal || filePath.startsWith(literal + path.sep),
4224+
))
41574225
);
4158-
}
4226+
};
4227+
4228+
const watcher = chokidar.watch([...new Set(roots)], options);
4229+
const refresh = () => {
4230+
matchedPaths = expand(patterns);
4231+
ignoredPaths = expand(ignoredPatterns);
4232+
};
4233+
let ready = false;
4234+
let flushScheduled = false;
4235+
// Paths no glob matched when they were added. They are checked again in
4236+
// one batch, so a burst of new files costs a single rescan, and adds
4237+
// seen before `ready` wait for the rescan that follows the initial scan.
4238+
/** @type {Set<string>} */
4239+
const pendingPaths = new Set();
4240+
const flush = () => {
4241+
flushScheduled = false;
4242+
refresh();
4243+
4244+
for (const item of pendingPaths) {
4245+
// Let chokidar stop watching files that no glob asked for. Skip removed
4246+
// files: chokidar already dropped them, and `unwatch()` would keep
4247+
// ignoring the path if it is created again.
4248+
if (!isWatched(item) && fs.existsSync(toAbsolute(item))) {
4249+
watcher.unwatch(item);
4250+
}
4251+
}
4252+
4253+
pendingPaths.clear();
4254+
};
4255+
4256+
watcher.on("ready", () => {
4257+
ready = true;
4258+
flush();
4259+
});
4260+
const hasGlobs = patterns.length > 0 || ignoredPatterns.length > 0;
4261+
4262+
watcher.on("add", (item) => {
4263+
// Without globs there is nothing to match, and a path a glob already
4264+
// matched stays watched.
4265+
if (!hasGlobs || matchedPaths.has(toAbsolute(item))) {
4266+
return;
4267+
}
4268+
4269+
pendingPaths.add(item);
41594270

4160-
const watcher = chokidar.watch(resolvedPaths, watchOptions);
4271+
if (ready && !flushScheduled) {
4272+
flushScheduled = true;
4273+
// Wait until chokidar has finished setting up the watcher for the
4274+
// burst of new files.
4275+
setImmediate(flush);
4276+
}
4277+
});
41614278

41624279
// disabling refreshing on changing the content
41634280
if (this.options.liveReload) {
41644281
watcher.on("change", (item) => {
4282+
if (!isWatched(item)) {
4283+
return;
4284+
}
4285+
41654286
// A change no compilation knows about, which is what the middleware's
41664287
// `reload` action is for — it loads the page again whatever the build
41674288
// settings say, since there is no update to apply.

0 commit comments

Comments
 (0)