Skip to content

Commit 1df193c

Browse files
betegonclaude
andcommitted
fix: use file-local createRequire for relative lazy requires in src/
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>
1 parent 48200d5 commit 1df193c

5 files changed

Lines changed: 23 additions & 8 deletions

File tree

script/require-shim.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
*
99
* The `require` function is anchored at the project root (package.json) so
1010
* that `node:*` builtins and npm package requires resolve correctly. Note
11-
* that relative `require("./foo.js")` calls will resolve from the project
12-
* root, not from the calling file — this is acceptable because all relative
13-
* `require()` calls in `src/` are behind runtime-only code paths (DB init,
14-
* telemetry) that don't execute during tsx script runs.
11+
* that relative `require("./foo.js")` calls resolve from the project root,
12+
* not from the calling file. Files in `src/` that use lazy relative requires
13+
* must use a file-local `createRequire(import.meta.url)` instead of relying
14+
* on this global shim.
1515
*
1616
* Usage: NODE_OPTIONS="--import ./script/require-shim.mjs" tsx script/...
1717
* Or in package.json scripts via the `pnpm tsx` alias.

src/lib/db/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
*/
66

77
import { chmodSync, mkdirSync } from "node:fs";
8+
import { createRequire } from "node:module";
89
import { join } from "node:path";
910
import { getEnv } from "../env.js";
11+
12+
const _require = createRequire(import.meta.url);
13+
1014
import { migrateFromJson } from "./migration.js";
1115
import { initSchema, runMigrations } from "./schema.js";
1216
import { Database } from "./sqlite.js";
@@ -107,7 +111,7 @@ export function getDatabase(): Database {
107111
if (getEnv().SENTRY_CLI_NO_TELEMETRY === "1") {
108112
db = rawDb;
109113
} else {
110-
const { createTracedDatabase } = require("../telemetry.js") as {
114+
const { createTracedDatabase } = _require("../telemetry.js") as {
111115
createTracedDatabase: (d: Database) => Database;
112116
};
113117
db = createTracedDatabase(rawDb);

src/lib/db/schema.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
* - Migration checks
1212
*/
1313

14+
import { createRequire } from "node:module";
1415
import { getEnv } from "../env.js";
1516
import { stringifyUnknown } from "../errors.js";
1617
import { logger } from "../logger.js";
1718
import type { Database } from "./sqlite.js";
1819

20+
const _require = createRequire(import.meta.url);
21+
1922
export const CURRENT_SCHEMA_VERSION = 16;
2023

2124
/** Environment variable to disable auto-repair */
@@ -671,7 +674,7 @@ export function tryRepairAndRetry<T>(
671674
let repairSucceeded = false;
672675
try {
673676
// Dynamic imports to avoid circular dependencies with db/index.js
674-
const { getRawDatabase } = require("./index.js") as {
677+
const { getRawDatabase } = _require("./index.js") as {
675678
getRawDatabase: () => Database;
676679
};
677680

src/lib/list-command.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
* buildOrgListCommand
1414
*/
1515

16+
import { createRequire } from "node:module";
1617
import type { Aliases, Command, CommandContext } from "@stricli/core";
1718
import type { SentryContext } from "../context.js";
19+
20+
const _require = createRequire(import.meta.url);
21+
1822
import { parseOrgProjectArg } from "./arg-parsing.js";
1923
import { buildCommand, numberParser } from "./command.js";
2024
import { disableOrgCache } from "./db/regions.js";
@@ -414,7 +418,7 @@ function getSubcommandsForRoute(routeName: string): Set<string> {
414418
_subcommandsByRoute = new Map();
415419

416420
try {
417-
const { routes } = require("../app.js") as {
421+
const { routes } = _require("../app.js") as {
418422
routes: { getAllEntries: () => readonly RouteEntry[] };
419423
};
420424

src/lib/telemetry.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
*/
1111

1212
import { chmodSync, statSync } from "node:fs";
13+
import { createRequire } from "node:module";
1314
// biome-ignore lint/performance/noNamespaceImport: Sentry SDK recommends namespace import
1415
import * as Sentry from "@sentry/node-core/light";
16+
17+
const _require = createRequire(import.meta.url);
18+
1519
import { isMusl } from "./binary.js";
1620
import {
1721
CLI_VERSION,
@@ -1017,7 +1021,7 @@ const noop = (): void => {};
10171021
/** Resolves the database path, falling back to a default if the import fails. */
10181022
function resolveDbPath(): string {
10191023
try {
1020-
const { getDbPath } = require("./db/index.js") as {
1024+
const { getDbPath } = _require("./db/index.js") as {
10211025
getDbPath: () => string;
10221026
};
10231027
return getDbPath();

0 commit comments

Comments
 (0)