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
This commit is contained in:
Miguel Ángel
2026-11-05 07:00:55 +00:00
committed by GitHub
parent d452a6fad4
commit d810bdbb27
2 changed files with 57 additions and 8 deletions
+45
View File
@@ -0,0 +1,45 @@
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, parse } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { copyDirSync } from "./copyDir.js";
const made: string[] = [];
afterEach(() => {
for (const dir of made.splice(0)) rmSync(dir, { recursive: true, force: true });
});
function source(): string {
const dir = mkdtempSync(join(tmpdir(), "copydir-src-"));
made.push(dir);
mkdirSync(join(dir, "compositions", "café"), { recursive: true });
writeFileSync(join(dir, "index.html"), "<html></html>");
writeFileSync(join(dir, "compositions", "café", "scène.html"), "<div></div>");
return dir;
}
describe("copyDirSync", () => {
it("copies every file, non-ASCII names included, into a folder that already holds others", () => {
const dest = mkdtempSync(join(tmpdir(), "copydir-dest-"));
made.push(dest);
writeFileSync(join(dest, "hyperframes.json"), "{}");
copyDirSync(source(), dest);
expect(readFileSync(join(dest, "index.html"), "utf8")).toBe("<html></html>");
expect(readFileSync(join(dest, "compositions", "café", "scène.html"), "utf8")).toBe(
"<div></div>",
);
expect(readFileSync(join(dest, "hyperframes.json"), "utf8")).toBe("{}");
});
// windows-latest: fs.cpSync into this folder ends the process with 0xC0000409, so this test fails without the fix.
it.skipIf(process.platform !== "win32")(
"copies into an existing folder directly under a drive root",
() => {
const dest = join(parse(tmpdir()).root, `copydir-root-${process.pid}-${Date.now()}`);
mkdirSync(dest);
made.push(dest);
copyDirSync(source(), dest);
expect(readFileSync(join(dest, "index.html"), "utf8")).toBe("<html></html>");
},
);
});
+12 -8
View File
@@ -1,10 +1,14 @@
import { cpSync, type CopySyncOptions } from "node:fs";
import { copyFileSync, mkdirSync, readdirSync, readlinkSync, symlinkSync } from "node:fs";
import { join } from "node:path";
export function copyDirSync(
src: string,
dest: string,
options: Omit<CopySyncOptions, "recursive" | "filter"> = {},
): void {
// Any filter keeps Node 22 off its native copy, which writes nothing under a non-ASCII path on Windows.
cpSync(src, dest, { ...options, recursive: true, filter: () => true });
/** Not fs.cpSync: Node dies (0xC0000409) copying into an existing folder directly under a Windows drive root. */
export function copyDirSync(src: string, dest: string): void {
mkdirSync(dest, { recursive: true });
for (const entry of readdirSync(src, { withFileTypes: true })) {
const from = join(src, entry.name);
const to = join(dest, entry.name);
if (entry.isDirectory()) copyDirSync(from, to);
else if (entry.isSymbolicLink()) symlinkSync(readlinkSync(from), to);
else copyFileSync(from, to);
}
}