-
Notifications
You must be signed in to change notification settings - Fork 5.1k
require(esm): a macro whose file is already in the graph no longer spins, and a shared module is fetched once (WebKit bump for oven-sh/WebKit#675) #42915
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
4
commits into
robobun/464ab2b6/macro-under-require-esm-spin
Choose a base branch
from
robobun/75afb400/macro-file-in-require-graph
base: robobun/464ab2b6/macro-under-require-esm-spin
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
93bace7
test: macros whose file is already in the require()'s graph, and modu…
robobun c1bb9db
Pin WebKit to the preview build of oven-sh/WebKit#675
robobun 982869c
test: a failing macro must have run, and import() of a just-fetched C…
robobun 6142e89
Pin WebKit to autobuild-preview-pr-675-d953c3bd [skip size check]
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
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,93 @@ | ||
| // require() of an ES module loads the whole graph without a yield to the event loop. The loader | ||
| // fetches each module synchronously, but a queued job hands the source to the module's registry | ||
| // entry. Until that job runs, the entry looks like a fetch that is still in flight. A second request | ||
| // for the module, and a require() of it from code that runs in between, must use the source that is | ||
| // already there. | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| test.concurrent("require(esm) loads a module that two siblings import once", async () => { | ||
| using dir = tempDir("require-esm-fetched-once", { | ||
| "index.cjs": ` | ||
| const loads = []; | ||
| Bun.plugin({ | ||
| name: "count-loads", | ||
| setup(build) { | ||
| build.onLoad({ filter: /\\.mjs$/ }, args => { | ||
| loads.push(require("node:path").basename(args.path)); | ||
| return { contents: require("node:fs").readFileSync(args.path, "utf8"), loader: "js" }; | ||
| }); | ||
| }, | ||
| }); | ||
| require("./root.mjs"); | ||
| console.log(JSON.stringify(loads)); | ||
| `, | ||
| "root.mjs": `import "./a.mjs";\nimport "./b.mjs";\n`, | ||
| "a.mjs": `import "./shared.mjs";\n`, | ||
| "b.mjs": `import "./shared.mjs";\n`, | ||
| "shared.mjs": `export {};\n`, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "index.cjs"], | ||
| 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 }).toEqual({ | ||
| stdout: JSON.stringify(["root.mjs", "a.mjs", "b.mjs", "shared.mjs"]) + "\n", | ||
| stderr: "", | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| // The loader evaluates a CommonJS module when it creates the module's record. Here that happens for | ||
| // second.cjs while the imports of esm.mjs are requested, one request after leaf.mjs was fetched. | ||
| test.concurrent("require() of an ES module that the enclosing graph has just fetched", async () => { | ||
| using dir = tempDir("require-esm-just-fetched", { | ||
| "index.cjs": `globalThis.order = [];\nrequire("./root.mjs");\nconsole.log(JSON.stringify(globalThis.order));\n`, | ||
| "root.mjs": `import "./first.cjs";\nimport "./second.cjs";\nglobalThis.order.push("root");\n`, | ||
| "first.cjs": `require("./esm.mjs");\nglobalThis.order.push("first");\n`, | ||
| "esm.mjs": `import "./leaf.mjs";\nimport "./second.cjs";\nglobalThis.order.push("esm");\n`, | ||
| "second.cjs": `require("./leaf.mjs");\nglobalThis.order.push("second");\n`, | ||
| "leaf.mjs": `globalThis.order.push("leaf");\n`, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "index.cjs"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stderr: "pipe", | ||
| }); | ||
|
robobun marked this conversation as resolved.
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect({ stdout, stderr }).toEqual({ | ||
| stdout: JSON.stringify(["leaf", "second", "esm", "first", "root"]) + "\n", | ||
| stderr: "", | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| // The loader reads the exports of a CommonJS module when it creates the module's record. Here the | ||
| // import() in second.cjs comes one request after counted.cjs was fetched, and the record must still be | ||
| // created once. | ||
| test.concurrent("import() of a CommonJS module that the enclosing graph has just fetched", async () => { | ||
| using dir = tempDir("require-esm-built-once", { | ||
| "index.cjs": `require("./root.mjs");\nglobalThis.imported.then(ns => console.log(JSON.stringify({ reads: globalThis.reads, x: ns.x })));\n`, | ||
| "root.mjs": `import "./first.cjs";\nimport "./second.cjs";\n`, | ||
| "first.cjs": `require("./esm.mjs");\n`, | ||
| "esm.mjs": `import "./counted.cjs";\nimport "./second.cjs";\n`, | ||
| "second.cjs": `globalThis.imported = import("./counted.cjs");\n`, | ||
| "counted.cjs": `globalThis.reads = 0;\nObject.defineProperty(module.exports, "x", { enumerable: true, get: () => ++globalThis.reads });\n`, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "index.cjs"], | ||
| 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 }).toEqual({ stdout: JSON.stringify({ reads: 1, x: 1 }) + "\n", stderr: "" }); | ||
| expect(exitCode).toBe(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.