File tree Expand file tree Collapse file tree
errors/exception-in-chunk-renderer/__snapshots__ Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ " webpack " : patch
3+ ---
4+
5+ Fail the build when a runtime module's code generation throws while hashing.
Original file line number Diff line number Diff 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 (
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 33exports [` errors 1` ] = `
44Array [
55 "Test exception",
6+ "Test exception",
7+ "Test exception",
68]
79` ;
Original file line number Diff line number Diff line change 1+ "use strict" ;
2+
3+ module . exports = [
4+ [ / g e n e r a t e o f t h e t h r o w i n g r u n t i m e m o d u l e f a i l e d / ] ,
5+ [ / g e n e r a t e o f t h e t h r o w i n g r u n t i m e m o d u l e f a i l e d / ]
6+ ] ;
Original file line number Diff line number Diff line change 1+ it ( "should report the failure of the re-render after hashing" , ( ) => {
2+ expect ( 1 ) . toBe ( 1 ) ;
3+ } ) ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 1+ "use strict" ;
2+
3+ module . exports = [
4+ [ / g e n e r a t e o f t h e t h r o w i n g r u n t i m e m o d u l e f a i l e d / ] ,
5+ [ / g e n e r a t e o f t h e t h r o w i n g r u n t i m e m o d u l e f a i l e d / ]
6+ ] ;
Original file line number Diff line number Diff line change 1+ it ( "should report the failure instead of hashing its message" , ( ) => {
2+ expect ( 1 ) . toBe ( 1 ) ;
3+ } ) ;
You can’t perform that action at this time.
0 commit comments