Skip to content

Commit 836faa7

Browse files
anandgupta42claude
andcommitted
fix(drivers): fail closed on an unavailable cwd, and keep a root path absolute
Two narrow fixes from the latest review round. **The workspace exclusion failed open when `process.cwd()` was unavailable.** `isWorkspaceRoot` returned false with no working directory to compare against, so every `node_modules` root an error happened to name was admitted — turning the one case where the process cannot see its own filesystem into the case with no boundary at all. It now fails closed: no cwd means no harvested roots. This was introduced by the guard added two commits ago, which is exactly the sort of thing a fail-open default hides. **A filesystem root lost its leading separator when deriving the lock path.** `installLockPath` stripped every trailing separator, so `/` became the relative `.lock` and `C:\` the drive-relative `C:.lock`. Two processes started from different working directories would then take different locks while installing into the same directory, which is the concurrent-npm mutation the lock exists to prevent. Roots are now left intact; trailing separators are still stripped everywhere else so `<dir>/` and `<dir>` agree on one lock. Gates: typecheck 13/13 (forced); marker check ok; lint 5903 warnings and 1 error, identical to `main`'s baseline; `packages/drivers` 258 pass; `test/altimate` 4226 pass. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VqnuBDGkh1ZT65Ti7e6DHZ
1 parent 53a196d commit 836faa7

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎packages/drivers/src/resolve.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,11 @@ function isWorkspaceRoot(root: string, scope: { cwd: string | undefined; ancesto
274274
// slip past a purely lexical comparison.
275275
const resolved = realPath(root)
276276
if (scope.ancestors.includes(resolved)) return true
277-
if (!scope.cwd) return false
277+
// Fail closed. With no working directory there is nothing to compare against,
278+
// and treating that as "not workspace content" would admit every root an error
279+
// happens to name — turning the one case where the process cannot see its own
280+
// filesystem into the case with no boundary at all.
281+
if (!scope.cwd) return true
278282
const rel = path.relative(scope.cwd, resolved)
279283
return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel))
280284
}
@@ -1011,7 +1015,14 @@ const installsInFlight = new Map<string, Promise<InstallResult>>()
10111015
* never sees it as stray package content.
10121016
*/
10131017
export function installLockPath(dir: string): string {
1014-
return `${dir.replace(/[\\/]+$/, "")}.lock`
1018+
const trimmed = dir.replace(/[\\/]+$/, "")
1019+
// Trailing separators are stripped so `<dir>/` and `<dir>` agree on one lock.
1020+
// A filesystem root is the exception: stripping there destroys the root — "/"
1021+
// would become the *relative* ".lock", and "C:\" the drive-relative "C:.lock"
1022+
// — so two processes started from different working directories would take
1023+
// different locks while installing into the same place.
1024+
const base = trimmed === "" || /^[A-Za-z]:$/.test(trimmed) ? dir : trimmed
1025+
return `${base}.lock`
10151026
}
10161027

10171028
/**

‎packages/drivers/test/install-lock.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ afterEach(() => {
3939
if (dir) fs.rmSync(dir, { recursive: true, force: true })
4040
})
4141

42+
describe("install lock path", () => {
43+
test("agrees on one lock whether or not the directory has a trailing separator", () => {
44+
expect(installLockPath("/a/drivers/")).toBe(installLockPath("/a/drivers"))
45+
})
46+
47+
test("keeps a filesystem root intact", () => {
48+
// Stripping the separator from a root turns the lock into a *relative*
49+
// path, so processes with different working directories would take
50+
// different locks while installing into the same directory.
51+
expect(installLockPath("/")).toBe("/.lock")
52+
expect(path.isAbsolute(installLockPath("/"))).toBe(true)
53+
expect(installLockPath("C:\\")).toBe("C:\\.lock")
54+
})
55+
})
56+
4257
describe("cross-process install lock", () => {
4358
test("excludes concurrent processes from the critical section", async () => {
4459
const target = path.join(dir, "drivers")

‎packages/drivers/test/resolve-cwd-prefix.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,25 @@ describe("harvested roots respect the workspace boundary", () => {
338338
}
339339
})
340340

341+
test("harvests nothing at all when the working directory cannot be established", () => {
342+
// Fail closed. With no cwd there is nothing to compare a root against, and
343+
// treating that as "not workspace content" would admit every root an error
344+
// names — turning the one case where the process cannot see its own
345+
// filesystem into the case with no boundary at all.
346+
const realCwd = process.cwd.bind(process)
347+
process.cwd = () => {
348+
throw Object.assign(new Error("ENOENT: no such file or directory, uv_cwd"), { code: "ENOENT" })
349+
}
350+
try {
351+
const error = new Error(
352+
`ENOENT: no such file or directory, open '${path.join(outsideModules, "duckdb", "package.json")}'`,
353+
)
354+
expect(searchRootsFromError(error)).toEqual([])
355+
} finally {
356+
process.cwd = realCwd
357+
}
358+
})
359+
341360
test("still harvests a root outside the workspace", () => {
342361
// The exclusion must not swallow the case the harvesting exists for.
343362
workspace = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "resolve-ws-")))

0 commit comments

Comments
 (0)