Skip to content

bundler: declare require inside a CommonJS wrapper whose body contains direct eval - #35581

Closed
robobun wants to merge 5 commits into
mainfrom
farm/dd47a4aa/cjs-wrapper-require-direct-eval
Closed

robobun wants to merge 5 commits into
mainfrom
farm/dd47a4aa/cjs-wrapper-require-direct-eval

Conversation

@robobun

@robobun robobun commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #14891

Problem

  • In ESM output, a direct eval in a bundled CommonJS file cannot see require: eval("require")(x) throws ReferenceError: require is not defined. @protobufjs/inquire@1.1.0 does this, and grpc-js then fails with Error: undefined undefined: undefined.
  • Cause: the wrapper closure is function(exports, module) (src/bundler/linker_context/generateCodeForFileInChunkJS.rs:577).

Fix

  • LinkerContext::direct_eval_require_ref selects a file: ESM output, a bun or node file, a direct eval, no own require. The linker then keeps the runtime __require and starts the wrapper body with var require = __require;.
  • Correct because a direct eval resolves names in its caller's scope.
  • Verified: test/bundler/bundler_cjs.test.ts (15 new cases, 9 fail on main), all of test/bundler/. Self-reviewed: 5 concerns raised, 5 addressed.

Background

  • A bundled CommonJS file runs in a closure passed to __commonJS. __require is a runtime export, one definition per target, emitted only in esm output.
  • Considered a require parameter on the closure: it changes every bundle. The prefix statement changes only files with a direct eval.

Downsides

  • Eval'd require now loads modules at run time. A --compile binary with inquire 1.1.0, started next to node_modules/long, throws on int64 decode. Bun 1.4.2 does the same with inquire 1.1.2.
  • Not covered, same output as main: browser files, --format=iife, ES module files, and a --target=node build with an HTML page.
  • Size: +27 bytes per affected file, plus __require if absent (37 bytes bun, 109 node). Other bundles are byte-identical. Release binary: .text +1,024 bytes.
Notes

Changes since the approved head 2c1ba3e

  • Rebased onto main 6c1eb06. The only conflict was test/bundler/bundler_cjs.test.ts. The src/ hunks of the first commit are byte-identical to 2c1ba3e.
  • The three later commits narrow the predicate (src/bundler/LinkerContext.rs:3330). A browser file gets no declaration. A --target=node build with a browser entry point (an HTML import or an HTML entry point) gets none. They add five test cases.
  • Two test comment lines now name inquire 1.1.0.

Tests

  • bun bd test test/bundler/bundler_cjs.test.ts: 77 pass.
  • The 15 new cases on bun 1.4.2 and on a build of main: 9 fail, 6 pass. The 6 pin output that must not change: node iife, a user-declared require, no direct eval, a browser build, and a node build with an HTML import or an HTML entry point.
  • All of test/bundler/ on release builds of main 6c1eb06 and of this branch: the 9 cases are the only difference. Two bytecode tests time out at 5 s on both builds in this container.
  • At the first commit, debug build, 13 suites (bundler_cjs2esm, bundler_bun, bundler_edgecase, bundler_npm, bundler_minify, bundler_splitting, bundler_string, esbuild/default, esbuild/splitting, esbuild/dce, esbuild/extra, esbuild/lower, transpiler/react-compiler): no assertion failure. 11 cases exceed the 5 s default under ASAN and pass with a longer timeout.

Behavior by output

Fixture: an ES module entry that imports a .cjs file with eval("typeof require") and eval("require")("buffer").

Output main This PR
--target=bun --format=esm "undefined", ReferenceError "function", the call works, +64 bytes
--target=node --format=esm "undefined", ReferenceError "function", the call works, +136 bytes
--target=browser, --format=iife, --format=cjs byte-identical to main
  • --splitting: a chunk that holds such a file imports __require from the chunk that holds the runtime (cjs/DirectEvalSeesRequireSplitting).
  • --minify: the name stays require. The parser keeps the symbol Unbound, and main pins module scope names of a CommonJS file with a direct eval.
  • A file that declares its own require gets no declaration.

Run-time require

Fixture: protobufjs@7.2.5 (protobufjs/minimal) with @protobufjs/inquire@1.1.0, --target=bun.

  • bun 1.4.2: finish() returns Uint8Array. util.Long is absent, so int64 decodes to a number.
  • This PR: finish() returns Buffer. protobufjs also calls inquire("long"). When long resolves at run time, int64 decodes to Long, as it does unbundled. When it does not resolve, int64 decodes to a number. Bun can auto-install long at run time when no node_modules directory exists.
  • inquire 1.1.2 on bun 1.4.2 gives the same results as this PR with 1.1.0.
  • bun build --compile, binary started in the project directory: require("long") returns the ES module namespace ({ default }), and int64 decode throws Module is not a constructor (evaluating 'new util.Long(...)'). bun 1.4.2 with inquire 1.1.2 does the same. The binary works in a directory with no node_modules/long. --compile-autoload-package-json makes it work in both.

HTML import builds

  • A server build with import page from "./index.html" bundles the scripts of the page for the browser. The runtime module is the server's, and each chunk prints every live part of it.
  • --target=bun: a server file with the eval gets the declaration, and the browser copy of the file gets none (cjs/DirectEvalInServerAndBrowserChunk). The browser chunk gains var __require = import.meta.require; (+37 bytes), which is inert in a browser.
  • --target=node: the runtime __require part starts with import { createRequire } from "node:module";, which a browser cannot load. Main prints it in the browser chunk when a server file has a dynamic require(name). This PR does not add a second way to reach that defect: a node build with a browser entry point gets no declaration, and its output is the same as on main. That covers an HTML import (cjs/DirectEvalNodeBuildWithHtmlImport) and an HTML file given as an entry point (cjs/DirectEvalNodeBuildWithHtmlEntryPoint).

Not covered

  • --target=browser: a browser has no require. The runtime __require there is a shim that throws Dynamic require of "x" is not supported. Code that hides require in an eval to detect Node would take the Node path with it. The output is the same as on main (cjs/DirectEvalBrowserTargetHasNoRequire).
  • --format=iife has no __require part. Node loads such a file as CommonJS, so require exists there. In Bun, eval("typeof require") is still "undefined".
  • A file that the parser keeps as an ES module is not wrapped by __commonJS. A direct eval there still has no require in the bundle. Unbundled Bun gives such a file a require when the file references the name: const r = require("node:path"); export const t = eval("typeof require"); console.log(t) prints function unbundled and undefined bundled.
  • __dirname and __filename in a direct eval are a separate parser change. Related to @prisma/internals fails when running a build #13194 and bundler: inject __dirname/__filename into CJS wrappers that use direct eval #35651. @prisma/internals@7.10.0 has eval(`require('path').join(__dirname, '..')`). With this PR its error changes from require is not defined to __dirname is not defined. One combined test case is wanted when both changes are merged.

Workarounds on bun 1.4.2

  • bun update @protobufjs/inquire moves 1.1.0 to 1.1.2, which has no eval.
  • --banner 'var require = import.meta.require;' for --target=bun.
  • --banner 'import { createRequire as __cr } from "node:module"; var require = __cr(import.meta.url);' for --target=node --format=esm.
  • globalThis.require ??= require; as the first line of a CommonJS entry.
  • --format=cjs.

The linker still declares require by default for two reasons. The failure is silent and far from its cause: inquire catches the error and returns null, and the symptom appears in grpc-js. The declaration is local to the one wrapped file, and a banner applies to the whole chunk.

Upstream

Measurements

  • Release builds of main 6c1eb06 and of this branch (672fe62) in one container, read with size: text 89,151,860 to 89,152,884 bytes (+1,024), .text 65,818,853 to 65,819,877 bytes (+1,024). The stripped file is 89,318,984 bytes in both.
  • Output: the declaration line is 27 bytes unminified. The __require definition is 37 bytes (bun) or 109 bytes (node), once per bundle, and only when the bundle did not have it.
  • 24 builds with no affected file (4 fixtures, 3 targets, with and without --minify) are byte-identical between the two release builds. So are the browser, iife and cjs builds of a fixture with an affected file.
  • Per build with no affected file: the predicate runs once per CommonJS-wrapped file at each of the two sites. It returns at the format check, the file target check or the contains_direct_eval check. The entry point scan runs only for a file with a direct eval and no require of its own. It allocates nothing. Instruction counts are not measured: valgrind and perf are not installed in this container.

Self-review

Five concerns were raised.

  1. A browser file in a server build got the server's __require. Addressed: a browser file gets no declaration.
  2. No test covered the browser target. Addressed by cjs/DirectEvalBrowserTargetHasNoRequire.
  3. The text cited three duplicates and the protobufjs result too broadly. Addressed: one issue on the first line, and the "Run-time require" section.
  4. ES module files are not covered. Addressed in "Not covered".
  5. Workarounds exist on released versions. Addressed in "Workarounds".

The review also asked for three follow-up issues. They are not filed: require("long") from a compiled executable returns the ES module namespace, require for a direct eval in an ES module file, and the runtime __require part in the browser chunk of a server build.

The automated review of the rebased head raised six points in two rounds. The later commits handle five: a node build with an HTML import, a node build with an HTML entry point, the browser target, a --splitting test, and a chunk count check. The sixth is the --compile case under Downsides.

Earlier revision

The first version of this PR added a third require parameter to the closure and a req argument to __commonJS. That changed the runtime helper in every bundle with a wrapped CommonJS file, and it made the __require part live in iife output, where it does not exist. Review asked for the prefix statement, which changes only the files that need it.


[review] gate passed · iteration 1 · 3 files touched

fails on main (without fix)
ASAN without fix: 7 FAILED
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/bundler/bundler_cjs.test.ts
bun test v1.4.0 (2c1ba3ed6)

test/bundler/bundler_cjs.test.ts:
(pass) bundler > cjs/__toESM_import_syntax_with_esModule [1558.67ms]
(pass) bundler > cjs/__toESM_import_syntax_without_esModule [806.55ms]
(pass) bundler > cjs/__toESM_import_syntax_function [793.09ms]
(pass) bundler > cjs/__toESM_import_syntax_primitive [909.99ms]
(pass) bundler > cjs/__toESM_import_syntax_named_and_default [893.34ms]
(pass) bundler > cjs/__toESM_import_syntax_namespace [613.59ms]
(pass) bundler > cjs/__toESM_target_node [1208.07ms]
(pass) bundler > cjs/__toESM_target_browser [710.53ms]
(pass) bundler > cjs/__toESM_target_bun [712.28ms]
(pass) bundler > cjs/__toESM_format_esm [793.92ms]
(pass) bundler > cjs/__toESM_format_cjs_with_import [685.80ms]
(pass) bundler > cjs/__toESM_mjs_reexport [739.84ms]
(pass) bundler > cjs/__toESM_mjs_reexport_with_esModule [739.41ms]
(pass) bundler > cjs/__toESM_deep_reexport_chain [641.10ms]
(pass) bundler > cjs/__toESM_reexport_with_rename [1251.80ms]
(pass) bundler > cjs/__toESM_default_
... (truncated)

release without fix: 7 FAILED
bun test v1.4.0-canary.1 (da3851e57)

test/bundler/bundler_cjs.test.ts:
(pass) bundler > cjs/__toESM_import_syntax_with_esModule [64.32ms]
(pass) bundler > cjs/__toESM_import_syntax_without_esModule [39.22ms]
(pass) bundler > cjs/__toESM_import_syntax_function [32.33ms]
(pass) bundler > cjs/__toESM_import_syntax_primitive [25.94ms]
(pass) bundler > cjs/__toESM_import_syntax_named_and_default [254.82ms]
(pass) bundler > cjs/__toESM_import_syntax_namespace [39.42ms]
(pass) bundler > cjs/__toESM_target_node [54.13ms]
(pass) bundler > cjs/__toESM_target_browser [85.96ms]
(pass) bundler > cjs/__toESM_target_bun [28.53ms]
(pass) bundler > cjs/__toESM_format_esm [69.92ms]
(pass) bundler > cjs/__toESM_format_cjs_with_import [35.49ms]
(pass) bundler > cjs/__toESM_mjs_reexport [72.69ms]
(pass) bundler > cjs/__toESM_mjs_reexport_with_esModule [103.76ms]
(pass) bundler > cjs/__toESM_deep_reexport_chain [147.88ms]
(pass) bundler > cjs/__toESM_reexport_with_rename [46.79ms]
(pass) bundler > cjs/__toESM_default_prop_no_esModule [45.84ms]
(pass) bundler > cjs/__toESM_mixed_import_styles [63.72ms]
(pass) bundler > cjs/__toESM_esModule_non_true [51.80ms]
(pass) bundler > cjs/__toESM_
... (truncated)
passes on PR (with fix)
ASAN with fix: all passed
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/bundler/bundler_cjs.test.ts
bun test v1.4.0 (2c1ba3ed6)

test/bundler/bundler_cjs.test.ts:
(pass) bundler > cjs/__toESM_import_syntax_with_esModule [1398.19ms]
(pass) bundler > cjs/__toESM_import_syntax_without_esModule [606.41ms]
(pass) bundler > cjs/__toESM_import_syntax_function [676.27ms]
(pass) bundler > cjs/__toESM_import_syntax_primitive [620.86ms]
(pass) bundler > cjs/__toESM_import_syntax_named_and_default [973.72ms]
(pass) bundler > cjs/__toESM_import_syntax_namespace [692.47ms]
(pass) bundler > cjs/__toESM_target_node [880.07ms]
(pass) bundler > cjs/__toESM_target_browser [1438.48ms]
(pass) bundler > cjs/__toESM_target_bun [666.12ms]
(pass) bundler > cjs/__toESM_format_esm [723.07ms]
(pass) bundler > cjs/__toESM_format_cjs_with_import [654.75ms]
(pass) bundler > cjs/__toESM_mjs_reexport [737.03ms]
(pass) bundler > cjs/__toESM_mjs_reexport_with_esModule [753.99ms]
(pass) bundler > cjs/__toESM_deep_reexport_chain [710.08ms]
(pass) bundler > cjs/__toESM_reexport_with_rename [1407.45ms]
(pass) bundler > cjs/__toESM_default_
... (truncated)

release with fix: all passed
$ bun scripts/build.ts --profile=release
[configured] bun-profile → bun (stripped)
  target       linux-x64-gnu
  build type   Release
  build dir    ./build/release
  revision     2c1ba3ed63
  features     baseline

22 deps, 107 codegen, 1176 objects in 4585ms

ninja: Entering directory `/workspace/bun/build/release'
[1/1238] gen bindgenv2
[2/1238] gen ErrorCode+*.h
[3/1238] gen JSBuffer.lut.h
Generating /workspace/bun/build/release/codegen/JSBuffer.lut.h from /workspace/bun/src/jsc/bindings/JSBuffer.cpp
[4/1238] fetch zlib
[zlib] up to date
[5/1238] fetch libjpeg-turbo
[libjpeg-turbo] up to date
[6/1238] fetch tinycc
[tinycc] up to date
[7/1237] gen ProcessBindingConstants.lut.h
Generating /workspace/bun/build/release/codegen/ProcessBindingConstants.lut.h from /workspace/bun/src/jsc/bindings/ProcessBindingConstants.cpp
[8/1237] subst deps/zlib/zconf.h
[9/1237] subst deps/zlib/zlib.h
[10/1237] gen ProcessBindingBuffer.lut.h
Generating /workspace/bun/build/release/codegen/ProcessBindingBuffer.lut.h from /workspace/bun/src/jsc/bindings/ProcessBindingBuffer.cpp
[11/1237] gen ProcessBindingFs.lut.h
Generating /workspace/bun/build/release/codegen/ProcessBindingFs.lut.
... (truncated)
diff hotspot
src/bundler/LinkerContext.rs                       |  24 +++
 .../linker_context/generateCodeForFileInChunkJS.rs |  33 ++++
 test/bundler/bundler_cjs.test.ts                   | 180 +++++++++++++++++++++
 3 files changed, 237 insertions(+)

gate history · 1 passed · 0 rejected · iteration 1

evidence per changed file
file                                                      reads  edits  tests
src/bundler/LinkerContext.rs                                 11     11      0
…/bundler/linker_context/generateCodeForFileInChunkJS.rs      8      7      0
test/bundler/bundler_cjs.test.ts                              5      9      0

@coderabbitai

coderabbitai Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 4 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
Commits

Reviewing files that changed from the base of the PR and between 672fe62 and 6a7f94a.

Files selected for processing: 3
  • src/bundler/LinkerContext.rs
  • src/bundler/linker_context/generateCodeForFileInChunkJS.rs
  • test/bundler/bundler_cjs.test.ts

No actionable comments were generated in the recent review.

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: oven-sh/bun/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 22a35747-ae79-4788-88c3-af6c29166464
📥 Commits

Reviewing files that changed from the base of the PR and between 142cef9 and 672fe62.

📒 Files selected for processing: 2
  • src/bundler/LinkerContext.rs
  • test/bundler/bundler_cjs.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

ESM CommonJS wrappers conditionally receive a local require binding from runtime __require when direct eval uses an unbound require. Bundler tests cover Bun and Node outputs, shadowing, browser targets, and split and HTML-import chunks.

Changes

CommonJS direct-eval require

Layer / File(s) Summary
Detect qualifying require references and import runtime require
src/bundler/LinkerContext.rs
The linker detects direct eval and an unbound require in qualifying ESM output. It excludes browser-target files and Node-target builds with any Browser-target entry point. Qualifying CommonJS wrappers use an imported runtime __require.
Bind require inside CommonJS wrappers
src/bundler/linker_context/generateCodeForFileInChunkJS.rs
The code generator adds var require = <runtime require>; only when both the direct-eval reference and runtime require reference are available.
Cover direct-eval require behavior
test/bundler/bundler_cjs.test.ts
Tests cover Bun and Node ESM output, minified and compiled output, dynamic and shadowed require, IIFE output, browser-target behavior, split chunks, and HTML-import chunks.

Suggested reviewers: jarred-sumner, alii

Priority: Normal

Severity of issue fixed: Medium

Merge Risk: Minimal · previous review 672fe

The change adds a local require binding only to CommonJS wrappers that use direct eval, and tests cover the main output configurations. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4
Passed Passed checks: 4 passed
Check name Status Explanation
Linked Issues check Passed [Issue #14891] The linker now selects ESM CommonJS wrappers that contain direct eval and no own require. The generated wrapper declares var require = __require;, so direct eval can resolve runti…
Out of Scope Changes check Passed The Rust changes implement the direct-eval require binding and limit it to affected CommonJS wrappers. The test changes verify the binding and prevent changes in unrelated output configurations. All…
Title check Passed The title clearly and concisely describes the main change: declaring require inside CommonJS wrappers that contain direct eval.
Description check Passed The description explains the problem, cause, fix, scope, limitations, and verification results. It does not use the template headings exactly, but it provides the required information in equivalent se…
  • 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.

@robobun

robobun commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 12:46 AM PT - Oct 27th, 2026

✅ @robobun, your commit 6a7f94aae3f6dab1915ac7e04e6a53c9b1811bc5 passed in Build #133470! 🎉


🧪   To try this PR locally:

bunx bun-pr 35581

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

bun-35581 --bun

@github-actions

Copy link
Copy Markdown
Contributor

Found 3 issues this PR may fix:

  1. Issue with @protobufjs/inquire when bundled with Bun #14891 - Directly reports @protobufjs/inquire's eval("quire".replace(/^/,"re")) pattern failing when bundled, the exact bug this PR fixes
  2. Bun.build fails with libraries that use @protobufjs/inquire@1.1.0 (used by protobufjs@7.5.4 / @valkey/valkey-glide@2.0.1) #22058 - Same root cause: Bun.build fails with libraries using @protobufjs/inquire@1.1.0 because require is unbound in the CJS wrapper
  3. bun build and firebase-admin: firestore not able to fetch a document when called inside an bundled executable. #19601 - firebase-admin Firestore fails when bundled because it depends on @grpc/grpc-js → protobufjs → @protobufjs/inquire, hitting the same unbound require in eval

If this is helpful, copy the block below into the PR description to auto-close these issues on merge.

Fixes #14891
Fixes #22058
Fixes #19601

🤖 Generated with Claude Code

Comment thread test/bundler/bundler_cjs.test.ts Resolved
Comment thread src/bundler/LinkerContext.rs Resolved
Comment thread src/bundler/LinkerContext.rs Resolved

@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.

Additional findings (outside current diff — PR may have been updated during review):

  • 🟡 src/bundler/LinkerContext.rs:3231-3243 — This gate (output_format != Cjs && contains_direct_eval) is looser than the one in generateCodeForFileInChunkJS.rs, which additionally requires module_scope.members["require"] to be Kind::Unbound. When a wrapped CJS module has a user-level var require = … (or let/const/function) plus direct eval, codegen sets needs_require_arg = false and emits a one-arg __commonJS call, but this block has already registered a use of __require on the wrapper part, so tree-shaking keeps it live and the output carries a dead var __require = import.meta.require (or, for target=node, an unused createRequire(import.meta.url) call). Mirror the Kind::Unbound check here — or better, hoist the decision into a single shared predicate — so the two sites agree.

    Extended reasoning...

    What the bug is

    The new require-binding feature is gated in two places, and the gates can diverge:

    • LinkerContext.rs:3231 registers a use of the runtime __require symbol on the CJS wrapper part whenever output_format != Cjs && module_scope.contains_direct_eval.
    • generateCodeForFileInChunkJS.rs:539-551 only emits the second __commonJS argument (and the third closure parameter) when, additionally, module_scope.members["require"] exists and its symbol kind is Kind::Unbound. If it is not Unbound — the code even comments this: "User declared their own var require; it already shadows whatever we would pass" — require_arg_ref becomes None and needs_require_arg = false.

    When the second gate says "no" while the first has already said "yes", the wrapper part carries a recorded use of __require that nothing in the printed output for that file consumes.

    The code path that triggers it

    declare_common_js_symbol (p.rs:4443-4472) is called for "require" with Kind::Unbound after hoist_symbols. If the user wrote var require = … at module scope, module_scope.members["require"] already holds the user's Hoisted symbol. The merge branch at p.rs:4443-4450 requires both sides to be Hoisted, but the incoming kind is Unbound, so it falls through and pushes the fresh Unbound ref onto module_scope.generated — leaving the user's non-Unbound symbol in members["require"]. The codegen lookup then hits the _ => None arm.

    The same divergence occurs for let require, const require, and function require declarations — none of those produce an Unbound member.

    Note that runtime_require_ref.is_some() is equivalent to output_format != Cjs (set in generateCompileResultForJSChunk.rs:141 / postProcessJSChunk.rs:89), so that half of the two gates already agrees; the divergence is purely the missing Kind::Unbound check on the LinkerContext side.

    Why existing code doesn't prevent it

    Nothing in LinkerContext.rs consults module_scope.members["require"] or the symbol table before calling generate_runtime_symbol_import_and_use. Tree-shaking runs after this registration and before codegen, so once the use is recorded the runtime __require part is kept live regardless of what codegen later decides to emit.

    Step-by-step proof

    Bundle to ESM (target: "bun" or target: "node", format: "esm"):

    // mod.cjs
    var require = globalThis.customRequire;   // user declares their own require
    module.exports = eval("1 + 1");            // direct eval
    1. output_format != Cjs and module_scope.contains_direct_eval == true → LinkerContext.rs:3231 registers a use of __require on the wrapper part.
    2. Tree-shaking marks the runtime __require part live.
    3. Codegen looks up module_scope.members["require"], finds the user's Hoisted symbol, hits _ => None, so needs_require_arg = false.
    4. The wrapper is printed as __commonJS(function(exports, module) { … }) — one argument, no __require passed.
    5. The chunk still contains var __require = import.meta.require; (target=bun) or import { createRequire } from "node:module"; var __require = createRequire(import.meta.url); (target=node) with no consumer from this call site.

    Impact

    No functional breakage — the bundle runs correctly. For target=bun it is a handful of dead bytes; for target=node it also runs createRequire(import.meta.url) at bundle load for nothing. The trigger (a top-level user require binding plus direct eval in a wrapped CJS module bundled to non-CJS output) is extremely obscure. This is a code-quality / "one source of truth" concern rather than a correctness bug.

    How to fix

    Mirror the Kind::Unbound check in LinkerContext.rs so the predicate matches codegen exactly — or, given the comment-cop bot has already flagged both blocks, hoist "does this wrapped CJS file need the require arg?" into a single shared helper on LinkerGraph/BundledAst and call it from both sites. That satisfies REVIEW.md's "One source of truth" and "Every line you add must be demonstrably live" without duplicating the lookup.

Comment thread src/bundler/LinkerContext.rs Resolved

@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.

All three of my earlier findings are now addressed — including the predicate-divergence nit on LinkerContext.rs, which the current diff already mirrors with the same Unbound-require member check as the emit site (that inline comment is stale). Deferring only because this changes the __commonJS runtime signature and the linker's wrapper codegen, which is worth a human look.

What was reviewed:

  • __commonJS signature change is additive; existing single-arg call sites keep req/mod undefined so the memoization slot still works, and cjs/NoRequireArgWithoutDirectEval pins the two-arg wrapper shape.
  • Emit-side and tree-shake-side guards now agree (contains_direct_eval + Unbound require scope member); the ast.require_ref-vs-polyfill hazard is covered by the new dynamic-require+eval tests, and runtime_require_ref.unwrap() is guarded by needs_require_arg.
  • Snapshot / sourcemap / filesize bumps are the expected +9-byte shift from the longer __commonJS helper text.
  • The other in-tree __commonJS helpers (src/js/node/wasi.ts, querystring.ts, internal/fs/glob.ts) are independent local copies, not the bundler runtime — correctly left alone.
Extended reasoning...

Overview

The PR threads a working require binding into the __commonJS closure when the wrapped CJS body contains direct eval, so eval("require") (protobufjs's inquire probe) resolves. Touches: src/runtime.js (__commonJS gains a req parameter), src/bundler/LinkerContext.rs (records a use of the runtime __require for the wrapper part), src/bundler/linker_context/generateCodeForFileInChunkJS.rs (emits the third require closure param and passes __require as the second __commonJS arg), plus 8 new itBundled cases and mechanical sourcemap/filesize snapshot bumps.

Prior review status

I previously flagged three issues: (1) 🔴 the third parameter used ast.require_ref, which under auto_polyfill_require is the Kind::Other __require symbol rather than the Unbound require — fixed by looking up module_scope.members["require"] and filtering on Kind::Unbound, with new DirectEvalWithDynamicRequire{Bun,NodeESM} tests covering it; (2) 🟡 the node-target test wasn't actually running under Node — fixed with runtime: "node"; (3) 🟡 the tree-shake-side guard in LinkerContext.rs didn't mirror the emit-side Unbound check — the current diff already includes that check, so the inline comment I left at 10:07 is stale (it raced with commits 8b38851e/1dc8873f/d57c752f). All three are addressed in the code as it stands.

Security risks

None. This is bundler output-shape only; no untrusted input parsing, no filesystem/network paths change.

Level of scrutiny

High — src/runtime.js's __commonJS is inlined into every bundle that wraps a CJS module, and generateCodeForFileInChunkJS / LinkerContext are core linker paths. The signature change is small and backward-compatible (verified: single-arg callers leave req undefined; mod is still the memoization slot), but it does add ~9 bytes to every bundle carrying the helper regardless of whether direct eval is present. That's a design trade-off (per REVIEW.md "API design" / "Performance" — a shared runtime helper is user-facing surface) a maintainer should sign off on rather than me.

Other factors

Test coverage is solid across the variant matrix (bun / bun+minify / node-esm-run-under-node / compile / dynamic-require+eval / require.resolve identity / negative tree-shake check). The comment-cop bot's multi-line-comment flags were addressed by the "trim comments" commits. No outstanding human reviewer comments.

@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.

All three earlier concerns are addressed: the wrapper parameter now uses the Unbound require scope member (not ast.require_ref), the tree-shaking guard in LinkerContext.rs mirrors the same Unbound check so the two sites can't diverge, and the node-target case runs under runtime: "node". Remaining inline comment is a test-hygiene nit only. Deferring to a human for final sign-off since this changes the __commonJS runtime signature and CJS-wrap codegen for every bundle.

Extended reasoning...

Overview

Threads a require binding into the __commonJS closure when the wrapped CJS body contains direct eval, so eval("require") (protobufjs's @protobufjs/inquire) resolves. Touches src/runtime.js (__commonJS gains a req parameter), generateCodeForFileInChunkJS.rs (emit third require param + second __commonJS arg), LinkerContext.rs (record __require use for tree-shaking), plus 8 new itBundled cases and mechanical sourcemap/filesize/snapshot updates.

Follow-up on prior review

The three issues I raised on earlier revisions are now fixed in the current diff:

  • The 🔴 ast.require_ref-may-be-__require-polyfill bug: emit side now looks up module_scope.members["require"] and filters on Kind::Unbound, and cjs/DirectEvalWithDynamicRequire{Bun,NodeESM} cover the dynamic-require + direct-eval combination with an onAfterBundle regex asserting the parameter is literally require.
  • The 🟡 predicate divergence: LinkerContext.rs now applies the identical contains_direct_eval && members["require"].kind == Unbound guard before recording the __require use, so the tree-shaking side and the emit side cannot disagree.
  • The 🟡 runtime: "node" omission: both node-target tests now pass runtime: "node".
    The comment-cop warnings were also resolved (comments trimmed to single lines).

Security risks

None. No user-controlled input reaches new parsing/allocation; the change is codegen shape only.

Level of scrutiny

High — this is bundler linker/codegen on the path every wrapped CJS module takes, and it changes the __commonJS runtime helper signature (visible in the react-compiler snapshot and the +9-byte filesize delta). The mechanism relies on renamer semantics for Kind::Unbound symbols under minify, and on two sites (LinkerContext vs generateCodeForFileInChunkJS) computing the same predicate. A prior attempt (#28885) was rejected on approach grounds, so a maintainer should confirm this design.

Other factors

I verified the __commonJS signature change is backward-compatible: the second parameter was previously an unused memoization slot never passed by generated code, so shifting mod to position 3 and inserting req is safe for the existing single-arg call sites. The other __commonJS definitions in src/js/ are unrelated built-in-module locals. The one open inline comment is a nit about extracting a shared dynamicRequireFiles fixture — no correctness impact.

Comment thread test/bundler/bundler_cjs.test.ts Resolved

@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.

All three rounds of my earlier feedback have been addressed and nothing new turned up this pass — the fix now looks correct and well-covered. Deferring to a human because this changes bundler codegen and the __commonJS runtime-helper signature (every CJS-wrapped bundle shifts, per the sourcemap/filesize snapshot updates), and it takes a different approach than the earlier #28885 attempt; a bundler maintainer should sign off on the shape.

What was reviewed:

  • __commonJS signature change: mod was only ever a memoization slot with no external callers passing it, so inserting req before it is safe.
  • runtime_require_ref provenance: confirmed it is the runtime __require symbol (generateCompileResultForJSChunk.rs:141-148) and .unwrap() is guarded by needs_require_arg.
  • The LinkerContext use-count guard and the emit-site guard now use the same Unbound-require-member predicate; the ast.require_ref-is-polyfill case is handled and tested (DirectEvalWithDynamicRequire*).
Extended reasoning...

Overview

Threads a working require binding into the __commonJS wrapper closure when the wrapped body contains direct eval, so that eval("require")(...) (the @protobufjs/inquire pattern pulled in by every gRPC / @google-cloud/* client) resolves instead of throwing ReferenceError. Touches src/runtime.js (__commonJS gains a req parameter), src/bundler/linker_context/generateCodeForFileInChunkJS.rs (adds the third closure param and second __commonJS call arg), src/bundler/LinkerContext.rs (records a use of __require so tree-shaking keeps it live), plus test additions in bundler_cjs.test.ts and mechanical snapshot/sourcemap-offset updates in bundler_edgecase.test.ts, bundler_npm.test.ts, and react-compiler.test.ts.snap.

Prior review status

I left three rounds of inline findings on earlier revisions (all now marked resolved), and each has been addressed in the current diff:

  • The wrapper param now uses the parser's Unbound require scope member rather than ast.require_ref (which can be the file-local __require polyfill), with new DirectEvalWithDynamicRequire{Bun,NodeESM} tests covering the mixed direct-eval + dynamic-require case.
  • The Node-target ESM tests now set runtime: "node" so they actually execute under Node.
  • The LinkerContext use-count predicate now mirrors the emit-site predicate (both check the Unbound scope member), so a user-shadowed require no longer leaves a dead __require prelude.
  • The two dynamic-require test fixtures were deduplicated into dynamicRequireFiles.
  • The comment-cop complaints about paragraph-length comments were resolved by trimming to single-line comments.

Security risks

None identified. This is bundler output-shape work; no auth/crypto/permissions/untrusted-input parsing is touched.

Level of scrutiny

High. src/runtime.js and the linker's CJS-wrapper codegen are on the hot path for every bundle that contains a CommonJS module — the 9-byte filesize delta and sourcemap column shifts in the snapshot updates show the blast radius. The approach (per-module parameter threading) also differs from the earlier #28885 attempt (setting globalThis.require), which is exactly the kind of design choice a maintainer should approve. The Unbound-symbol lookup is subtle enough that it took two revisions to get right.

Other factors

Test coverage is good: bun target, bun+minify, node ESM run under Node, --compile, require.resolve/identity via the eval'd handle, the dynamic-require + direct-eval mix, and a negative check that __require stays tree-shaken when there is no direct eval. I verified runtime_require_ref resolves to the runtime __require symbol and that the .unwrap() is guarded, and that no existing caller passes a second positional arg to __commonJS (the old mod was purely a memoization slot). Other in-tree __commonJS copies (src/js/internal/fs/glob.ts, src/js/node/wasi.ts, src/js/node/querystring.ts) are independent local helpers, not the bundler runtime, so they are correctly untouched.

@robobun

robobun commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator Author

Build #80748 ran bundler_cjs.test.ts, bundler_npm.test.ts, bundler_edgecase.test.ts and transpiler/react-compiler.test.ts cleanly on every lane that picked them up. The remaining red is infrastructure and unrelated to this diff:

  • :windows: x64 verify-baseline: static scan flags strcspn with an xbegin (RTM) instruction. This change touches only src/runtime.js, two Rust files in src/bundler/, and tests; none of that is compiled by MSVC or linked into strcspn. Likely a Windows CRT/toolchain change on the agent.
  • :darwin: 14 x64 test-bun: the Node test runner itself crashed with uv_os_get_passwd ENOENT at test 658/1098, after all bundler tests had already passed.
  • 22 test jobs expired / 2 build jobs timed out waiting for agents.
  • The five annotated test files are all marked [flaky] (passed on retry).

Ready for review.

@alii alii left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linker fix is right, the wrapper change is not the shape I'd land.

  • --target=node --format=iife bundles with an eval-containing CJS dep go from loading to throwing at load (LinkerContext.rs:3226).
  • Changing __commonJS touches every bundle with a wrapped CJS module; var require = __require; in the wrapper prefix fixes only the modules that need it and drops the three snapshot updates.
  • Needs a rebase either way; bundler_npm.test.ts is the conflict and has to be re-measured against main, not delta-patched.

Comment thread src/bundler/LinkerContext.rs Resolved
Comment thread src/runtime.js Resolved
Comment thread test/bundler/bundler_npm.test.ts Resolved
@robobun
robobun force-pushed the farm/dd47a4aa/cjs-wrapper-require-direct-eval branch from b77991f to debe29c Compare August 13, 2026 03:32
Comment thread src/bundler/LinkerContext.rs Resolved
@robobun
robobun force-pushed the farm/dd47a4aa/cjs-wrapper-require-direct-eval branch from debe29c to 2c1ba3e Compare August 13, 2026 03:34
@robobun robobun changed the title bundler: bind require in the CommonJS wrapper when the body contains direct eval bundler: declare require inside a CommonJS wrapper whose body contains direct eval Aug 13, 2026
@robobun

robobun commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator Author

Reworked per review and rebased onto main (2c1ba3e):

  • src/runtime.js is unchanged. The wrapper now gets var require = __require; through stmts.inside_wrapper_prefix, so only files with a direct eval differ from main; the react-compiler snapshot and the bundler_edgecase / bundler_npm offset updates are gone and those tests pass unmodified.
  • Both the tree-shaking site and the emit site use one predicate, LinkerContext::direct_eval_require_ref, which is None outside --format=esm (the only format with a __require part). New cjs/DirectEvalSeesRequireNodeIIFE pins that node+iife still has no __require and still prints Buffer under node.
  • The dead needs_require_arg || branches are gone with the parameter approach.
  • Two pre-existing bugs found on the way are filed separately rather than folded in here: the iife dynamic-require path printing an undefined __require, and the renamer renaming a user-declared require inside an eval-containing CJS file.

PR body updated to describe the current shape.

@alii alii left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ready to merge from this side. The new head is the inside_wrapper_prefix shape: runtime.js and the three snapshot files are untouched, both the liveness site and the emit site go through one esm-only predicate that reads the module scope's require member (always present, and replaced by the user's own symbol when the file declares one, so the Unbound check holds), the decl is a var appended after the directive, and CJS-wrapped files are always one contiguous part range so it is emitted once. Checked the three pin cases (node iife, shadowed require, typeof require) against main's binary; the seven remaining cases failing on main and passing here is per the PR's own run, not rebuilt here. One nit not worth a push: the body says 8 of 10 cases fail without the fix, it is 7 (NoRequireDeclWithoutDirectEval also passes both ways).

…s direct eval

A direct eval in a bundled CommonJS file cannot see `require` in ESM
output. The `__commonJS` closure has `exports` and `module` only.
@protobufjs/inquire 1.1.0 hides its require this way, so protobufjs
falls back to the plain Writer, serializers return Uint8Array, and
grpc-js fails with "undefined undefined: undefined".

When a wrapped CommonJS file contains a direct eval and does not declare
its own require, prepend `var require = __require;` to the wrapper body
(stmts.inside_wrapper_prefix) and mark the runtime __require part live.
LinkerContext::direct_eval_require_ref is the one predicate for both
sites. It reads the binding from the module scope, not ast.require_ref:
the parser points ast.require_ref at the __require polyfill when the
file also has a dynamic require().

The change applies to --format=esm only, the one format that emits the
__require part. A bundle without such a file is byte for byte unchanged.

Fixes #14891
A server build with an HTML import bundles the scripts of the page for
the browser, but the runtime `__require` is the server's. With the
previous commit, the browser copy of a CommonJS file with a direct eval
got `var require = __require;`. For --target=node that pulled
`import { createRequire } from "node:module"` into the browser chunk,
which a browser cannot load.

direct_eval_require_ref now returns None for a Target::Browser file when
the build target is server side. The server copy of the file keeps the
declaration. When only browser files contain the eval, the build output
is the same as on main.

Tests: the browser target (eval sees the `__require` shim), and two
HTML import builds.
@robobun
robobun force-pushed the farm/dd47a4aa/cjs-wrapper-require-direct-eval branch from 2c1ba3e to 7dc8b83 Compare October 11, 2026 16:12
@robobun

robobun commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator Author

Rebased onto main (6c1eb06). The branch conflicted only in test/bundler/bundler_cjs.test.ts.

  • First commit (e9dcbd9): the approved change. Its src/ hunks are byte-identical to 2c1ba3e.
  • The three later commits (7dc8b83, 142cef9, 672fe62) are new, and they are the part that needs a look. They narrow the one predicate. A browser file gets no declaration, so a --target=browser bundle is the same as on main. A --target=node build with a browser entry point (an HTML import or an HTML entry point) gets none, because its browser chunk would import node:module. Five test cases cover the browser target, --splitting, and three HTML builds.
  • The PR body is rewritten. 9 of the 15 new cases fail without the change. The earlier text said 8 of 10, and the correct number was 7. The body also lists what an eval'd require now does at run time and what is not covered.

Reproduction:

printf 'console.log(eval("typeof require"));' > index.cjs
bun build --target=bun index.cjs --outfile=out.js && bun out.js

bun 1.4.2 and main print undefined. This branch prints function.

@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.

Beyond the inline findings, I also checked that the new var require = __require; prefix cannot print at chunk top level: it uses the same inside_wrapper_prefix slot as the existing "use strict" directive, and a __commonJS-wrapped file's wrapper part is live whenever any of its parts are. The --format=iife gap was also looked at — there is no __require part in that format, and the PR text names it as out of scope.

Extended reasoning...

The change adds a direct_eval_require_ref predicate in src/bundler/LinkerContext.rs, marks the runtime __require live for matching CJS-wrapped files, and prepends var require = __require; inside the wrapper in src/bundler/linker_context/generateCodeForFileInChunkJS.rs, with 13 new itBundled cases. It touches bundler output semantics (what a direct eval can reach) but no auth, crypto, or input-parsing surface. The approach changed since the last human review (closure parameter replaced by a prefix statement; src/runtime.js is no longer touched), and a CHANGES_REQUESTED review from a maintainer remains the latest human state, so approval is not appropriate; the inline findings cover the behavior tradeoffs a maintainer must weigh.

Findings marked 🟡 are optional suggestions and need no follow-up push.

Comment thread src/bundler/LinkerContext.rs Resolved
Comment thread src/bundler/LinkerContext.rs Resolved
Comment thread test/bundler/bundler_cjs.test.ts Resolved
Two cases from review:

- A browser has no `require`. In a --target=browser build the eval'd
  `require` was the runtime shim, which throws `Dynamic require of "x"
  is not supported`. Code that hides `require` in an eval to detect Node
  then took the Node path. A browser file now gets no declaration, in a
  browser build and in a server build. Its output is the same as on
  main.
- A --target=node build with an HTML import prints the runtime
  `__require` part in the browser chunk too, and that part imports
  `node:module`. Such a build now gets no declaration, as --format=iife
  already does.

Tests: the browser target, --splitting, and two HTML import builds.

@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.

Findings marked 🟡 are optional suggestions and need no follow-up push.

Comment thread src/bundler/LinkerContext.rs Resolved
Comment thread test/bundler/bundler_cjs.test.ts Resolved
Comment thread src/bundler/LinkerContext.rs Resolved
The previous commit excluded a --target=node build with an HTML import.
An HTML file given as an entry point beside a server entry has the same
browser chunk and no HTML import record, so that build still printed
the node `__require` part (`import ... from "node:module"`) in the page
script.

The predicate now checks the entry points: a node build with a browser
entry point gets no declaration. The check runs only for a file that
already has a direct eval and no `require` of its own.

Tests: a node build with an HTML entry point. The bun HTML import case
now also checks that the browser chunk exists.
@robobun
robobun force-pushed the farm/dd47a4aa/cjs-wrapper-require-direct-eval branch from cc7e9fa to 672fe62 Compare October 15, 2026 14:38

@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.

I re-reviewed the latest push (672fe62) and found no bugs; since this changes linker codegen and an earlier human review requested changes on the previous approach, a bundler maintainer's look is still worthwhile.

What was reviewed:

  • The new entry-point gate in direct_eval_require_ref (src/bundler/LinkerContext.rs): both an HTML entry point (bundle_v2.rs:3578-3592) and an HTML import (bundle_v2.rs:7324-7328) push the HTML source as a Browser-target entry, so the node/browser-chunk case I raised on the prior push is now covered by one predicate.
  • The __require use recorded at the wrapper part and the var require = __require; prefix only fire together: both call the same predicate, and runtime_require_ts_ref is Some for every non-CJS output format.
  • cjs/DirectEvalInServerAndBrowserChunk now asserts the chunk count before the filter, so the browser-chunk negative check can no longer pass vacuously.
Extended reasoning...

The diff adds a predicate in src/bundler/LinkerContext.rs plus a var require = __require; prefix statement in src/bundler/linker_context/generateCodeForFileInChunkJS.rs for wrapped CommonJS files containing direct eval, and ~370 lines of itBundled tests. It touches no security-sensitive surface (no auth, injection, or data exposure), but it changes bundler output and runtime behaviour of eval'd require for bun and node targets. The latest commit addressed both inline findings from the previous run and the hunt ran dry, yet an outstanding CHANGES_REQUESTED review from a human maintainer on an earlier version of this PR has not been superseded by an approval, and the change is non-trivial linker code, so approval is not appropriate.

@robobun

robobun commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator Author

Merged main (8a0e49e) into this branch.

  • One conflict, in src/bundler/LinkerContext.rs. This PR adds direct_eval_require_ref directly above get_source. Main (98a4365, Fix use-after-free of BuildMessage.position.file: real lifetimes for Source, Path, ImportRecord and resolve results #46834) deleted the doc comment of get_source and changed its return type to &Source<'a>. The resolution keeps the new function of this PR and the get_source of main.
  • The added and removed lines of the PR are the same as before the merge. Only the context of that hunk changed.
  • cargo check --workspace passes on the merged tree (Linux x64).
  • Test, on a local debug build that contains this merge and the main merges of eight other open PRs, with --timeout 30000: test/bundler/bundler_cjs.test.ts (77 pass).

@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.

Code review found no issues

No high-confidence issues detected in this change.

@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as stale: this PR conflicts with main, and its last commit is from 2026-10-27.

The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR.

@robobun robobun closed this Nov 17, 2026
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.

Issue with @protobufjs/inquire when bundled with Bun

2 participants