Skip to content

Commit 4f3f215

Browse files
fix(runtime): throw when runtime module code generation fails in updateHash
Closes #22026
1 parent d4b2f44 commit 4f3f215

7 files changed

Lines changed: 182 additions & 30 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+
Throw when runtime module code generation fails in updateHash.

‎lib/Compilation.js‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5440,24 +5440,32 @@ 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-
});
5448-
const moduleHashDigest = moduleHash.digest(hashDigest);
5449-
const oldHash = chunkGraph.getModuleHash(module, chunk.runtime);
5450-
chunkGraph.setModuleHashes(
5451-
module,
5452-
chunk.runtime,
5453-
moduleHashDigest,
5454-
moduleHashDigest.slice(0, hashDigestLength)
5455-
);
5456-
/** @type {CodeGenerationJob} */
5457-
(
5458-
/** @type {Map<Module, CodeGenerationJob>} */
5459-
(codeGenerationJobsMap.get(oldHash)).get(module)
5460-
).hash = moduleHashDigest;
5443+
try {
5444+
module.updateHash(moduleHash, {
5445+
chunkGraph,
5446+
runtime: chunk.runtime,
5447+
runtimeTemplate
5448+
});
5449+
const moduleHashDigest = moduleHash.digest(hashDigest);
5450+
const oldHash = chunkGraph.getModuleHash(module, chunk.runtime);
5451+
chunkGraph.setModuleHashes(
5452+
module,
5453+
chunk.runtime,
5454+
moduleHashDigest,
5455+
moduleHashDigest.slice(0, hashDigestLength)
5456+
);
5457+
const jobs = codeGenerationJobsMap.get(oldHash);
5458+
if (jobs) {
5459+
const job = jobs.get(module);
5460+
if (job) {
5461+
job.hash = moduleHashDigest;
5462+
}
5463+
}
5464+
} catch (err) {
5465+
this.errors.push(
5466+
new (getModuleHashingError())(module, /** @type {Error} */ (err))
5467+
);
5468+
}
54615469
}
54625470
const chunkHash = createHash(hashFunction);
54635471
chunkHash.update(/** @type {string} */ (chunk.hash));

‎lib/RuntimeModule.js‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,14 @@ 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+
const code =
140+
this.fullHash || this.dependentHash
141+
? // Do not use getGeneratedCode here, because i. e. compilation hash might be not
142+
// ready at this point. We will cache it later instead.
143+
this.generate()
144+
: this.getGeneratedCode();
145+
if (code !== null && code !== undefined) {
146+
hash.update(code);
151147
}
152148
super.updateHash(hash, context);
153149
}

‎test/RuntimeModule.unittest.js‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"use strict";
2+
3+
const RuntimeModule = require("../lib/RuntimeModule");
4+
const createHash = require("../lib/util/createHash");
5+
6+
describe("RuntimeModule", () => {
7+
class TestRuntimeModule extends RuntimeModule {
8+
constructor(name, stage) {
9+
super(name, stage);
10+
this.code = "var test = 1;";
11+
}
12+
13+
generate() {
14+
return this.code;
15+
}
16+
}
17+
18+
it("should update hash with name, stage and generated code", () => {
19+
const m = new TestRuntimeModule("test", 10);
20+
const hash = createHash("md4");
21+
m.updateHash(hash, {
22+
chunkGraph: {
23+
getModuleGraphHash: () => "graph-hash"
24+
}
25+
});
26+
27+
const expectedHash = createHash("md4");
28+
expectedHash.update("test");
29+
expectedHash.update("10");
30+
expectedHash.update("var test = 1;");
31+
expectedHash.update("graph-hash");
32+
33+
expect(hash.digest("hex")).toBe(expectedHash.digest("hex"));
34+
});
35+
36+
it("should cache generated code during updateHash when not fullHash or dependentHash", () => {
37+
const m = new TestRuntimeModule("test", 0);
38+
expect(m._cachedGeneratedCode).toBeUndefined();
39+
40+
const hash = createHash("md4");
41+
m.updateHash(hash, {
42+
chunkGraph: {
43+
getModuleGraphHash: () => ""
44+
}
45+
});
46+
47+
expect(m._cachedGeneratedCode).toBe("var test = 1;");
48+
expect(m.getGeneratedCode()).toBe("var test = 1;");
49+
});
50+
51+
it("should not cache generated code during updateHash when fullHash is true", () => {
52+
const m = new TestRuntimeModule("test", 0);
53+
m.fullHash = true;
54+
expect(m._cachedGeneratedCode).toBeUndefined();
55+
56+
const hash = createHash("md4");
57+
m.updateHash(hash, {
58+
chunkGraph: {
59+
getModuleGraphHash: () => ""
60+
}
61+
});
62+
63+
expect(m._cachedGeneratedCode).toBeUndefined();
64+
});
65+
66+
it("should not cache generated code during updateHash when dependentHash is true", () => {
67+
const m = new TestRuntimeModule("test", 0);
68+
m.dependentHash = true;
69+
expect(m._cachedGeneratedCode).toBeUndefined();
70+
71+
const hash = createHash("md4");
72+
m.updateHash(hash, {
73+
chunkGraph: {
74+
getModuleGraphHash: () => ""
75+
}
76+
});
77+
78+
expect(m._cachedGeneratedCode).toBeUndefined();
79+
});
80+
81+
it("should throw when generate() throws during updateHash instead of swallowing error", () => {
82+
class ThrowingRuntimeModule extends RuntimeModule {
83+
constructor() {
84+
super("throwing");
85+
}
86+
87+
generate() {
88+
throw new Error("failed to generate code");
89+
}
90+
}
91+
92+
const m = new ThrowingRuntimeModule();
93+
const hash = createHash("md4");
94+
95+
expect(() => {
96+
m.updateHash(hash, {
97+
chunkGraph: {
98+
getModuleGraphHash: () => ""
99+
}
100+
});
101+
}).toThrow("failed to generate code");
102+
});
103+
});
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+
[/error thrown in runtime module generate during hashing/],
5+
[/error thrown in runtime module generate during hashing/]
6+
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
it("should fail compilation when runtime module throws during hashing", () => {});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
3+
const { RuntimeModule } = require("../../../../");
4+
5+
const PLUGIN_NAME = "RuntimeModuleUpdateHashThrowPlugin";
6+
7+
class FailingRuntimeModule extends RuntimeModule {
8+
constructor() {
9+
super("failing runtime module");
10+
}
11+
12+
generate() {
13+
throw new Error("error thrown in runtime module generate during hashing");
14+
}
15+
}
16+
17+
/** @type {import("../../../../types").Configuration} */
18+
module.exports = {
19+
plugins: [
20+
{
21+
apply(compiler) {
22+
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
23+
compilation.hooks.additionalTreeRuntimeRequirements.tap(
24+
PLUGIN_NAME,
25+
(chunk) => {
26+
compilation.addRuntimeModule(chunk, new FailingRuntimeModule());
27+
}
28+
);
29+
});
30+
}
31+
}
32+
]
33+
};

0 commit comments

Comments
 (0)