Repository navigation
[turbopack] Only ship top-level async support in the runtime when needed - #94376
Merged
Merged
Conversation
Contributor
Stats skippedCommit: a29c5d1 |
Contributor
Tests PassedCommit: a29c5d1 |
lukesandberg
approved these changes
Jun 3, 2026
…ntime/async-module.ts Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Closed
1 task done
lukesandberg
added a commit
that referenced
this pull request
Aug 4, 2026
The runtime chunk is emitted to a fixed path, so every module graph using a chunking context writes the same `[turbopack]_runtime.js`. Since #94376 the async-module (top-level await) machinery is omitted when a graph has no async modules, but that decision was made per-graph. Contexts shared by several independent graphs therefore produce two different runtimes for one path. The Next.js node execution context is shared by every build-time JS evaluation (postcss configs, webpack loaders, next/font/google), each with its own graph. A graph without async modules would emit a runtime without `.a` and clobber the variant a postcss config's top-level `await` needs, failing at build time with `__turbopack_context__.a is not a function`. Which variant landed depended on emission order, so it reproduced intermittently. Add `shared_runtime_chunk` to NodeJsChunkingContext and set it on the shared node execution contexts, so those always emit the complete runtime. This matches the existing dev carve-out: in both cases a single graph can't see everything sharing the runtime. On the browser side the equivalent check tested `RuntimeType::Development` as a proxy for per-page graphs. That happens to work only because `per_page_module_graph` is currently `mode == Development`; plumb the real flag through instead. Also rename `has_async_modules` to `include_async_module_runtime`, since the value is now true whenever we can't tell rather than meaning the graph has async modules.
lukesandberg
added a commit
that referenced
this pull request
Aug 4, 2026
#96599) ### What? Turbopack could emit a `[turbopack]_runtime.js` without the async-module (top-level await) machinery while chunks written next to it call `__turbopack_context__.a(...)`, failing production builds with `TypeError: __turbopack_context__.a is not a function`. Reported against 16.3 when loading a `postcss.config.js`. Intermittent, production only. ### Why? The runtime chunk is emitted to a fixed path, so every module graph using a chunking context writes the same file. Since #94376 the async-module machinery is dropped when a graph has no async modules — but that's decided per graph. The Next.js node execution context is shared by every build-time JS evaluation (postcss configs, webpack loaders, `next/font/google`), each with its own `ModuleGraph`. A graph with no async modules emits a runtime without `.a` and clobbers the variant the postcss loader needs. The winner is `assets.first()` in `emit_assets`, which depends on emission order — hence the intermittency. ### How? Add `shared_runtime_chunk` to the chunking contexts and set it where one runtime is shared by several graphs, so those always emit the complete runtime. This matches the carve-out that already exists for development: in both cases a single graph can't see everything sharing the runtime. Whole-app server contexts keep the optimization. The browser side tested `RuntimeType::Development` as a proxy for per-page graphs, which only works because `per_page_module_graph` currently *is* `mode == Development`. It now reads the real flag. Also renames `has_async_modules` to `include_async_module_runtime`, since it's now true whenever we can't tell. I audited the rest of `.next/build/` for the same hazard: the runtime chunk (and its `.map`) was the only fixed-path output. Everything else is content-hashed via `AssetIdent::output_name`. ### Testing `test/production/app-dir/turbopack-shared-runtime-async-module` uses a single synchronous loader — enough on its own to produce the stripped runtime. Verified to fail without the fix and pass with it. `cargo test -p turbopack-tests` passes with no snapshot churn. Follow-up, not in this PR: the conflict is silent. `EmitConflictIssue` is `IssueSeverity::Error` but never fired here, because the two runtimes weren't grouped into one `emit_assets` call.
m-kawafuji
pushed a commit
to m-kawafuji/next.js
that referenced
this pull request
Aug 8, 2026
…ded (vercel#94376) There's a lot of code in the runtime that enables top-level async support. We may not always need to ship this. It makes up approximately 10% of the runtime. This PR separates this code into a separate module: `async-module.ts`. This is included in the runtime based on this condition: ```rust !self.module_graph.async_module_info().await?.is_empty() ``` --------- Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
m-kawafuji
pushed a commit
to m-kawafuji/next.js
that referenced
this pull request
Aug 8, 2026
vercel#96599) ### What? Turbopack could emit a `[turbopack]_runtime.js` without the async-module (top-level await) machinery while chunks written next to it call `__turbopack_context__.a(...)`, failing production builds with `TypeError: __turbopack_context__.a is not a function`. Reported against 16.3 when loading a `postcss.config.js`. Intermittent, production only. ### Why? The runtime chunk is emitted to a fixed path, so every module graph using a chunking context writes the same file. Since vercel#94376 the async-module machinery is dropped when a graph has no async modules — but that's decided per graph. The Next.js node execution context is shared by every build-time JS evaluation (postcss configs, webpack loaders, `next/font/google`), each with its own `ModuleGraph`. A graph with no async modules emits a runtime without `.a` and clobbers the variant the postcss loader needs. The winner is `assets.first()` in `emit_assets`, which depends on emission order — hence the intermittency. ### How? Add `shared_runtime_chunk` to the chunking contexts and set it where one runtime is shared by several graphs, so those always emit the complete runtime. This matches the carve-out that already exists for development: in both cases a single graph can't see everything sharing the runtime. Whole-app server contexts keep the optimization. The browser side tested `RuntimeType::Development` as a proxy for per-page graphs, which only works because `per_page_module_graph` currently *is* `mode == Development`. It now reads the real flag. Also renames `has_async_modules` to `include_async_module_runtime`, since it's now true whenever we can't tell. I audited the rest of `.next/build/` for the same hazard: the runtime chunk (and its `.map`) was the only fixed-path output. Everything else is content-hashed via `AssetIdent::output_name`. ### Testing `test/production/app-dir/turbopack-shared-runtime-async-module` uses a single synchronous loader — enough on its own to produce the stripped runtime. Verified to fail without the fix and pass with it. `cargo test -p turbopack-tests` passes with no snapshot churn. Follow-up, not in this PR: the conflict is silent. `EmitConflictIssue` is `IssueSeverity::Error` but never fired here, because the two runtimes weren't grouped into one `emit_assets` call.
jenistenxavier
pushed a commit
to jenistenxavier/nudgebee
that referenced
this pull request
Sep 7, 2026
…(#36122) [email protected] fails cold Turbopack builds nondeterministically: every CSS module that goes through PostCSS errors with TypeError: __turbopack_context__.a is not a function at mod (postcss.config.js_.loader.mjs:11:31) Turbopack spawns two Node transform pools for this app — `postcss` (we have a postcss.config.js) and `webpack-loaders` (sass, plus the @svgr/webpack rule in next.config.js) — and both write the same .next/build/chunks/[turbopack]_runtime.js. Since vercel/next.js#94376 ("smaller runtime size", shipped in 16.3) the runtime only carries top-level-await support when async_module_info is non-empty, so the webpack-loaders variant omits `contextPrototype.a = asyncModule`. The generated postcss.config.js_.loader.mjs uses top-level await and calls that helper. Last pool to write the shared file decides whether the build works — hence the flake. Turbopack only emits the async `_.loader.mjs` for configs whose path ends in `.js`, so renaming the (already CommonJS) config to `.cjs` removes the async loader entirely and makes the build deterministic. Config discovery is unaffected: Turbopack looks for postcss.config `.js`/`.mjs`/`.cjs`/`.ts`/`.mts`/`.cts`. Upstream: vercel/next.js#96591, vercel/next.js#96619 (open, no fix released; 16.3.1-canary.11 still affected, 16.2.12 unaffected). Verified locally on 16.3.0 with a cold .next: - `npm run build` succeeds - 0 chunks reference postcss.config*_.loader.mjs (was 4) - autoprefixer still runs: source has no -webkit-user-select, built CSS has 6 Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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.
There's a lot of code in the runtime that enables top-level async support. We may not always need to ship this. It makes up approximately 10% of the runtime. This PR separates this code into a separate module:
async-module.ts. This is included in the runtime based on this condition: