Skip to content

Commit d810bdb

Browse files
fix(cli): init no longer crashes copying into a folder at a Windows drive root (#7220)
* fix(cli): init no longer crashes copying into a folder at a Windows drive root * fix(cli): keep copyDirSync's comment to one line
1 parent d452a6f commit d810bdb

2 files changed

Lines changed: 57 additions & 8 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import { join, parse } from "node:path";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
import { copyDirSync } from "./copyDir.js";
6+
7+
const made: string[] = [];
8+
afterEach(() => {
9+
for (const dir of made.splice(0)) rmSync(dir, { recursive: true, force: true });
10+
});
11+
12+
function source(): string {
13+
const dir = mkdtempSync(join(tmpdir(), "copydir-src-"));
14+
made.push(dir);
15+
mkdirSync(join(dir, "compositions", "café"), { recursive: true });
16+
writeFileSync(join(dir, "index.html"), "<html></html>");
17+
writeFileSync(join(dir, "compositions", "café", "scène.html"), "<div></div>");
18+
return dir;
19+
}
20+
21+
describe("copyDirSync", () => {
22+
it("copies every file, non-ASCII names included, into a folder that already holds others", () => {
23+
const dest = mkdtempSync(join(tmpdir(), "copydir-dest-"));
24+
made.push(dest);
25+
writeFileSync(join(dest, "hyperframes.json"), "{}");
26+
copyDirSync(source(), dest);
27+
expect(readFileSync(join(dest, "index.html"), "utf8")).toBe("<html></html>");
28+
expect(readFileSync(join(dest, "compositions", "café", "scène.html"), "utf8")).toBe(
29+
"<div></div>",
30+
);
31+
expect(readFileSync(join(dest, "hyperframes.json"), "utf8")).toBe("{}");
32+
});
33+
34+
// windows-latest: fs.cpSync into this folder ends the process with 0xC0000409, so this test fails without the fix.
35+
it.skipIf(process.platform !== "win32")(
36+
"copies into an existing folder directly under a drive root",
37+
() => {
38+
const dest = join(parse(tmpdir()).root, `copydir-root-${process.pid}-${Date.now()}`);
39+
mkdirSync(dest);
40+
made.push(dest);
41+
copyDirSync(source(), dest);
42+
expect(readFileSync(join(dest, "index.html"), "utf8")).toBe("<html></html>");
43+
},
44+
);
45+
});

‎packages/cli/src/utils/copyDir.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { cpSync, type CopySyncOptions } from "node:fs";
1+
import { copyFileSync, mkdirSync, readdirSync, readlinkSync, symlinkSync } from "node:fs";
2+
import { join } from "node:path";
23

3-
export function copyDirSync(
4-
src: string,
5-
dest: string,
6-
options: Omit<CopySyncOptions, "recursive" | "filter"> = {},
7-
): void {
8-
// Any filter keeps Node 22 off its native copy, which writes nothing under a non-ASCII path on Windows.
9-
cpSync(src, dest, { ...options, recursive: true, filter: () => true });
4+
/** Not fs.cpSync: Node dies (0xC0000409) copying into an existing folder directly under a Windows drive root. */
5+
export function copyDirSync(src: string, dest: string): void {
6+
mkdirSync(dest, { recursive: true });
7+
for (const entry of readdirSync(src, { withFileTypes: true })) {
8+
const from = join(src, entry.name);
9+
const to = join(dest, entry.name);
10+
if (entry.isDirectory()) copyDirSync(from, to);
11+
else if (entry.isSymbolicLink()) symlinkSync(readlinkSync(from), to);
12+
else copyFileSync(from, to);
13+
}
1014
}

0 commit comments

Comments
 (0)