Skip to content

Commit 5bc01b3

Browse files
cliffhallclaude
andcommitted
fix(filesystem): treat a file in the way the same on every platform
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 704bd35 commit 5bc01b3

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

‎src/filesystem/__tests__/pinned-directory.test.ts‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,22 @@ describe("createDirectoryInPlace", () => {
330330
).toThrow(expect.objectContaining({ code: "EEXIST" }));
331331
});
332332

333+
it("fails with EEXIST when chdir reports a file in the way as ENOENT (Windows)", () => {
334+
fs.writeFileSync(path.join(dir, "file"), "");
335+
const realChdir = process.chdir.bind(process);
336+
vi.spyOn(process, "chdir").mockImplementation((directory) => {
337+
if (directory === "file") {
338+
throw Object.assign(new Error("ENOENT: no such file or directory"), {
339+
code: "ENOENT",
340+
});
341+
}
342+
realChdir(directory);
343+
});
344+
expect(() => createDirectoryInPlace(path.join(dir, "file"))).toThrow(
345+
expect.objectContaining({ code: "EEXIST" }),
346+
);
347+
});
348+
333349
it.skipIf(!symlinks)("refuses a parent that resolves somewhere else", () => {
334350
fs.symlinkSync(outside, path.join(dir, "link"));
335351
expect(() =>
@@ -351,15 +367,19 @@ describe("createDirectoryInPlace", () => {
351367
expect(fs.statSync(path.join(dir, nfc, "sub")).isDirectory()).toBe(true);
352368
});
353369

354-
it("refuses a step down that lands outside the anchor", () => {
355-
const realChdir = process.chdir.bind(process);
356-
vi.spyOn(process, "chdir").mockImplementation((directory) => {
357-
realChdir(directory === "a" ? outside : directory);
358-
});
359-
expect(() => createDirectoryInPlace(path.join(dir, "a"))).toThrow(
360-
"Access denied",
361-
);
362-
});
370+
// Windows skips the getcwd check by design; the test below covers that path.
371+
it.skipIf(process.platform === "win32")(
372+
"refuses a step down that lands outside the anchor",
373+
() => {
374+
const realChdir = process.chdir.bind(process);
375+
vi.spyOn(process, "chdir").mockImplementation((directory) => {
376+
realChdir(directory === "a" ? outside : directory);
377+
});
378+
expect(() => createDirectoryInPlace(path.join(dir, "a"))).toThrow(
379+
"Access denied",
380+
);
381+
},
382+
);
363383

364384
it("steps down without the getcwd check on Windows", () => {
365385
Object.defineProperty(process, "platform", { value: "win32" });

‎src/filesystem/pinned-directory.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ export function createDirectoryInPlace(directory: string): void {
301301
try {
302302
process.chdir(name);
303303
} catch (error) {
304-
if (inTheWay && (error as NodeJS.ErrnoException).code === "ENOTDIR") {
304+
// A file in the way: POSIX reports ENOTDIR, Windows ENOENT.
305+
const code = (error as NodeJS.ErrnoException).code;
306+
if (inTheWay && (code === "ENOTDIR" || code === "ENOENT")) {
305307
throw inTheWay;
306308
}
307309
throw error;

0 commit comments

Comments
 (0)