Skip to content

Turbopack: a missing module factory is not cached on the requiring module - #100601

Open
vjeux wants to merge 1 commit into
vercel:canaryfrom
vjeux:vjeux/turbopack-missing-factory
Open

vjeux wants to merge 1 commit into
vercel:canaryfrom
vjeux:vjeux/turbopack-missing-factory

Conversation

@vjeux

@vjeux vjeux commented Oct 22, 2026

Copy link
Copy Markdown

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 require instantiates 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, as evaluation-errors/basic expects.

Why?

In an App Router production build, a dynamic import() can throw Module <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 (a WeakSet), and isFactoryNotAvailableError() checks the tag. The message is unchanged.
  • build-base.ts (browser and Node.js): instantiateModule throws the tagged error. On a tagged error it deletes the module from moduleCache instead of setting module.error.
  • New execution test 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 with UPDATE=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-fix shows 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.

…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
@github-actions github-actions Bot added the Turbopack Related to Turbopack with Next.js. label 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a scope-hoisted factory publishes another module's namespace before encountering a missing dependency, deleting only the requested ID leaves that namespace bound to the failed invocation even after a successful retry.

Fix on Vercel

@github-actions

Copy link
Copy Markdown
Contributor

Failing CI jobs

Commit: 4331f50 | About building and testing Next.js

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.

1 participant