Skip to content

Commit 64d800f

Browse files
fix: name the runtime module whose generation fails while hashing
A runtime module is generated once before any chunk hash exists, so a `generate()` resolving its chunk's filename template throws there. Since 5.111.0 that throw fails the build, and the message named only the filename template, pointing at the user's `output.filename` rather than at the plugin. The failure now names the runtime module and its class, and, for a path variable the hashing pass has no value for, says to ask `runtimeTemplate.chunkRootOutputDir` instead. Refs #22199 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01EAcvnxxuoLV22VqcY51yCg
1 parent 4ffacc7 commit 64d800f

14 files changed

Lines changed: 240 additions & 12 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+
Name the runtime module whose generation fails while hashing.

‎lib/Compilation.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5153,8 +5153,12 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
51535153
});
51545154
moduleHashDigest = moduleHash.digest(hashDigest);
51555155
} catch (err) {
5156+
// A diagnostic that already names its module says more than this wrapper
5157+
// would, e.g. which runtime module's `generate()` threw.
51565158
errors.push(
5157-
new (getModuleHashingError())(module, /** @type {Error} */ (err))
5159+
err instanceof WebpackError && err.module
5160+
? err
5161+
: new (getModuleHashingError())(module, /** @type {Error} */ (err))
51585162
);
51595163
moduleHashDigest = "XXXXXX";
51605164
}

‎lib/errors/PathVariableError.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Alexander Akait @alexander-akait
4+
*/
5+
6+
"use strict";
7+
8+
const WebpackError = require("./WebpackError");
9+
10+
class PathVariableError extends WebpackError {
11+
/**
12+
* Creates an instance of PathVariableError.
13+
* @param {string} variable the placeholder that has no value here, e.g. `[contenthash:8]`
14+
* @param {string} template the path template the placeholder appears in
15+
*/
16+
constructor(variable, template) {
17+
super(
18+
`Path variable ${variable} not implemented in this context: ${template}`
19+
);
20+
21+
/** @type {string} */
22+
this.name = "PathVariableError";
23+
/** @type {string} */
24+
this.variable = variable;
25+
/** @type {string} */
26+
this.template = template;
27+
}
28+
}
29+
30+
/** @type {typeof PathVariableError} */
31+
module.exports = PathVariableError;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Alexander Akait @alexander-akait
4+
*/
5+
6+
"use strict";
7+
8+
const PathVariableError = require("./PathVariableError");
9+
const WebpackError = require("./WebpackError");
10+
const deriveStackFromNestedError = require("./deriveStackFromNestedError");
11+
12+
/** @import RuntimeModule from "../runtime/RuntimeModule" */
13+
14+
/**
15+
* Names the runtime module that failed and, for a placeholder that only the
16+
* hashing pass lacks a value for, what to ask instead.
17+
* @param {RuntimeModule} module the runtime module whose `generate()` threw
18+
* @param {Error} error the error it threw
19+
* @returns {string} the message
20+
*/
21+
const buildMessage = (module, error) => {
22+
const message = `Generating the runtime module ${module.identifier()} (${
23+
module.constructor.name
24+
}) for hashing failed: ${error.message}`;
25+
if (!(error instanceof PathVariableError)) return message;
26+
return `${message}
27+
A runtime module is generated to be hashed, before the chunk it belongs to has a hash, so ${error.variable} has no value there. Where the path from the chunk's own asset back to the output root is what is needed, ask compilation.runtimeTemplate.chunkRootOutputDir(chunk, enforceRelative), which neutralizes the hash placeholders first.`;
28+
};
29+
30+
class RuntimeModuleGenerationError extends WebpackError {
31+
/**
32+
* Creates an instance of RuntimeModuleGenerationError.
33+
* @param {RuntimeModule} module the runtime module whose `generate()` threw
34+
* @param {Error} error the error it threw
35+
*/
36+
constructor(module, error) {
37+
super(buildMessage(module, error));
38+
39+
/** @type {string} */
40+
this.name = "RuntimeModuleGenerationError";
41+
/** @type {Error} */
42+
this.error = error;
43+
/** @type {RuntimeModule} */
44+
this.module = module;
45+
46+
deriveStackFromNestedError(this, error);
47+
}
48+
}
49+
50+
/** @type {typeof RuntimeModuleGenerationError} */
51+
module.exports = RuntimeModuleGenerationError;

‎lib/runtime/RuntimeModule.js‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,20 @@ class RuntimeModule extends Module {
138138
hash.update(`${this.stage}`);
139139
// A failing `generate()` propagates: hashing its message instead pins the etag
140140
// of the code generation cache to a constant, which then serves stale code.
141-
const code =
142-
this.fullHash || this.dependentHash
143-
? // Do not use getGeneratedCode here, because i. e. compilation hash might be not
144-
// ready at this point. We will cache it later instead.
145-
this.generate()
146-
: this.getGeneratedCode();
141+
/** @type {string | null} */
142+
let code;
143+
try {
144+
code =
145+
this.fullHash || this.dependentHash
146+
? // Do not use getGeneratedCode here, because i. e. compilation hash might be not
147+
// ready at this point. We will cache it later instead.
148+
this.generate()
149+
: this.getGeneratedCode();
150+
} catch (err) {
151+
const RuntimeModuleGenerationError = require("../errors/RuntimeModuleGenerationError");
152+
153+
throw new RuntimeModuleGenerationError(this, /** @type {Error} */ (err));
154+
}
147155
if (code !== null && code !== undefined) {
148156
hash.update(code);
149157
}

‎lib/template/TemplatedPathPlugin.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { basename, extname, posix } = require("path");
99
const util = require("util");
1010
const Chunk = require("../Chunk");
1111
const Module = require("../Module");
12+
const PathVariableError = require("../errors/PathVariableError");
1213
const {
1314
decode: decodeBase,
1415
encode: encodeBase
@@ -330,9 +331,7 @@ const replacer = (value, allowEmpty) => {
330331
}
331332
if (value === null || value === undefined) {
332333
if (!allowEmpty) {
333-
throw new Error(
334-
`Path variable ${match} not implemented in this context: ${input}`
335-
);
334+
throw new PathVariableError(match, input);
336335
}
337336

338337
return "";

‎test/configCases/errors/exception-in-chunk-renderer/__snapshots__/errors.snap‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
exports[`errors 1`] = `
44
Array [
5-
"Test exception",
6-
"Test exception",
5+
"Generating the runtime module webpack/runtime/compat (CompatRuntimeModule) for hashing failed: Test exception",
6+
"Generating the runtime module webpack/runtime/compat (CompatRuntimeModule) for hashing failed: Test exception",
77
"Test exception",
88
]
99
`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
module.exports = [
4+
{
5+
moduleName: /index\.js$/,
6+
message: /updateHash of the module failed/
7+
}
8+
];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should report a module whose hashing failed", () => {
2+
expect(1).toBe(1);
3+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
3+
const PLUGIN_NAME = "BreakModuleHashingPlugin";
4+
5+
/** @type {import("../../../../").Configuration} */
6+
module.exports = {
7+
optimization: {
8+
// the module still generates code, so the asset is worth looking at
9+
emitOnErrors: true
10+
},
11+
plugins: [
12+
{
13+
apply(compiler) {
14+
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
15+
compilation.hooks.finishModules.tap(PLUGIN_NAME, (modules) => {
16+
for (const module of modules) {
17+
module.updateHash = () => {
18+
throw new Error("updateHash of the module failed");
19+
};
20+
}
21+
});
22+
});
23+
}
24+
}
25+
]
26+
};

0 commit comments

Comments
 (0)