Repository navigation
Fix spurious "require() async module" TypeError when require() hits an ESM module the import graph is still fetching #37185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
11
commits into
main
Choose a base branch
from
farm/7d8432f2/require-esm-in-flight-sibling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+203
−4
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f6c9b25
require(esm): deliver sync-transpiled source to a mid-fetch registry …
robobun be872ba
Merge remote-tracking branch 'origin/main' into farm/7d8432f2/require…
robobun c8bf720
test: add regression test for #33180
robobun e06da7d
require(esm): make the in-flight sibling test deterministic, trim com…
robobun f4bd0ba
require(esm): shorten provideFetchForSyncLoad comment
robobun a671e79
require(esm): drop provideFetchForSyncLoad header comment
robobun e05032d
require(esm): symmetric exception checks in provideFetchForSyncLoad
robobun 2e2870f
ci: retrigger
robobun a5f80d7
Merge remote-tracking branch 'origin/main' into farm/7d8432f2/require…
robobun 5968a2f
require(esm): only settle the JavaScript-typed registry entry
robobun 9ac5986
require(esm): tighten provideFetchForSyncLoad comments
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| // A CommonJS module imported by an ESM graph runs its body while the graph is | ||
| // still loading, so a require() in it can hit an ESM sibling whose transpile is | ||
| // still in flight on the transpiler thread. The CJS module here is named | ||
| // reflect-metadata because the runtime transpiles that package on the main | ||
| // thread (ALWAYS_SYNC_MODULES in src/runtime/jsc_hooks.rs): its body runs in the | ||
| // microtask checkpoint right after the graph's fetches are issued, before the | ||
| // thread pool can have delivered e.mjs, so the sibling is mid-fetch on every run. | ||
| test("require() of an ESM sibling the import graph is still fetching loads it synchronously", async () => { | ||
| using dir = tempDir("require-esm-in-flight", { | ||
| "entry.mjs": ` | ||
| import "./e.mjs"; | ||
| import "reflect-metadata"; | ||
| console.log(globalThis.order.concat("entry").join(",")); | ||
| `, | ||
| "e.mjs": ` | ||
| (globalThis.order ??= []).push("e"); | ||
| export const e = 1; | ||
| `, | ||
| "node_modules/reflect-metadata/package.json": JSON.stringify({ name: "reflect-metadata", main: "index.js" }), | ||
| "node_modules/reflect-metadata/index.js": ` | ||
| const { e } = require("../../e.mjs"); | ||
| (globalThis.order ??= []).push("polyfill:" + e); | ||
| `, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "entry.mjs"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect({ stdout, stderr, exitCode }).toEqual({ stdout: "e,polyfill:1,entry\n", stderr: "", exitCode: 0 }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // https://github.com/oven-sh/bun/issues/33180 | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| test.concurrent("require() of an ESM module the surrounding import graph is still fetching", async () => { | ||
| using dir = tempDir("issue-33180-cycle", { | ||
| "entry.mjs": ` | ||
| import "./starter.cjs"; | ||
| import a from "./a.cjs"; | ||
| import { lateViaESM } from "./b.mjs"; | ||
| console.log(JSON.stringify({ evaluations: globalThis.evaluations, late: a.late, lateViaESM: lateViaESM() })); | ||
| `, | ||
| "starter.cjs": `require("./a.cjs");`, | ||
| "a.cjs": ` | ||
| globalThis.evaluations = (globalThis.evaluations ?? 0) + 1; | ||
| exports.early = "early"; | ||
| require("./b.mjs"); | ||
| exports.late = "late"; | ||
| `, | ||
| "b.mjs": ` | ||
| import a from "./a.cjs"; | ||
| export function lateViaESM() { return a.late; } | ||
| `, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "entry.mjs"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect({ stdout, stderr, exitCode }).toEqual({ | ||
| stdout: JSON.stringify({ evaluations: 1, late: "late", lateViaESM: "late" }) + "\n", | ||
| stderr: "", | ||
| exitCode: 0, | ||
| }); | ||
| }); | ||
|
|
||
| test.concurrent("consecutive require()s of several ESM siblings that are still transpiling", async () => { | ||
| const files: Record<string, string> = { | ||
| "entry.mjs": ` | ||
| import "./e1.mjs"; | ||
| import "./e2.mjs"; | ||
| import "./e3.mjs"; | ||
| import "./e4.mjs"; | ||
| import "./k.cjs"; | ||
| console.log("entry-done"); | ||
| `, | ||
| "k.cjs": ` | ||
| const ids = [1, 2, 3, 4].map(i => require("./e" + i + ".mjs").id); | ||
| console.log("required " + JSON.stringify(ids)); | ||
| module.exports = {}; | ||
| `, | ||
| }; | ||
| for (let i = 1; i <= 4; i++) { | ||
| let source = ""; | ||
| for (let j = 0; j < 500; j++) source += `export const v${j} = ${j};\n`; | ||
| files[`e${i}.mjs`] = source + `export const id = ${i};\n`; | ||
| } | ||
| using dir = tempDir("issue-33180-siblings", files); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "entry.mjs"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect({ stdout, stderr, exitCode }).toEqual({ | ||
| stdout: "required [1,2,3,4]\nentry-done\n", | ||
| stderr: "", | ||
| exitCode: 0, | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.