Repository navigation
Conversation
…n error When a module factory throws, the runtime stores the error on the module and rethrows it on every later load without evaluating the module again. For a RangeError from running out of stack that is wrong: it depends on how deep the stack was when the module was first loaded, not on the module, so one deep render made the module fail for every later render until the server restarted. Drop the module from the cache for that error so the next load evaluates it again. Other errors are cached as before. The change is in a shared helper used by the Node.js build runtime, the dev runtime and the browser build runtime, which also recognizes the error by name so it works across realms and for Firefox's wording. Fixes vercel#101554
|
Vercel Agent finished reviewing your changes. Vercel Agent's latest review does not approve this pull request under the repository's review policy. Resolved issues (1)
Review steps (1m 28s)Preparing the workspace 14.4s |
|
This breaks ESM semantics |
The previous commit stopped caching a stack overflow as a module's evaluation error for every kind of load, which breaks ESM semantics: an imported module that failed must keep its error. Keep the error for import, and apply CommonJS rules at the require() boundary instead: when commonJsRequire throws, every module that failed with that error is removed from the cache, so the next require() evaluates them again. Node.js does this for require.cache, and Next.js sets output.strictModuleExceptionHandling for webpack to get the same behaviour. The catch blocks go back to storing module.error inline, so a second stack overflow there can no longer leave a failed module cached without its error. require.context in require mode goes through the same path. Tests: require re-evaluates a module that threw, a require of an ESM chain that overflowed recovers on the next require, and import keeps the same error object.
|
You're right, the first version re-evaluated imported modules. Reworked: The failing load in the repro is a |
What?
When a
require()throws, the Turbopack runtime now removes the modules that failed with that error from the cache, so the nextrequire()evaluates them again.importkeeps a failed module's error, as ESM requires.Why?
When a module factory throws,
instantiateModulestores the error on the module (module.error) andgetCachedModulerethrows it on every later load. That is right for ESM imports, andevaluation-errors/basicpins it. Forrequire()it differs from Node.js, which removes a module that throws fromrequire.cache, and from Next.js's own webpack build, which setsoutput.strictModuleExceptionHandling: truefor the same behaviour. In the repro from the issue, one deep render runs out of stack inside the first load of a client module, loaded throughcommonJsRequirefrom the Flight client's__next_require__. From then on every render that uses the module fails with the sameRangeError, even at depth 0, until the server restarts.How?
commonJsRequirecatches the error, callsforgetModulesFailedWith(this.c, error), which deletes every cache entry whosemodule.erroris that error, and rethrows.require.contextin require mode now goes throughcommonJsRequiretoo. The catch blocks ininstantiateModulekeep storingmodule.errorinline, so a module is always either cached with its error or removed, even if the stack is exhausted again while the error is handled. The runtime snapshots undertests/snapshotare regenerated because they embed the runtime.A new execution test,
evaluation-errors/require, checks that arequire()of a module that threw evaluates it again, that arequire()of an ESM chaina -> b -> cwherecoverflows on its first run recovers on the nextrequire()(all three evaluated again), and that a secondimport()of a module that overflowed throws the same error object without evaluating it again. The first two fail on the base runtime and pass with the change;evaluation-errors/basicpasses in both. I ran them on GitHub Actions with the pinned nightly toolchain, since the Turbopack tests do not build on my machine.Open points:
importand later loaded withrequire()is evaluated again by thatrequire(), as webpack does withstrictModuleExceptionHandling.Fixes #101554