PER-9666: avoid require binding name that breaks the packaged binary#2306
PER-9666: avoid require binding name that breaks the packaged binary#2306rishigupta1599 wants to merge 4 commits into
require binding name that breaks the packaged binary#2306Conversation
…ed binary
lockfileDiff.js and smartsnap.js bound `const require = createRequire(import.meta.url)`.
When transpiled to CommonJS for the pkg binary, two Babel transforms collide:
preset-env renames the local `require` to `_require` to avoid shadowing CJS's
built-in require, and transform-import-meta expands `import.meta.url` into a
`require('url')...` call whose `require` is then ALSO renamed to `_require`.
The result is `_require(...)` inside its own initializer, so the binary crashes
on load with `TypeError: _require is not a function`.
Rename the binding to `cjsRequire` so the import-meta-emitted `require('url')`
stays the real CJS require. Pure rename; no behavior change.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude Code PR ReviewPR: #2306 • Head: 7be2263 • Reviewers: stack:code-reviewer SummaryRenames the Review Table
Findings
Verdict: PASS |
require binding name that breaks the packaged binaryrequire binding name that breaks the packaged binary
|
Add UTs - Rest LGTM |
Claude Code PR ReviewPR: #2306 • Head: 1bc95ea • Reviewers: stack:code-reviewer SummaryFixes a Review Table
Findings
Verdict: PASS |
Problem
The packaged
pkgbinary crashes on startup (./percy --version):Root cause
lockfileDiff.jsandsmartsnap.js(both new this release) bind:When these ESM files are transpiled to CommonJS for the binary, two Babel transforms collide:
@babel/preset-envrenames the localrequire→_requireto avoid shadowing CJS's built-inrequire.babel-plugin-transform-import-metaexpandsimport.meta.urlinto arequire('url').pathToFileURL(__filename)...call — and that emittedrequiregets renamed to_requiretoo, because the scope now has a_requirebinding.The transpiled line becomes:
_require('url')runs inside_require's own initializer, while_requireis stillundefined→TypeError: _require is not a function.(No other file hits this —
core/src/api.jsusescreateRequire(url)inline and never binds it to a name calledrequire.)Fix
Rename the binding to
cjsRequirein both files (plus usages). The import-meta-emittedrequire('url')then stays the real CJSrequire, and the transpiled output is correct:Pure rename — no behavior change.
Verification
BABEL_ENV=devCJS transpile, then confirmed the fixed output executes past thecreateRequireline.cli-commandunit specs pass.🤖 Generated with Claude Code