Skip to content

[turbopack] Only ship top-level async support in the runtime when needed - #94376

Merged
sampoder merged 4 commits into
canaryfrom
sp/turbopack/trim-async-module-runtime
Jun 4, 2026
Merged

sampoder merged 4 commits into
canaryfrom
sp/turbopack/trim-async-module-runtime

Conversation

@sampoder

@sampoder sampoder commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

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:

!self.module_graph.async_module_info().await?.is_empty()

@sampoder
sampoder requested review from bgw and lukesandberg June 3, 2026 07:49
@github-actions

github-actions Bot commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Stats skipped

Commit: a29c5d1
View workflow run

@github-actions

github-actions Bot commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: a29c5d1

sampoder and others added 2 commits June 3, 2026 16:33
…ntime/async-module.ts

Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
@sampoder
sampoder merged commit 539efa8 into canary Jun 4, 2026
240 of 245 checks passed
@sampoder
sampoder deleted the sp/turbopack/trim-async-module-runtime branch June 4, 2026 16:42
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants