Skip to content

dev server: return the promise from import() with options - #46683

Open
robobun wants to merge 2 commits into
mainfrom
robobun/ad922343/dev-server-import-options-return
Open

robobun wants to merge 2 commits into
mainfrom
robobun/ad922343/dev-server-import-options-return

Conversation

@robobun

@robobun robobun commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • In the dev server, import(x, options) returns undefined when the dev server did not bundle x (a built-in such as node:path, a computed specifier).
  • The promise of the real import is dropped, so nothing can handle its rejection. A route that wraps await import(missingFile, { with }) in try/catch does not reach the catch, and the unhandled rejection ends the dev server process.
  • The cause is HMRModule.dynamicImport (src/runtime/bake/hmr-module.ts:165). The fallback for the options form is new Function("specifier, opts", "import(specifier, opts)"), and that body has no return.

Fix

  • The body of the helper is now return import(specifier, opts).
  • This is correct because the branch beside it, import(id) without options, already returns its promise.
  • Verified: two new tests in test/bake/dev/esm.test.ts (server runtime and client runtime) fail on main and pass here. The other 18 tests of that file pass.

Background

Downsides

  • None found. Checked: the printer's hmr.dynamicImport(...) lowering is the only caller. The runtime text grows 7 bytes per side (client 52,885 to 52,892, server 10,908 to 10,915).
Notes

Runtime size is from bun src/codegen/bake-codegen.ts --debug=OFF on main and on this branch.

Fail-before on main (bun bd test test/bake/dev/esm.test.ts with main's src/): the server test reports builtin: [false, false], computed: [false, null] and missing: "resolved". The client test reports computed: ["undefined", null].

Not in this PR. The same method has two more faults: it evaluates a bundled module inside the import() call, and a failed load throws at the call and does not reject. A fix for those makes import() reject, and a caller that keeps the promise (for example preloadModule of react-server-dom-bun, or React.lazy) then keeps the rejection after the file is fixed, because replaceModules does not run that importer again. That work needs the importer re-run first (#46764), so it is separate from this one-token fix. It is #46770.


no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/bake/dev/esm.test.ts

@robobun

robobun commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 1:13 AM PT - Oct 24th, 2026

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


🧪   To try this PR locally:

bunx bun-pr 46683

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

bun-46683 --bun

@robobun

robobun commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status

Reproduced with the two new tests in test/bake/dev/esm.test.ts ("import() with an options object returns its promise", server runtime and client runtime). On main both fail: import("node:path", { with: {} }) returns undefined, and a route that wraps await import(missingFile, { with }) in try/catch does not reach the catch. Both pass on this branch.

Local runs with the debug build on Linux x64: bun bd test test/bake/dev/esm.test.ts gives 18 pass and 2 fail with main's src/ (the two new tests), and 20 pass with this branch.

CI. The diff is green. The two red jobs are not from this change:

  • Build #131659: 178 jobs passed. The macOS aarch64 test job stopped before it ran a test: buildkite-agent artifact download timed out after 120s.
  • Build #131703, a rerun of the same diff: 180 of 181 jobs passed, macOS aarch64 included. The macOS x64 test job failed on test/js/node/http/node-http-uaf.test.ts ("should not crash on abort", timed out after 20000ms). This PR does not touch node:http.

test/bake/dev/esm.test.ts passed on every lane that ran it in both builds, Windows included.

PR: #46683

@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 7d240c1 and edb7efd.

Files selected for processing: 2
  • src/runtime/bake/hmr-module.ts
  • test/bake/dev/esm.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

Walkthrough

The options-bearing fallback in HMRModule.dynamicImport now returns the native import(specifier, opts) promise. Development-mode tests cover dynamic imports with options in server and client runtimes.

Changes

Dynamic import options

Layer / File(s) Summary
Return native import promises
src/runtime/bake/hmr-module.ts, test/bake/dev/esm.test.ts
The fallback returns the native import promise. Server and client tests check promise results and resolved values. The server test also checks rejection for a missing import and expected responses from consecutive route requests.

Possibly related PRs

  • oven-sh/bun#46615: Changes the same HMR module-loading runtime, including CommonJS import loading and retry behavior after failed loads.

Suggested reviewers: alii, jarred-sumner

Priority: Normal

Merge Risk: Minimal · up to b1983

Merge Risk: ⚪ Minimal · up to b1983

The fallback now returns the import promise, and no merge-blocking concern remains from the supplied evidence.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly identifies the main change: returning the promise from dynamic import() calls with options in the development server.
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 explains the problem, fix, scope, downsides, and verification results. It does not use the template headings exactly, but it provides the required information and is complete.
Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline finding, I also checked for sibling sites sharing the missing-return pattern: the new Function import helper at src/runtime/bake/hmr-module.ts:165 is the only one in the runtime, both printer lowerings (src/js_printer/lib.rs:3029 and :3652) call the same dynamicImport method, and hmr-module.ts is the single source for the client and server runtime builds, so no parallel copy needs the same fix. The (await builtin) === path namespace-identity assertion and the client-side base-URL difference for the options form were also examined and ruled out as defects in this change.

Extended reasoning...

The change adds return to the new Function body that forwards import(specifier, opts) in the Bake dev-server HMR runtime and adds two devTest cases covering the server and client runtimes. It touches no security-sensitive surface. One confirmed inline finding (synchronous throw instead of rejection for bundled modules in the error state) was reported separately, so this note only records the sibling-site and ruled-out checks.

Comment thread src/runtime/bake/hmr-module.ts Resolved

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants