Commit d3c0966
fix: use file-local createRequire for relative lazy requires in src/ (#1008)
## Problem
After the Bun→Node migration, `pnpm cli` and any direct `pnpm tsx
src/bin.ts` invocation were broken:
```
Fatal: Error: Cannot find module '../telemetry.js'
Require stack:
- /Users/bete/code/init/cli/package.json
```
Every single invocation crashed before executing any command.
## Root cause
`require-shim.mjs` installs a global `require()` anchored at
`package.json` (project root) so that `node:*` builtins work in ESM.
This has two failure modes:
1. **Relative paths resolve wrong** — `require("../telemetry.js")` from
`src/lib/db/` resolves relative to the project root, not the calling
file: `/project-root/../telemetry.js` → does not exist
2. **Shim not loaded at all** — when invoking via `pnpm tsx
/path/to/bin.ts` from outside the `cli/` directory, the `--import
./script/require-shim.mjs` in the `pnpm tsx` script is never applied, so
`require` doesn't exist at all in ESM scope
The shim comment said relative `require()` calls in `src/` "don't
execute during tsx script runs." That was true when `pnpm tsx` was only
used for `script/*.ts` utilities — but `pnpm cli` also uses tsx for
`src/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:
| File | Symptom |
|------|---------|
| `src/lib/db/sqlite.ts` | **Fatal crash** — module-level require, fires
on import |
| `src/lib/db/index.ts` | **Fatal crash** — `require("../telemetry.js")`
wrong path |
| `src/lib/list-command.ts` | Silent — subcommand interception (`sentry
issue view` tip) never worked |
| `src/lib/telemetry.ts` | Silent — DB path fell back to hardcoded
`~/.sentry/cli.db`; Sentry reporter never attached |
| `src/lib/db/schema.ts` | Silent — schema auto-repair never ran |
| `src/lib/db/migration.ts` | Silent — JSON→SQLite migration silently
skipped |
| `src/lib/custom-ca.ts` | Silent — `setDefaultCACertificates` always
`undefined` on Node 24+ |
| `src/lib/logger.ts` | Silent — Sentry consola reporter never attached
|
| `src/lib/init/ui/ink-ui.ts` | Silent — SEA binary detection always
false |
## Fix
Replace every bare `require()` in `src/` with a file-local
`createRequire(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 in `telemetry.test.ts`
that were permanently skipped on Node, and updated two stale `Bun.*`
references in comments.
## Test plan
```
# Before (from any directory)
pnpm tsx /path/to/cli/src/bin.ts init
→ Fatal: ReferenceError: require is not defined
# Before (from cli/ dir)
pnpm cli issue view
→ Fatal: Cannot find module '../telemetry.js'
# After (both invocations)
→ Expected argument for issue (DB init works, routing works)
```
---------
Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Burak Yigit Kaya <byk@sentry.io>1 parent 5aa10dc commit d3c0966
15 files changed
Lines changed: 96 additions & 131 deletions
File tree
- script
- src/lib
- db
- init/ui
- test
- commands/cli
- e2e
- lib
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
470 | 470 | | |
471 | 471 | | |
472 | 472 | | |
473 | | - | |
| 473 | + | |
474 | 474 | | |
475 | 475 | | |
476 | 476 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
15 | 22 | | |
16 | 23 | | |
17 | 24 | | |
18 | 25 | | |
19 | 26 | | |
20 | | - | |
| 27 | + | |
21 | 28 | | |
22 | 29 | | |
23 | 30 | | |
24 | 31 | | |
25 | 32 | | |
26 | 33 | | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
| 22 | + | |
| 23 | + | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
| |||
30 | 33 | | |
31 | 34 | | |
32 | 35 | | |
33 | | - | |
| 36 | + | |
34 | 37 | | |
35 | 38 | | |
36 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
10 | 14 | | |
11 | 15 | | |
12 | 16 | | |
| |||
30 | 34 | | |
31 | 35 | | |
32 | 36 | | |
33 | | - | |
| 37 | + | |
34 | 38 | | |
35 | 39 | | |
36 | 40 | | |
| |||
107 | 111 | | |
108 | 112 | | |
109 | 113 | | |
| 114 | + | |
110 | 115 | | |
111 | 116 | | |
112 | 117 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
7 | 11 | | |
8 | 12 | | |
9 | 13 | | |
| |||
34 | 38 | | |
35 | 39 | | |
36 | 40 | | |
37 | | - | |
| 41 | + | |
38 | 42 | | |
39 | 43 | | |
40 | 44 | | |
41 | 45 | | |
42 | 46 | | |
43 | 47 | | |
44 | | - | |
| 48 | + | |
45 | 49 | | |
46 | 50 | | |
47 | 51 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| 20 | + | |
| 21 | + | |
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
| |||
671 | 674 | | |
672 | 675 | | |
673 | 676 | | |
| 677 | + | |
674 | 678 | | |
675 | 679 | | |
676 | 680 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
| 14 | + | |
| 15 | + | |
13 | 16 | | |
14 | 17 | | |
15 | 18 | | |
| |||
58 | 61 | | |
59 | 62 | | |
60 | 63 | | |
61 | | - | |
| 64 | + | |
62 | 65 | | |
63 | 66 | | |
64 | 67 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
50 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
51 | 55 | | |
52 | 56 | | |
53 | 57 | | |
| |||
235 | 239 | | |
236 | 240 | | |
237 | 241 | | |
238 | | - | |
| 242 | + | |
239 | 243 | | |
240 | 244 | | |
241 | 245 | | |
| |||
245 | 249 | | |
246 | 250 | | |
247 | 251 | | |
248 | | - | |
| 252 | + | |
249 | 253 | | |
250 | 254 | | |
251 | 255 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
18 | 22 | | |
19 | 23 | | |
20 | 24 | | |
| |||
414 | 418 | | |
415 | 419 | | |
416 | 420 | | |
| 421 | + | |
417 | 422 | | |
418 | 423 | | |
419 | 424 | | |
| |||
0 commit comments