Repository navigation
Conversation
…dule When a module is required before any chunk has registered its factory, the build runtimes threw from inside the requiring module's factory and kept that error on the requiring module, so every later require rethrew it, even after the chunk with the missing factory had registered. The runtime's own factory-not-available error now carries a tag, and on that error `instantiateModule` drops the requiring module from the module cache instead of keeping the error. A later require instantiates it once the factory is there. An error a module's own code throws is still kept and rethrown without re-evaluating, as `evaluation-errors/basic` requires. Test: `execution/turbopack/runtime/missing-factory-instantiated-later` on the build runtime. Snapshots regenerated with `UPDATE=1`. <!-- NEXT_JS_LLM -->
vjeux
added a commit
to vjeux/next-turbopack-missing-module-factory
that referenced
this pull request
Oct 22, 2026
Comment on lines
+63
to
+67
| if (isFactoryNotAvailableError(error)) { | ||
| // A dependency had no factory yet: drop the module so a later require | ||
| // instantiates it once that factory's chunk has registered. An error | ||
| // the module's own code throws stays cached and is rethrown. | ||
| moduleCache.delete(id) |
Contributor
Contributor
Failing CI jobsCommit: 4331f50 | About building and testing Next.js |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
In the build runtimes (browser and Node.js), a module whose instantiation fails because a module it requires has no factory yet no longer keeps that error. A later
requireinstantiates it again once the chunk with the missing factory has registered. An error thrown by a module's own code is still kept and rethrown, asevaluation-errors/basicexpects.Why?
In an App Router production build, a dynamic
import()can throwModule <X> was instantiated because it was required from module <Y>, but the module factory is not available.The runtime then keeps that error on<Y>, so every later import of<Y>rethrows it until a reload, even after<X>'s chunk has registered. We see this in production in a large App Router app.The loader of an
import()is emitted under one module id with a different chunk list in each chunk group that embeds the importer, and the runtime installs the first copy that registers (#98099). "Any variant can be used" doesn't hold under the App Router: a copy counts on everything its own chunk group brings, but it registers as soon as its own chunk lands, while the rest of that group may still be loading. An importer instantiated from a group that has already completed then uses that copy and instantiates the target before its dependencies have registered.Repro (
[email protected], 6 source files, chunk arrival order set with Playwright): https://github.com/vjeux/next-turbopack-missing-module-factory. Both clicks fail today. With this change applied to the served runtime (--simulate-runtime-fix), the second click loads.How?
runtime-utils.ts:factoryNotAvailableError()tags the error it creates (aWeakSet), andisFactoryNotAvailableError()checks the tag. The message is unchanged.build-base.ts(browser and Node.js):instantiateModulethrows the tagged error. On a tagged error it deletes the module frommoduleCacheinstead of settingmodule.error.turbopack/runtime/missing-factory-instantiated-later(build runtime): a module required before its dependency's chunk throws the factory-not-available error, then instantiates once that chunk registers; a module whose own code throws is evaluated once and keeps rethrowing. It fails without the runtime change. Snapshots regenerated withUPDATE=1.This makes the failure recoverable but doesn't prevent the first one. Preventing it needs a chunking change so that a loader copy only counts on chunks that are loaded wherever it can run, for example by also listing its own group's chunks, which that group fetches anyway (the repro's
--simulate-loader-fixshows that output passing). Happy to follow up on that if you want that direction.Related: #98099
This PR was written by Christopher Chedeau's coding agent at his direction.