Skip to content

Commit e56cd6b

Browse files
anandgupta42claude
andcommitted
fix(drivers): load a driver from the location the failing runtime named
A globally installed CLI (`npm install -g`, package tree under `/usr/lib/node_modules/altimate-code`) run from an unrelated working directory could not load any warehouse driver — 8 of 8 trials on a cold VM: DuckDB driver found at duckdb but failed to load: ENOENT: no such file or directory, open '<cwd>/usr/lib/node_modules/altimate-code/node_modules/duckdb/package.json' The file exists at that path without the `<cwd>` prefix. Ambient resolution concatenated the working directory onto an already-absolute path, which is a runtime behaviour we cannot change — but it names the correct location in the error, and that is better evidence than anything we can infer. `searchRootsFromError` now mines the paths an ambient failure quotes, repairs a concatenated working directory, and searches the enclosing `node_modules` first. `repairCwdPrefixedPath` is deliberately conservative: it fires only when the named path is absent, is genuinely prefixed by the working directory, and the de-prefixed remainder exists on disk, so a legitimately nested `<cwd>/node_modules/…` is left alone. The message was the second half of the bug. `found at duckdb` named the bare specifier as though it were a location, so a package that had never been located anywhere read as a load failure at a known path — which is why this was diagnosed as a load bug rather than a search-coverage one. Both sites that produced it now say what actually happened: the default module resolution failed, here is where we looked, and here is the on-disk copy we also tried. Verified in the shape it occurs in, not in unit tests alone: a probe compiled with the production `Bun.build` options (`external`, `autoloadPackageJson`), laid out as a real `npm install -g` tree with a real `npm install duckdb`, run from an unrelated cwd with no `NODE_PATH` and no `ALTIMATE_BIN_DIR`. before: 8/8 fail, reproducing the reported message verbatim after: 8/8 load the driver Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VqnuBDGkh1ZT65Ti7e6DHZ
1 parent 212f454 commit e56cd6b

2 files changed

Lines changed: 230 additions & 5 deletions

File tree

‎packages/drivers/src/resolve.ts‎

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,60 @@ function nodeModulesUpward(start: string): string[] {
164164
return found
165165
}
166166

167+
/**
168+
* Repair a path that had the working directory concatenated onto an
169+
* already-absolute path, e.g. `<cwd>/usr/lib/node_modules/…` for a package
170+
* that really lives at `/usr/lib/node_modules/…`.
171+
*
172+
* Observed from a globally installed CLI (`npm install -g`, package tree under
173+
* `/usr/lib/node_modules/altimate-code`) run from an unrelated directory: the
174+
* runtime's own resolution reported
175+
* `ENOENT … open '<cwd>/usr/lib/node_modules/altimate-code/node_modules/duckdb/package.json'`
176+
* while the file existed at that path without the `<cwd>` prefix.
177+
*
178+
* Deliberately conservative — it fires only when the named path is absent, is
179+
* genuinely prefixed by the working directory, and the de-prefixed remainder
180+
* exists. That last check is what keeps a legitimately nested
181+
* `<cwd>/node_modules/…` from being mangled.
182+
*/
183+
export function repairCwdPrefixedPath(candidate: string, cwd = process.cwd()): string | undefined {
184+
if (!candidate || fs.existsSync(candidate)) return undefined
185+
if (!candidate.startsWith(cwd + path.sep)) return undefined
186+
const remainder = candidate.slice(cwd.length)
187+
if (!path.isAbsolute(remainder)) return undefined
188+
return fs.existsSync(remainder) ? remainder : undefined
189+
}
190+
191+
/**
192+
* `node_modules` directories named by an error the runtime raised while trying
193+
* to resolve a package itself.
194+
*
195+
* When ambient resolution fails it often names the exact absolute location it
196+
* was reaching for. That location is better evidence than anything we can
197+
* infer, so it is worth searching — after repairing a concatenated working
198+
* directory, which is the failure this exists for. Returns roots only when they
199+
* exist on disk, so a nonsense path contributes nothing.
200+
*/
201+
export function searchRootsFromError(error: unknown): string[] {
202+
const message = error instanceof Error ? error.message : String(error)
203+
const roots: string[] = []
204+
// Absolute paths the runtime quoted, POSIX or Windows.
205+
for (const match of message.matchAll(/['"`]((?:\/|[A-Za-z]:[\\/])[^'"`\n]+)['"`]/g)) {
206+
const named = match[1]
207+
if (!named) continue
208+
for (const candidate of [named, repairCwdPrefixedPath(named)]) {
209+
if (!candidate) continue
210+
// Walk back to the enclosing node_modules directory.
211+
const marker = `${path.sep}node_modules${path.sep}`
212+
const at = candidate.lastIndexOf(marker)
213+
if (at === -1) continue
214+
const root = candidate.slice(0, at + marker.length - 1)
215+
if (isDirectory(root) && !roots.includes(root)) roots.push(root)
216+
}
217+
}
218+
return roots
219+
}
220+
167221
/**
168222
* Directories to search for an optional SDK, most specific first.
169223
*
@@ -325,12 +379,19 @@ export async function loadOptionalDriver(
325379
return await importer(specifier)
326380
} catch (ambientError) {
327381
const ambientBroken = !isModuleNotFound(ambientError, specifier)
328-
const roots = driverSearchRoots()
382+
// Search the location the runtime itself named first. When ambient
383+
// resolution fails it frequently quotes the absolute path it was reaching
384+
// for, and that beats anything we can infer — including the case where it
385+
// concatenated the working directory onto an already-absolute path.
386+
const roots = [...searchRootsFromError(ambientError), ...driverSearchRoots()]
329387
const resolved = resolveOptionalPackage(specifier, roots)
330388

331389
if (!resolved) {
332-
// A broken ambient copy is a load failure, not an absence.
333-
if (ambientBroken) throw loadFailure(driver, specifier, ambientError)
390+
// Nothing was found anywhere, so there is no location to name. Saying
391+
// "found at <specifier>" here would report the bare specifier as though
392+
// it were a place on disk, which reads as a load failure at a known path
393+
// and sends the reader looking for a file that was never located.
394+
if (ambientBroken) throw ambientLoadFailure(driver, ambientError, describeSearched(roots))
334395
throw new DriverNotInstalledError(driver, DRIVER_PACKAGES[driver], roots)
335396
}
336397

@@ -340,7 +401,18 @@ export async function loadOptionalDriver(
340401
// On disk but will not load — a half-installed copy, or a native addon
341402
// built for another platform. When an ambient copy was also broken,
342403
// report that one: it is the copy the runtime would normally pick.
343-
throw loadFailure(driver, ambientBroken ? specifier : resolved, ambientBroken ? ambientError : loadError)
404+
if (ambientBroken) {
405+
// Both copies are unusable. Lead with the ambient one — it is the copy
406+
// the runtime would normally pick — but name the on-disk path we also
407+
// tried, rather than passing the specifier off as a location.
408+
throw ambientLoadFailure(
409+
driver,
410+
ambientError,
411+
`A copy at ${resolved} was also tried and failed to load: ` +
412+
(loadError instanceof Error ? loadError.message : String(loadError)),
413+
)
414+
}
415+
throw loadFailure(driver, resolved, loadError)
344416
}
345417
}
346418
}
@@ -376,6 +448,29 @@ function loadFailure(driver: DriverName, where: string, error: unknown): Error {
376448
)
377449
}
378450

451+
/**
452+
* The ambient import failed for a reason other than "not installed".
453+
*
454+
* We do not know where that copy lives — the runtime resolved it, not us — so
455+
* the specifier must not be reported as though it were a location on disk. The
456+
* previous wording, `found at duckdb but failed to load`, read as a load
457+
* failure at a known path for a package that had in fact never been located,
458+
* and sent readers looking for a file that was not there.
459+
*/
460+
function ambientLoadFailure(driver: DriverName, error: unknown, detail: string): Error {
461+
return new Error(
462+
`${DRIVER_LABELS[driver]} driver failed to load from the default module resolution: ` +
463+
`${error instanceof Error ? error.message : String(error)}\n${detail}`,
464+
)
465+
}
466+
467+
function describeSearched(searched: readonly string[]): string {
468+
return searched.length
469+
? `It was not found in any searchable location. Searched ${searched.length} ` +
470+
`location${searched.length === 1 ? "" : "s"}: ${searched.join(", ")}`
471+
: "It was not found in any searchable location, and no driver directory exists yet."
472+
}
473+
379474
/**
380475
* Import an optional package that is not a warehouse driver, returning
381476
* undefined when it is unavailable.
@@ -389,7 +484,10 @@ export async function loadOptionalPackage(specifier: string): Promise<any | unde
389484
return await import(/* @vite-ignore */ specifier)
390485
} catch (ambientError) {
391486
if (!isModuleNotFound(ambientError, specifier)) throw ambientError
392-
const resolved = resolveOptionalPackage(specifier)
487+
const resolved = resolveOptionalPackage(specifier, [
488+
...searchRootsFromError(ambientError),
489+
...driverSearchRoots(),
490+
])
393491
if (!resolved) return undefined
394492
return await import(/* @vite-ignore */ pathToFileURL(resolved).href)
395493
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { afterAll, beforeAll, describe, expect, test } from "bun:test"
2+
import fs from "node:fs"
3+
import os from "node:os"
4+
import path from "node:path"
5+
6+
import {
7+
loadOptionalDriver,
8+
repairCwdPrefixedPath,
9+
searchRootsFromError,
10+
} from "../src/resolve"
11+
12+
// A globally installed CLI (`npm install -g`, package tree under
13+
// /usr/lib/node_modules/altimate-code) run from an unrelated directory reported:
14+
//
15+
// DuckDB driver found at duckdb but failed to load: ENOENT … open
16+
// '<cwd>/usr/lib/node_modules/altimate-code/node_modules/duckdb/package.json'
17+
//
18+
// Two defects in one line. The runtime concatenated the working directory onto
19+
// an already-absolute path, and our message then named the bare specifier as
20+
// though it were a location on disk — so it read as "found it, could not load
21+
// it" when in fact nothing had been found at all.
22+
//
23+
// Real directories on disk here, because the whole mechanism is path existence.
24+
25+
let root = ""
26+
let pkgRoot = ""
27+
let nodeModules = ""
28+
29+
/** Build a minimal but real installed package tree. */
30+
function writePackage(dir: string, name: string, main: string, body: string) {
31+
const pkgDir = path.join(dir, name)
32+
fs.mkdirSync(pkgDir, { recursive: true })
33+
fs.writeFileSync(path.join(pkgDir, "package.json"), JSON.stringify({ name, version: "1.0.0", main }))
34+
fs.writeFileSync(path.join(pkgDir, main), body)
35+
return pkgDir
36+
}
37+
38+
describe("cwd concatenated onto an absolute path", () => {
39+
beforeAll(() => {
40+
root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "resolve-cwd-")))
41+
pkgRoot = path.join(root, "lib", "node_modules", "altimate-code")
42+
nodeModules = path.join(pkgRoot, "node_modules")
43+
fs.mkdirSync(nodeModules, { recursive: true })
44+
writePackage(nodeModules, "duckdb", "index.js", "module.exports = { Database: function () {} }\n")
45+
})
46+
47+
afterAll(() => {
48+
if (root) fs.rmSync(root, { recursive: true, force: true })
49+
})
50+
51+
test("repairs a path whose absolute form exists", () => {
52+
const real = path.join(nodeModules, "duckdb", "package.json")
53+
const mangled = process.cwd() + real
54+
expect(repairCwdPrefixedPath(mangled)).toBe(real)
55+
})
56+
57+
test("leaves a path that exists alone", () => {
58+
const real = path.join(nodeModules, "duckdb", "package.json")
59+
expect(repairCwdPrefixedPath(real)).toBeUndefined()
60+
})
61+
62+
test("leaves a legitimately nested path under cwd alone", () => {
63+
// `<cwd>/node_modules/x` de-prefixes to `/node_modules/x`, which does not
64+
// exist — so the repair must decline rather than invent a root.
65+
const nested = path.join(process.cwd(), "node_modules", "definitely-not-here", "package.json")
66+
expect(repairCwdPrefixedPath(nested)).toBeUndefined()
67+
})
68+
69+
test("declines when the de-prefixed path does not exist either", () => {
70+
const mangled = process.cwd() + path.join(root, "nope", "package.json")
71+
expect(repairCwdPrefixedPath(mangled)).toBeUndefined()
72+
})
73+
74+
test("harvests the node_modules root the runtime named", () => {
75+
const mangled = process.cwd() + path.join(nodeModules, "duckdb", "package.json")
76+
const error = Object.assign(new Error(`ENOENT: no such file or directory, open '${mangled}'`), {
77+
code: "ENOENT",
78+
})
79+
expect(searchRootsFromError(error)).toContain(nodeModules)
80+
})
81+
82+
test("harvests nothing from an error naming no usable path", () => {
83+
expect(searchRootsFromError(new Error("something went wrong"))).toEqual([])
84+
expect(searchRootsFromError(new Error("open '/no/such/place/pkg/package.json'"))).toEqual([])
85+
})
86+
87+
test("loads the driver from the location the failing runtime named", async () => {
88+
// Reproduces the reported failure exactly: ambient resolution throws ENOENT
89+
// naming the correct absolute path with cwd concatenated on, and nothing
90+
// else on this machine can see that tree.
91+
const mangled = process.cwd() + path.join(nodeModules, "duckdb", "package.json")
92+
const ambient = Object.assign(new Error(`ENOENT: no such file or directory, open '${mangled}'`), {
93+
code: "ENOENT",
94+
})
95+
96+
let call = 0
97+
const importer = async (spec: string) => {
98+
call++
99+
if (call === 1) throw ambient
100+
return await import(/* @vite-ignore */ spec)
101+
}
102+
103+
const mod: any = await loadOptionalDriver("duckdb", "duckdb", importer)
104+
const duckdb = mod.default ?? mod
105+
expect(typeof duckdb.Database).toBe("function")
106+
})
107+
108+
test("does not claim a location when nothing was found", async () => {
109+
const ambient = Object.assign(new Error("ENOENT: no such file or directory, open '/nowhere/pkg.json'"), {
110+
code: "ENOENT",
111+
})
112+
const importer = async () => {
113+
throw ambient
114+
}
115+
116+
let message = ""
117+
try {
118+
await loadOptionalDriver("duckdb", "duckdb", importer)
119+
} catch (e) {
120+
message = e instanceof Error ? e.message : String(e)
121+
}
122+
// The old text was `found at duckdb but failed to load: …`, naming the bare
123+
// specifier as a place on disk.
124+
expect(message).not.toContain("found at duckdb")
125+
expect(message).toContain("failed to load from the default module resolution")
126+
})
127+
})

0 commit comments

Comments
 (0)