From d810bdbb27ed5fe1d428ec5389dbd6861318386a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Thu, 5 Nov 2026 07:00:55 +0000 Subject: [PATCH] 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 --- packages/cli/src/utils/copyDir.test.ts | 45 ++++++++++++++++++++++++++ packages/cli/src/utils/copyDir.ts | 20 +++++++----- 2 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 packages/cli/src/utils/copyDir.test.ts diff --git a/packages/cli/src/utils/copyDir.test.ts b/packages/cli/src/utils/copyDir.test.ts new file mode 100644 index 0000000000..fcd37905f3 --- /dev/null +++ b/packages/cli/src/utils/copyDir.test.ts @@ -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"), ""); + writeFileSync(join(dir, "compositions", "café", "scène.html"), "
"); + 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(""); + expect(readFileSync(join(dest, "compositions", "café", "scène.html"), "utf8")).toBe( + "
", + ); + 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(""); + }, + ); +}); diff --git a/packages/cli/src/utils/copyDir.ts b/packages/cli/src/utils/copyDir.ts index 47f890e4e8..5c98a527c9 100644 --- a/packages/cli/src/utils/copyDir.ts +++ b/packages/cli/src/utils/copyDir.ts @@ -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 = {}, -): 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); + } }