Skip to content

Commit a73d3c6

Browse files
fix(runtime): fail when a runtime module cannot be hashed (#22034)
`RuntimeModule.updateHash` hashed `err.message` when `generate()` threw, so the module's hash stopped depending on its code: the same constant on every build. That hash is the etag of the `Compilation/codeGeneration` cache entry and feeds the chunk's content hash, so a warm filesystem cache served the runtime module recorded under it however the chunk graph had changed. The throw now propagates. `Compilation._createModuleHash` already turns one into a `ModuleHashingError`, as it does for every other module; the re-hash of full-hash modules after the compilation hash exists was unguarded, and records the same error instead of aborting the compilation. Closes #22026
1 parent 9afd535 commit a73d3c6

12 files changed

Lines changed: 130 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": patch
3+
---
4+
5+
Fail the build when a runtime module's code generation throws while hashing.

‎lib/Compilation.js‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5440,11 +5440,18 @@ This prevents using hashes of each other and should be avoided.`);
54405440
chunkGraph.getChunkFullHashModulesIterable(chunk)
54415441
)) {
54425442
const moduleHash = createHash(hashFunction);
5443-
module.updateHash(moduleHash, {
5444-
chunkGraph,
5445-
runtime: chunk.runtime,
5446-
runtimeTemplate
5447-
});
5443+
try {
5444+
module.updateHash(moduleHash, {
5445+
chunkGraph,
5446+
runtime: chunk.runtime,
5447+
runtimeTemplate
5448+
});
5449+
} catch (err) {
5450+
this.errors.push(
5451+
new (getModuleHashingError())(module, /** @type {Error} */ (err))
5452+
);
5453+
continue;
5454+
}
54485455
const moduleHashDigest = moduleHash.digest(hashDigest);
54495456
const oldHash = chunkGraph.getModuleHash(module, chunk.runtime);
54505457
chunkGraph.setModuleHashes(

‎lib/RuntimeModule.js‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,16 @@ class RuntimeModule extends Module {
136136
updateHash(hash, context) {
137137
hash.update(this.name);
138138
hash.update(`${this.stage}`);
139-
try {
140-
const code =
141-
this.fullHash || this.dependentHash
142-
? // Do not use getGeneratedCode here, because i. e. compilation hash might be not
143-
// ready at this point. We will cache it later instead.
144-
this.generate()
145-
: this.getGeneratedCode();
146-
if (code !== null && code !== undefined) {
147-
hash.update(code);
148-
}
149-
} catch (err) {
150-
hash.update(/** @type {Error} */ (err).message);
139+
// A failing `generate()` propagates: hashing its message instead pins the etag
140+
// 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();
147+
if (code !== null && code !== undefined) {
148+
hash.update(code);
151149
}
152150
super.updateHash(hash, context);
153151
}

‎lib/RuntimeTemplate.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3342,7 +3342,7 @@ class RuntimeTemplate {
33423342
* The `../` path from a chunk's own asset back to the output root. Hashes are
33433343
* neutralized first: a runtime module is generated once to be hashed, before any
33443344
* hash exists, so resolving one there throws — and `RuntimeModule.updateHash`
3345-
* swallows that, pinning the module's hash to the message.
3345+
* lets that throw fail the build rather than hashing it.
33463346
* @param {Chunk} chunk the chunk whose asset holds the reference
33473347
* @param {boolean} enforceRelative whether the answer keeps a leading `./`
33483348
* @returns {string} the path back to the output root

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
exports[`errors 1`] = `
44
Array [
55
"Test exception",
6+
"Test exception",
7+
"Test exception",
68
]
79
`;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
module.exports = [
4+
[/generate of the throwing runtime module failed/],
5+
[/generate of the throwing runtime module failed/]
6+
];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should report the failure of the re-render after hashing", () => {
2+
expect(1).toBe(1);
3+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"use strict";
2+
3+
const { RuntimeModule } = require("../../../../");
4+
5+
const PLUGIN_NAME = "ThrowingFullHashRuntimeModulePlugin";
6+
7+
class ThrowingFullHashRuntimeModule extends RuntimeModule {
8+
constructor() {
9+
super("throwing full hash", RuntimeModule.STAGE_TRIGGER);
10+
this.fullHash = true;
11+
}
12+
13+
/**
14+
* Generates runtime code for this runtime module.
15+
* @returns {string} runtime code
16+
*/
17+
generate() {
18+
// The pre-hash pass has no full hash yet, so only the re-render after hashing
19+
// throws — which is the call site this case covers.
20+
if (!this.compilation || !this.compilation.fullHash) return "";
21+
throw new Error("generate of the throwing runtime module failed");
22+
}
23+
}
24+
25+
/** @type {import("../../../../").Configuration} */
26+
module.exports = {
27+
plugins: [
28+
{
29+
apply(compiler) {
30+
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
31+
compilation.hooks.additionalTreeRuntimeRequirements.tap(
32+
PLUGIN_NAME,
33+
(chunk) => {
34+
compilation.addRuntimeModule(
35+
chunk,
36+
new ThrowingFullHashRuntimeModule()
37+
);
38+
}
39+
);
40+
});
41+
}
42+
}
43+
]
44+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
module.exports = [
4+
[/generate of the throwing runtime module failed/],
5+
[/generate of the throwing runtime module failed/]
6+
];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should report the failure instead of hashing its message", () => {
2+
expect(1).toBe(1);
3+
});

0 commit comments

Comments
 (0)