Repository navigation
Register all top-level symbols before walking nested scopes in bundler renamer #30272
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
Changes from 4 commits
0f382d9
a763847
fbd9cb6
c7d0a7f
c1839e9
4f52904
3a661ff
b117b7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // https://github.com/oven-sh/bun/issues/30269 | ||
| // | ||
| // The ESM bundler renamer used to interleave two operations per part: | ||
| // 1. register that part's top-level symbols in the root scope, and | ||
| // 2. walk that part's nested scopes to rename collisions with the root. | ||
| // | ||
| // Because nested-scope walking for part N happened before part M>N's | ||
| // top-level symbols had been registered, a local binding in part N could be | ||
| // renamed to a name (`r` → `r2`) that later became a top-level symbol in | ||
| // part M (here the `r2` function). The local `r2` then shadowed the | ||
| // top-level `r2`, and callers inside `typecheck` invoked an `Expression` | ||
| // instance instead of the function. | ||
| // | ||
| // Fix: split the single-pass interleaved loop into two passes — register | ||
| // all top-level symbols for the whole chunk first, then walk nested scopes. | ||
| // Matches esbuild's structure. | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| test("#30269 bundler doesn't rename a nested local into another top-level symbol's name", async () => { | ||
| using dir = tempDir("bun-issue-30269", { | ||
| // Importing this forces Bun's ESM renamer to treat `r` as a conflicting | ||
| // name at the chunk's root scope. Removing `conflict.r()` from main.js | ||
| // hides the bug. | ||
| "conflict.js": ` | ||
| export function r() { | ||
| return "top-level r"; | ||
| } | ||
| `, | ||
| // `typecheck` has a nested \`let r\`. The renamer picks \`r2\` for it — | ||
| // but \`r2\` is already a top-level function in the same file. | ||
|
Comment on lines
+30
to
+31
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Tiny cosmetic nit: the Extended reasoning...What this isLines 30-31 of the new test file are ordinary `,
// `typecheck` has a nested \`let r\`. The renamer picks \`r2\` for it —
// but \`r2\` is already a top-level function in the same file.
"module.js": `Because these are plain TypeScript line comments (not inside any template literal), the source bytes are taken verbatim — Step-by-step
Why nothing prevents itThere is nothing to prevent — the parser, the test runner, and the bundled fixture are all completely unaffected. Comments are discarded before execution. No linter in this repo flags ``` in comments. This is purely a readability / consistency issue in newly-added prose. ImpactZero functional impact. The test compiles, runs, and catches the #30269 regression identically with or without the backslashes. The only effect is that the explanatory comment reads slightly oddly with four stray FixDelete the four backslashes: - // `typecheck` has a nested \`let r\`. The renamer picks \`r2\` for it —
- // but \`r2\` is already a top-level function in the same file.
+ // `typecheck` has a nested `let r`. The renamer picks `r2` for it —
+ // but `r2` is already a top-level function in the same file. |
||
| "module.js": ` | ||
| class Expression {} | ||
|
|
||
| export function run() { | ||
| const result = typecheck({ left: new Expression(), op: {}, right: {} }); | ||
| if (result !== true) throw new Error("expected true"); | ||
| return result; | ||
| } | ||
|
|
||
| function typecheck(node) { | ||
| let r, t, c; | ||
| block: { | ||
| let k = node.left; | ||
| let I = node.op; | ||
| let q = node.right; | ||
| r = k; | ||
| t = I; | ||
| c = q; | ||
| break block; | ||
| } | ||
|
|
||
| let d = r; | ||
| let n = t; | ||
| let s = c; | ||
| return r2().bo4(d, n, s).a(); | ||
| } | ||
|
|
||
| function r2() { | ||
| return { | ||
| bo4() { | ||
| return { | ||
| a() { | ||
| return true; | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
| } | ||
| `, | ||
| "main.js": ` | ||
| import * as conflict from "./conflict.js"; | ||
| import { run } from "./module.js"; | ||
|
|
||
| conflict.r(); | ||
| console.log("ok:", run()); | ||
| `, | ||
| }); | ||
|
|
||
| // Bundle, then run the bundled file. The renamer bug is triggered during | ||
| // bundling; executing the bundle is what surfaces the TypeError. | ||
| await using build = Bun.spawn({ | ||
| cmd: [bunExe(), "build", "--outfile", "out.js", "main.js"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| { | ||
| const [stdout, stderr, exitCode] = await Promise.all([build.stdout.text(), build.stderr.text(), build.exited]); | ||
| // `stdout` carries the "Bundled N modules" banner; assert on it before | ||
| // the exit-code check so a build failure shows what bun actually printed. | ||
| expect(stdout).toContain("out.js"); | ||
| expect(exitCode, stderr).toBe(0); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| await using run = Bun.spawn({ | ||
| cmd: [bunExe(), "out.js"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([run.stdout.text(), run.stderr.text(), run.exited]); | ||
| // On the buggy build this stderr contains: | ||
| // TypeError: r2 is not a function. (In 'r2()', 'r2' is an instance of Expression) | ||
| // Asserting on stdout + exit code is enough; stderr can hold unrelated | ||
| // debug-build warnings (e.g. MADV_DONTNEED) that we don't want to flake on. | ||
| expect(stdout).toBe("ok: true\n"); | ||
| expect(exitCode).toBe(0); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.