Skip to content

bake: keep a route failed while a sibling client component has a pending resolution failure - #40115

Closed
robobun wants to merge 1 commit into
mainfrom
farm/8838396d/bake-scb-failure-placeholder
Closed

robobun wants to merge 1 commit into
mainfrom
farm/8838396d/bake-scb-failure-placeholder

Conversation

@robobun

@robobun robobun commented Aug 22, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • With serverComponents.separateSSRGraph, a route imports a "use client" file whose own import does not resolve. The route serves the build error page. After an edit to a sibling client component, the next request runs the route and throws Failed to load bundled module 'components/Sibling.ts'. This is not a dynamic import, and therefore is a bug in Bun's bundler. The route then never recovers: null is not an object (evaluating 'pageModule.streaming').
  • Cause: the file is parsed under the browser target, so its resolution failure goes to the client graph (src/bundler/bundle_v2.rs:5885). The failure skips server component boundary registration, so the server graph has no node for the file and the route has no edge to it. check_route_failures (src/runtime/bake/DevServer.rs:2236) traces the route, finds nothing, and marks it loaded.

Fix

  • When such a file fails import resolution, the bundler calls a new dev server hook, insert_stale_client_component_boundary.
  • The hook inserts a stale server-graph node flagged is_client_component_boundary, the placeholder shape the success path creates. Route edges attach to it, and the failure trace crosses from it into the client graph, where the failure is recorded.
  • The route stays on the build error page until the failure clears, so it never runs with a missing module.
  • Verified: new test in test/bake/dev/bundle.test.ts, fails on unfixed bun at the post-edit step. Also ran the bake dev suites bundle, esm, hot, html, css, response-to-bake-response, incremental-graph-edge-deletion.

Background

  • The dev server keeps two incremental graphs: server files (routes, SSR copies) and browser files. A route's failure check walks the server graph and crosses into the client graph only at client component boundary nodes.
  • A boundary is a "use client" file. A successful parse registers it: a reference proxy stub in the server graph, plus an SSR copy when the graphs are separate. A failed parse or resolve skips that step.
  • Server-side failures and "use client" syntax errors are attributed to the server graph, so only this resolve path had the hole.
Notes

Reproduction (from the issue): a route imports two "use client" components. One imports ./sibling-missing, which does not exist.

  1. GET / serves the build error page (correct).
  2. Edit the clean component.
  3. GET / previously ran the route and threw throwNotFound for the sibling. The route's server HMR module was cached in Pending state with exports = null, and nothing ever invalidated it.
  4. Create the missing file.
  5. GET / previously failed with pageModule.streaming on null. With the fix it renders.

The step 5 symptom is downstream of step 3: the registry entry for the route module is created before its dependencies parse, and the synchronous throwNotFound leaves it Pending with null exports. With the route held in the failed state, that request never runs, so the registry is never poisoned.

Variants checked against the unfixed build:

  • A syntax error in the sibling instead of an unresolved import already behaved correctly. Its parse failure carries the original (server) target, so the failure lands in the server graph and is traceable.
  • separateSSRGraph: false already behaved correctly for the same reason.

The placeholder node is keyed by the file's absolute path, the same key the real reference proxy chunk uses. When the failure clears and the client component bundles, the stub chunk lands on the same node and replaces the placeholder. The hook also ensures a client-graph node exists, because the server-to-client crossing in trace_imports panics when a boundary has no client-graph file.

Pre-existing failure seen while testing: production > import.meta properties are inlined in catch-all routes during production build times out at 5s under the debug ASAN build, with and without this diff.


[review] gate passed · iteration 0 · 5 files touched

fails on main (without fix)
ASAN without fix: 1 FAILED
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/bake/dev/bundle.test.ts
bun test v1.4.1 (4448a2e21)

test/bake/dev/bundle.test.ts:
Dev server testing directory: /tmp/bun-dev-test-W17YSG
�[0;30mdev|�[0m Started development server: http://localhost:37005
�[0;30mdev|�[0m �[32mBundled page in 189ms�[0m�[2m:�[0m routes/index.ts �[2m+ 1 more�[0m
�[0;30mdev|�[0m [Bun] Hot update was not accepted because it or its importers do not call `import.meta.hot.accept`. To prevent full page reloads, call `import.meta.hot.accept` in one of the following files to handle the update:
�[0;30mdev|�[0m 
�[0;30mdev|�[0m Module "db.ts" is not accepted by routes/index.ts,
�[0;30mdev|�[0m �[32mReloaded in 75ms�[0m�[2m:�[0m db.ts
�[0;30mdev|�[0m [Bun] Hot update was not accepted because it or its importers do not call `import.meta.hot.accept`. To prevent full page reloads, call `import.meta.hot.accept` in one of the following files to handle the update:
�[0;30mdev|�[0m 
�[0;30mdev|�[0m Module "routes/index.ts" is a root module that does not self-accept.
�[0;30mdev|�[0m �[36m[x2]�[0m �[32mReloaded in 66ms�[
... (truncated)

release without fix: 1 FAILED
bun test v1.4.0-canary.1 (4448a2e21)

test/bake/dev/bundle.test.ts:
Dev server testing directory: /tmp/bun-dev-test-3bMZ2m
�[0;30mdev|�[0m Started development server: http://localhost:42693
�[0;30mdev|�[0m �[32mBundled page in 2ms�[0m�[2m:�[0m routes/index.ts �[2m+ 1 more�[0m
�[0;30mdev|�[0m [Bun] Hot update was not accepted because it or its importers do not call `import.meta.hot.accept`. To prevent full page reloads, call `import.meta.hot.accept` in one of the following files to handle the update:
�[0;30mdev|�[0m 
�[0;30mdev|�[0m Module "db.ts" is not accepted by routes/index.ts,
�[0;30mdev|�[0m �[32mReloaded in 1ms�[0m�[2m:�[0m db.ts
�[0;30mdev|�[0m [Bun] Hot update was not accepted because it or its importers do not call `import.meta.hot.accept`. To prevent full page reloads, call `import.meta.hot.accept` in one of the following files to handle the update:
�[0;30mdev|�[0m 
�[0;30mdev|�[0m Module "routes/index.ts" is a root module that does not self-accept.
�[0;30mdev|�[0m �[36m[x2]�[0m �[32mReloaded in 1ms�[0m�[2m:�[0m routes/index.ts
(pass)  DEV:bundle-1: import identifier doesnt get renamed [199.33ms]
�[0;30mdev|�[0m Started development server: http://localhos
... (truncated)
passes on PR (with fix)
ASAN with fix: all passed
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/bake/dev/bundle.test.ts
bun test v1.4.1 (4448a2e21)

test/bake/dev/bundle.test.ts:
Dev server testing directory: /tmp/bun-dev-test-32Z3B4
�[0;30mdev|�[0m Started development server: http://localhost:37625
�[0;30mdev|�[0m �[32mBundled page in 198ms�[0m�[2m:�[0m routes/index.ts �[2m+ 1 more�[0m
�[0;30mdev|�[0m [Bun] Hot update was not accepted because it or its importers do not call `import.meta.hot.accept`. To prevent full page reloads, call `import.meta.hot.accept` in one of the following files to handle the update:
�[0;30mdev|�[0m 
�[0;30mdev|�[0m Module "db.ts" is not accepted by routes/index.ts,
�[0;30mdev|�[0m �[32mReloaded in 79ms�[0m�[2m:�[0m db.ts
�[0;30mdev|�[0m [Bun] Hot update was not accepted because it or its importers do not call `import.meta.hot.accept`. To prevent full page reloads, call `import.meta.hot.accept` in one of the following files to handle the update:
�[0;30mdev|�[0m 
�[0;30mdev|�[0m Module "routes/index.ts" is a root module that does not self-accept.
�[0;30mdev|�[0m �[36m[x2]�[0m �[32mReloaded in 40ms�[
... (truncated)

release with fix: all passed
$ bun scripts/build.ts --profile=release
[configured] bun-profile → bun (stripped)
  target       linux-x64-gnu
  build type   Release
  build dir    ./build/release
  revision     b989994901
  features     baseline

23 deps, 129 codegen, 1172 objects in 643ms

ninja: Entering directory `/workspace/bun/build/release'
[1/1244] install /workspace/bun
bun install v1.4.0-canary.1 (4448a2e21)

Checked 26 installs across 63 packages (no changes) [11.00ms]
[2/1244] install /workspace/bun/packages/bun-error
bun install v1.4.0-canary.1 (4448a2e21)

Checked 1 install across 2 packages (no changes) [2.00ms]
[3/1244] gen ErrorCode+*.h
[4/1244] install /workspace/bun/src/node-fallbacks
bun install v1.4.0-canary.1 (4448a2e21)

Checked 111 installs across 104 packages (no changes) [13.00ms]
[5/1244] gen bindgenv2
[6/1244] fetch libjpeg-turbo
[libjpeg-turbo] up to date
[7/1217] gen node-fallbacks/react-refresh.js
Bundled 1 module in 18ms

  react-refresh.js  4.81 KB  (entry point)

[8/1217] gen ProcessBindingConstants.lut.h
Generating /workspace/bun/build/release/codegen/ProcessBindingConstants.lut.h from /workspace/bun/src/jsc/bindings/ProcessBindingConstants.cpp
[9/1217] fetch 
... (truncated)
diff hotspot
src/bundler/bundle_v2.rs           | 20 +++++++++++++
 src/bundler/lib.rs                 |  1 +
 src/runtime/bake/DevServer.rs      | 21 ++++++++++++++
 src/runtime/bake/dev_server/mod.rs |  5 ++++
 test/bake/dev/bundle.test.ts       | 59 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 106 insertions(+)

gate history · 1 passed · 0 rejected · iteration 0

evidence per changed file
file                                reads  edits  tests
src/bundler/bundle_v2.rs                5      2      0
src/bundler/lib.rs                      1      1      0
src/runtime/bake/DevServer.rs           4      2      0
src/runtime/bake/dev_server/mod.rs      2      1      0
test/bake/dev/bundle.test.ts            1      2      0

root cause · written by the author bot

In separate SSR graph mode, a "use client" file is parsed under the browser target, so its resolution failure was recorded only in the client graph while the failed file never registered a server component boundary, leaving the server graph with no node connecting the route to the failure. As a result, the route failure trace found nothing, a rebuild triggered by editing a sibling component cleared the route's failed state, and the route ran against a module that never bundled. The fix adds a dev server hook that inserts a stale server graph node flagged as a client component boundary when …

…ing resolution failure

With separateSSRGraph, a "use client" file is parsed under the browser
target, so its resolution failure is attributed to the client graph. The
failed file never reaches server component boundary registration, so the
server graph had no node for it and routes had no edge to the failure.
After a rebuild of an unrelated file, the route failure trace found
nothing, the route loaded, and its import of the never-bundled component
threw at runtime. That request left the route module stuck even after
the failure was fixed.

Insert a stale server graph placeholder marked as a client component
boundary when such a file fails to resolve an import. Route failure
traces then cross into the client graph and find the failure.

Fixes #40113
@robobun

robobun commented Aug 22, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 10:32 AM PT - Aug 22nd, 2026

❌ @robobun, your commit b989994 has 1 failures in Build #103646 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 40115

That installs a local version of the PR into your bun-40115 executable, so you can run:

bun-40115 --bun

@coderabbitai

coderabbitai Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 1d7da65f-060f-43cf-b50d-28cffad31914

📥 Commits

Reviewing files that changed from the base of the PR and between abe2ad4 and b989994.

📒 Files selected for processing (5)
  • src/bundler/bundle_v2.rs
  • src/bundler/lib.rs
  • src/runtime/bake/DevServer.rs
  • src/runtime/bake/dev_server/mod.rs
  • test/bake/dev/bundle.test.ts

Included review availability: Your plan provides up to 5 included reviews per hour; 1 remains after this review.


Walkthrough

The bundler preserves "use client" metadata when import resolution fails. In separate SSR graph mode, it inserts a stale client-component boundary through the dev server. A regression test verifies failure persistence and recovery.

Changes

Stale Client Boundary Recovery

Layer / File(s) Summary
Failure detection and dispatch
src/bundler/bundle_v2.rs, src/bundler/lib.rs, src/runtime/bake/dev_server/mod.rs
The bundler captures the failed file metadata, detects browser-targeted "use client" files, and dispatches stale-boundary insertion through DevServerHandle.
Graph placeholder and regression coverage
src/runtime/bake/DevServer.rs, test/bake/dev/bundle.test.ts
The dev server creates client and stale server graph entries for the failed path. The regression test verifies that the route remains failed after an unrelated edit and recovers after the missing module is created.

Suggested reviewers: jarred-sumner, dylan-conway, alii

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The description clearly explains the problem, root cause, fix, and verification results, including the regression test and suites run.
Title check ✅ Passed The title clearly and concisely describes the primary fix for route failures caused by pending sibling client-component resolution errors.

Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

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.

I reviewed this PR and didn't find any bugs. The fix is well-reasoned with a thorough writeup and a regression test that exercises the full failure→edit→recovery cycle. Because it introduces a new incremental-graph state (a stale server-graph node pre-marked as a client-component boundary before any chunk arrives) and threads a new hook through the bundler↔DevServer dispatch boundary, a human familiar with bake's graph invariants should confirm the placeholder interacts correctly with receiveChunk's found-existing SCB transitions and client_components_added bookkeeping.

What was reviewed

  • Verified the hook mirrors track_resolution_failure's locking/threading pattern (same call site in run_resolution_for_parse_task, same graph_safety_lock guard).
  • Checked insert_stale + post-hoc is_client_component_boundary = true is idempotent whether the node already exists, and that the client-graph insert_empty prevents the trace_imports panic at incremental_graph.rs:1206.
  • Traced the recovery path: when the real reference-proxy chunk lands on the placeholder, receiveChunk's found_existing && scb arm re-marks the flag and pushes client_components_added exactly once (the placeholder itself never pushed), matching the no-placeholder flow.
  • Confirmed source_path is captured before resolve_import_records and consumed before parse_result.value is overwritten.
Extended reasoning...

Overview

This PR fixes a bake dev-server bug (#40113) where, with serverComponents.separateSSRGraph, a route importing a "use client" file whose own import fails to resolve would incorrectly transition to Loaded after an unrelated sibling edit, then run with a missing bundled module and permanently poison its server HMR module registry entry. The fix adds a new DevServerHandle dispatch hook, insert_stale_client_component_boundary, called from run_resolution_for_parse_task's error branch when a browser-target use_directive == Client file fails resolution under separate_ssr_graph. The hook inserts a stale server-graph node flagged is_client_component_boundary and an empty client-graph node so check_route_failures' trace crosses into the client graph and finds the recorded failure. Files touched: src/bundler/bundle_v2.rs (call site + captures), src/bundler/lib.rs (link_interface entry), src/runtime/bake/DevServer.rs (hook impl), src/runtime/bake/dev_server/mod.rs (link_impl wiring), and a new regression test in test/bake/dev/bundle.test.ts.

Security risks

None. This is dev-server-only incremental-graph bookkeeping; no untrusted input parsing, no auth/crypto, no externally reachable surface beyond what already exists.

Level of scrutiny

Medium-high. The diff is small (~50 LOC across four source files) and follows established patterns exactly — the new hook sits alongside track_resolution_failure in the same dispatch table, is called from the same thread context under the same graph_safety_lock debug guard, and composes two existing graph operations. But bake's dual incremental graphs are dense with lifecycle invariants (edge deletion, HMR invalidation, receiveChunk state transitions, client_components_added/removed bookkeeping), and the fix creates a node state that previously did not exist: a stale server node marked as a CCB before any chunk arrives for it. I traced the recovery path through receiveChunk's found_existing arm and it looks correct — the flag is re-set idempotently and client_components_added is pushed once, matching what a fresh insert would have done — but a maintainer who owns this subsystem should confirm there's no other consumer that assumes a CCB-flagged server node always has a corresponding SSR entry or reference-proxy chunk.

Other factors

The PR description is unusually thorough: it names the exact source lines for cause and effect, enumerates the variants checked against the unfixed build (syntax error vs. resolution failure; separateSSRGraph: false), explains why the placeholder key matches the real reference-proxy chunk's key so recovery replaces it in place, and reports running the full bake dev suite (bundle, esm, hot, html, css, response-to-bake-response, incremental-graph-edge-deletion). The test asserts all three phases (initial 500, still 500 after sibling edit, 200 after creating the missing file) with content assertions, not just status codes. The bug-hunting system found nothing. I'm deferring rather than approving only because the incremental-graph state machine is subtle enough that the placeholder's interaction with paths I did not exhaustively trace (e.g. invalidate, edge deletion when the route itself is edited, or the is_rsc flag insert_stale sets on the placeholder) deserves a human sign-off.

@robobun

robobun commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator Author

On the placeholder's interaction with receiveChunk and the manifest bookkeeping, the relevant paths are:

  • The placeholder is inserted outside receive_chunk, so it never pushes client_components_added. When the real reference proxy chunk lands, the found_existing && scb arm (incremental_graph.rs:720) re-sets the flag and pushes client_components_added once. That is the same count as the fresh-insert arm. The test's recovery step (page: object) depends on this: it loads the component through the manifest after the failure clears.
  • Demotion: if the directive is removed and a non-scb server chunk lands on the placeholder, the demotion arm (incremental_graph.rs:726) calls disconnect_and_delete_file and panics when the client file is missing. The hook inserts the client-graph file for this reason. A client_components_removed entry for a component that was never added is a no-op in registerUpdate (hmr-runtime-server.ts:188): the manifest lookup returns undefined and the loop body never runs.
  • The existing demotion test, "removing 'use client' from a component with a pending resolution failure", covers the directive-removal path and passes with this change.
  • insert_stale sets is_rsc on the placeholder, which matches the reference proxy side. receive_chunk sets is_rsc/is_ssr additively on existing nodes, so the flags converge to the same state as the no-placeholder flow.

@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as stale: this PR conflicts with main, and its last commit is from 2026-08-22.

The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR.

@robobun robobun closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant