Repository navigation
Conversation
Bun's const-inlining optimization was replacing variable references with literal values inside function bodies, changing the observable result of Function.prototype.toString(). This broke patterns that rely on toString() + eval() (serialization for workers, RPC, etc.). Only applies to the runtime transpiler (non-bundler mode) to preserve toString() fidelity. The bundler path is unchanged since users explicitly opt into minification there. Closes #12710 Co-Authored-By: Claude <noreply@anthropic.com>
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. Comment |
|
✅ dc3ba — Looks good! Reviewed 3 files across |
|
Nooo |
Summary
Function.prototype.toString()Root Cause
Bun's transpiler has a const-inlining optimization that replaces references to
constvariables with their literal values. Theconst_valuesmap was global to the parser and did not respect function scope boundaries — soconst hi = "hi"; log(() => console.log(hi))was transpiled tolog(() => console.log("hi")), changing whatFunction.prototype.toString()returns.When the serialized function was
eval()'d in another module, the inlined literal meant the eval succeeded when it should have thrownReferenceError, since the original variablehidoesn't exist in the eval scope.Fix
Save and clear the
const_valuesmap when entering function/arrow bodies (invisitFuncande_arrow), and restore it when leaving. This prevents outer-scope const values from being inlined into function bodies. Const values declared inside a function body are still inlined within that function.The fix is gated on
!p.options.bundleso the bundler path (where users explicitly opt into minification viaminifySyntax) is unaffected.Test plan
bun bd test test/regression/issue/12710.test.ts— 5/5 passUSE_SYSTEM_BUN=1 bun test test/regression/issue/12710.test.ts— 1 fail (confirms test catches the bug)bun bd test test/bundler/esbuild/default.test.ts— 150 pass (was 149 before fix due toConstDeclRemovedIfReferencedBeforeAllUses)bun bd test test/bundler/bundler_minify.test.ts— 32 passbun bd test test/bundler/bundler_string.test.ts— 59 passbun bd test test/bundler/esbuild/ts.test.ts— 57 passCloses #12710
🤖 Generated with Claude Code