Skip to content

Commit b69fe0e

Browse files
test: close the compiler after a failed build so its cache does not store in the background
A compiler left open after a fatal error still stored its filesystem cache once idle, racing the next compiler on the same cache directory ("Caching failed for pack: ENOENT ... rename"). Closing it exposed a crash in ResolverCachePlugin when a resolve finishes after close, which now returns the uncached result. Claude-Session: https://claude.ai/code/session_017TkBWTk3wcyUcJYDp5ZhaP
1 parent d98cef8 commit b69fe0e

4 files changed

Lines changed: 51 additions & 10 deletions

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+
Do not throw when a compiler is closed while a cached resolve is still in progress, e.g. after a build with `bail` failed.

‎lib/cache/ResolverCachePlugin.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,20 @@ class ResolverCachePlugin {
256256
const fileDependencies = newResolveContext.fileDependencies;
257257
const contextDependencies = newResolveContext.contextDependencies;
258258
const missingDependencies = newResolveContext.missingDependencies;
259-
// Only reachable past the `!fileSystemInfo` guard on the resolve tap.
260-
const fsInfo = /** @type {FileSystemInfo} */ (fileSystemInfo);
261-
fsInfo.createSnapshot(
259+
const resolveResult = withYield ? yieldResult : result;
260+
// the compiler was closed while resolving, e.g. after a bailed build
261+
if (!fileSystemInfo) {
262+
if (withYield && result) {
263+
/** @type {ResolveRequest[]} */
264+
(yieldResult).push(result);
265+
}
266+
return callback(
267+
null,
268+
/** @type {ResolveRequest} */
269+
(resolveResult)
270+
);
271+
}
272+
fileSystemInfo.createSnapshot(
262273
resolveTime,
263274
/** @type {Dependencies} */
264275
(fileDependencies),
@@ -269,7 +280,6 @@ class ResolverCachePlugin {
269280
snapshotOptions,
270281
(err, snapshot) => {
271282
if (err) return callback(err);
272-
const resolveResult = withYield ? yieldResult : result;
273283
// since we intercept resolve hook
274284
// we still can get result in callback
275285
if (withYield && result) {
@@ -445,7 +455,11 @@ class ResolverCachePlugin {
445455
const processCacheResult = (err, cacheEntry) => {
446456
if (err) return done(err);
447457

448-
if (cacheEntry && dependsOnChangedPath(cacheEntry.snapshot)) {
458+
// the compiler was closed while reading the cache
459+
if (
460+
cacheEntry &&
461+
(!fileSystemInfo || dependsOnChangedPath(cacheEntry.snapshot))
462+
) {
449463
cacheInvalidResolves++;
450464
return doRealResolve(
451465
itemCache,

‎test/templates/ConfigTestCases.js‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,19 @@ const describeCases = (config) => {
319319
setTimeout(done, 200);
320320
};
321321

322+
/**
323+
* Closes the compiler before reporting a failed build. Left open, its
324+
* filesystem cache still stores the pack once idle, after the test is
325+
* over and at the same time as the next compiler on the same cache
326+
* directory, and one store then renames a file away from the other.
327+
* @param {import("../../").Compiler} compiler compiler that failed
328+
* @param {Error} err the fatal error
329+
* @param {(err?: Error) => void} done done callback
330+
*/
331+
const closeAfterFatalError = (compiler, err, done) => {
332+
compiler.close(() => handleFatalError(err, done));
333+
};
334+
322335
/**
323336
* Runs the compiler, reporting a synchronous throw from a hook
324337
* (e.g. a `compilation` tap) to the callback like a failed run.
@@ -350,7 +363,7 @@ const describeCases = (config) => {
350363

351364
runCompiler(compiler, (err) => {
352365
deprecationTracker();
353-
if (err) return handleFatalError(err, done);
366+
if (err) return closeAfterFatalError(compiler, err, done);
354367
// Check after close: the disk cache is stored during close,
355368
// so store failures (warnings) only surface afterwards.
356369
compiler.close((closeErr) => {
@@ -401,7 +414,11 @@ const describeCases = (config) => {
401414
runCompiler(compiler, (err, stats) => {
402415
deprecationTracker();
403416
if (err) {
404-
return handleFatalError(/** @type {Error} */ (err), done);
417+
return closeAfterFatalError(
418+
compiler,
419+
/** @type {Error} */ (err),
420+
done
421+
);
405422
}
406423
const { modules, children, errorsCount } =
407424
/** @type {import("../../").Stats} */ (stats).toJson({
@@ -690,7 +707,11 @@ const describeCases = (config) => {
690707

691708
compiler.run((err) => {
692709
if (err) {
693-
return handleFatalError(/** @type {Error} */ (err), done);
710+
return closeAfterFatalError(
711+
compiler,
712+
/** @type {Error} */ (err),
713+
done
714+
);
694715
}
695716
compiler.run((error, stats) => {
696717
compiler.close((err) => {

‎test/templates/TestCases.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ const describeCases = (config) => {
420420
compiler.run((err, _stats) => {
421421
const stats = /** @type {import("../../").Stats} */ (_stats);
422422
const deprecations = deprecationTracker();
423-
if (err) return done(err);
423+
// closed so its cache does not store after the test, see ConfigTestCases
424+
if (err) return compiler.close(() => done(err));
424425
const infrastructureLogErrors = [
425426
...filterInfraStructureErrors(infraStructureLog, {
426427
run: 3,
@@ -509,7 +510,7 @@ const describeCases = (config) => {
509510
const deprecationTracker = deprecationTracking.start();
510511
compiler.run((err) => {
511512
deprecationTracker();
512-
if (err) return done(err);
513+
if (err) return compiler.close(() => done(err));
513514
run();
514515
});
515516
} else {

0 commit comments

Comments
 (0)