Skip to content

fix(turbopack): re-evaluate modules that failed in a require(), keep import errors cached - #101804

Open
A-RYAN-1 wants to merge 3 commits into
vercel:canaryfrom
A-RYAN-1:fix/turbopack-stack-overflow-module-cache
Open

A-RYAN-1 wants to merge 3 commits into
vercel:canaryfrom
A-RYAN-1:fix/turbopack-stack-overflow-module-cache

Conversation

@A-RYAN-1

@A-RYAN-1 A-RYAN-1 commented Nov 14, 2026 •

Copy link
Copy Markdown

What?

When a require() throws, the Turbopack runtime now removes the modules that failed with that error from the cache, so the next require() evaluates them again. import keeps a failed module's error, as ESM requires.

Why?

When a module factory throws, instantiateModule stores the error on the module (module.error) and getCachedModule rethrows it on every later load. That is right for ESM imports, and evaluation-errors/basic pins it. For require() it differs from Node.js, which removes a module that throws from require.cache, and from Next.js's own webpack build, which sets output.strictModuleExceptionHandling: true for 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 through commonJsRequire from the Flight client's __next_require__. From then on every render that uses the module fails with the same RangeError, even at depth 0, until the server restarts.

How?

commonJsRequire catches the error, calls forgetModulesFailedWith(this.c, error), which deletes every cache entry whose module.error is that error, and rethrows. require.context in require mode now goes through commonJsRequire too. The catch blocks in instantiateModule keep storing module.error inline, 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 under tests/snapshot are regenerated because they embed the runtime.

A new execution test, evaluation-errors/require, checks that a require() of a module that threw evaluates it again, that a require() of an ESM chain a -> b -> c where c overflows on its first run recovers on the next require() (all three evaluated again), and that a second import() 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/basic passes 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:

Fixes #101554

…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

vercel Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Vercel Agent finished reviewing your changes.
6f3b06a • Sun, 15 Nov 2026 02:16:54 GMT

Vercel Agent's latest review does not approve this pull request under the repository's review policy.

Resolved issues (1)

turbopack/crates/turbopack-ecmascript-runtime/js/src/nodejs/runtime/build-base.ts:56 — When module loading itself exhausts the stack, calling cacheModuleError can overflow again and leave failed modules cached without an error, so later imports silently return incomplete exports.

Review steps (1m 28s)

Preparing the workspace 14.4s
Analyzing the changes 1m 5s
Finalizing the review 8.6s

@github-actions github-actions Bot added the Turbopack Related to Turbopack with Next.js. label Nov 14, 2026
@lukesandberg

Copy link
Copy Markdown
Contributor

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.
@A-RYAN-1 A-RYAN-1 changed the title fix(turbopack): do not cache a stack overflow as a module's evaluation error fix(turbopack): re-evaluate modules that failed in a require(), keep import errors cached Nov 15, 2026
@A-RYAN-1

Copy link
Copy Markdown
Author

You're right, the first version re-evaluated imported modules. Reworked: import keeps the error exactly as before (a new test checks that the second import throws the same error object). Only require() changes: when it throws, the modules that failed with that error leave the cache, which is how Node treats require.cache and how Next's webpack build already behaves with output.strictModuleExceptionHandling: true.

The failing load in the repro is a require (commonJsRequire from the Flight client's __next_require__), so it recovers on the next render. This also removes the extra call in the catch blocks that the bot flagged. Does this split work for you?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Turbopack Related to Turbopack with Next.js.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Turbopack runtime caches a stack overflow from a module's first server load as a permanent module error

2 participants