fix: use file-local createRequire for relative lazy requires in src/#1008
Merged
Conversation
Contributor
|
Contributor
Codecov Results 📊✅ Patch coverage is 100.00%. Project has 4235 uncovered lines. Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 81.86% 81.87% +0.01%
==========================================
Files 328 328 —
Lines 23351 23359 +8
Branches 15114 15114 —
==========================================
+ Hits 19115 19124 +9
- Misses 4236 4235 -1
- Partials 1621 1621 —Generated by Codecov Action |
1df193c to
e75f2ba
Compare
The require-shim.mjs anchors globalThis.require at the project root
(package.json), which works for node:* builtins but breaks relative
paths — require("../telemetry.js") resolves to /project-root/../
instead of relative to the calling file.
The shim assumed relative require() calls in src/ only ran during
script/*.ts tsx runs, but pnpm cli (which also uses tsx) hits both
db/index.ts and list-command.ts at runtime. db/index.ts crashed hard;
list-command.ts was silently swallowed by try/catch, leaving
getSubcommandsForRoute() always returning an empty map.
Fix: replace the global require() with a file-local createRequire
anchored to import.meta.url in each affected file. Update the shim
comment to reflect the constraint.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
e75f2ba to
95897c6
Compare
…ile import hook
- Keep _require (createRequire) for node builtins and npm packages
- Use bare require() for relative paths (../telemetry.js, ./index.js, etc.)
so esbuild resolves them at bundle time
- Add registerHooks loader for with { type: 'file' } in tsx dev mode
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 01b8d18. Configure here.
BYK
added a commit
that referenced
this pull request
May 22, 2026
Documents the critical esbuild bundling rule that caused two post-migration bugs (#1008, #1009): **esbuild only statically resolves bare `require()` calls.** Any aliased require (`_require`, `localRequire`, etc.) passes through as-is into the bundle and breaks at runtime when used with relative paths. Adds a reference table and 4 key rules to AGENTS.md so AI agents and contributors avoid this pattern. Filed #1010 for the companion testing improvements (deeper smoke tests for binary + npm bundle).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Problem
After the Bun→Node migration,
pnpm cliand any directpnpm tsx src/bin.tsinvocation were broken:Every single invocation crashed before executing any command.
Root cause
require-shim.mjsinstalls a globalrequire()anchored atpackage.json(project root) so thatnode:*builtins work in ESM. This has two failure modes:require("../telemetry.js")fromsrc/lib/db/resolves relative to the project root, not the calling file:/project-root/../telemetry.js→ does not existpnpm tsx /path/to/bin.tsfrom outside thecli/directory, the--import ./script/require-shim.mjsin thepnpm tsxscript is never applied, sorequiredoesn't exist at all in ESM scopeThe shim comment said relative
require()calls insrc/"don't execute during tsx script runs." That was true whenpnpm tsxwas only used forscript/*.tsutilities — butpnpm clialso uses tsx forsrc/bin.ts, which hits all these paths on every invocation.What was broken
A full audit found 13 bare
require()calls across 9 files — two crashed hard, the rest failed silently:src/lib/db/sqlite.tssrc/lib/db/index.tsrequire("../telemetry.js")wrong pathsrc/lib/list-command.tssentry issue viewtip) never workedsrc/lib/telemetry.ts~/.sentry/cli.db; Sentry reporter never attachedsrc/lib/db/schema.tssrc/lib/db/migration.tssrc/lib/custom-ca.tssetDefaultCACertificatesalwaysundefinedon Node 24+src/lib/logger.tssrc/lib/init/ui/ink-ui.tsFix
Replace every bare
require()insrc/with a file-localcreateRequire(import.meta.url). This resolves paths relative to the calling file regardless of how the CLI was invoked or where the shim is anchored. Also corrects the shim comment.Also removed 3 dead
skipIf(!isBunSqlite)tests intelemetry.test.tsthat were permanently skipped on Node, and updated two staleBun.*references in comments.Test plan