Skip to content

Commit 98e93a1

Browse files
aryguptclaude
andauthored
fix(json-provider): resolve DUMP_DIR when Turbopack strips import.meta.dirname (#374)
`import.meta.dirname` is undefined in workspace-package code that Next.js 16 + Turbopack bundles into server chunks, so the `resolve(import.meta.dirname, '..')` in getStore() threw ERR_INVALID_ARG_TYPE on every API route that touched the JSON dump path (Option A in the README). Pages rendered but every /api/v1/* call 500'd. `import.meta.url` is preserved correctly by Turbopack and still points at the original source file, so fall back to `dirname(fileURLToPath(import.meta.url))` when `import.meta.dirname` is unavailable. Behavior is identical for tsx, plain Node ESM, and vitest, where `import.meta.dirname` is defined and the ?? short- circuits. No production impact: Vercel uses DATABASE_READONLY_URL, never DUMP_DIR, so getStore() is unreachable in prod. Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
1 parent e7d022d commit 98e93a1

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

packages/db/src/json-provider.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
*/
1010

1111
import { existsSync, readFileSync } from 'node:fs';
12-
import { resolve } from 'node:path';
12+
import { dirname, resolve } from 'node:path';
13+
import { fileURLToPath } from 'node:url';
1314

1415
import type { BenchmarkRow } from './queries/benchmarks.js';
1516
import type { EvalRow } from './queries/evaluations.js';
@@ -168,7 +169,11 @@ function getStore(): Store {
168169

169170
// Resolve relative paths from the monorepo root (packages/db/../../), not CWD,
170171
// since Next.js runs from packages/app/ but .env paths are repo-root-relative.
171-
const pkgRoot = resolve(import.meta.dirname, '..');
172+
// import.meta.dirname is undefined under Turbopack bundling — derive from
173+
// import.meta.url instead, which Turbopack rewrites to a usable file URL.
174+
// oxlint-disable-next-line unicorn/prefer-import-meta-properties -- import.meta.dirname is undefined under Turbopack; this is the fallback.
175+
const thisDir = import.meta.dirname ?? dirname(fileURLToPath(import.meta.url));
176+
const pkgRoot = resolve(thisDir, '..');
172177
const monoRoot = resolve(pkgRoot, '../..');
173178
const resolvedDir = existsSync(resolve(dir)) ? resolve(dir) : resolve(monoRoot, dir);
174179

0 commit comments

Comments
 (0)