From 50ce9fb514fefb544213666af5f1daa87a9a25bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 4 Nov 2026 10:47:39 +0000 Subject: [PATCH] fix(cli): an output in a drive root (D:\) no longer fails before it is written on Windows (#7151) * fix(cli): an output in a drive root (D:\) no longer fails before it is written on Windows * fix(cli): keep the output folder helper's reason in its test * fix(cli): make the output folder only when nothing is there, as the producer does * fix(cli): keep the output folder's directory check and fix the stat mock that hid isDirectory --- packages/cli/src/cloud/download.ts | 5 +- packages/cli/src/commands/batchRender.ts | 7 +-- packages/cli/src/commands/compare.ts | 3 +- packages/cli/src/commands/grade-compare.ts | 4 +- packages/cli/src/commands/measure/extract.ts | 5 +- packages/cli/src/commands/measure/probe.ts | 4 +- packages/cli/src/commands/motionShot.ts | 5 +- packages/cli/src/commands/render/execute.ts | 5 +- packages/cli/src/commands/templates.ts | 3 +- packages/cli/src/fileIds.windows.test.ts | 8 ++-- packages/cli/src/heygen/generate.ts | 13 ++--- packages/cli/src/tts/synthesize.ts | 3 +- packages/cli/src/utils/outputFolder.test.ts | 50 ++++++++++++++++++++ packages/cli/src/utils/outputFolder.ts | 7 +++ 14 files changed, 90 insertions(+), 32 deletions(-) create mode 100644 packages/cli/src/utils/outputFolder.test.ts create mode 100644 packages/cli/src/utils/outputFolder.ts diff --git a/packages/cli/src/cloud/download.ts b/packages/cli/src/cloud/download.ts index 7cafb43804..aac21dc149 100644 --- a/packages/cli/src/cloud/download.ts +++ b/packages/cli/src/cloud/download.ts @@ -13,7 +13,8 @@ * existing destination is replaced only after the download succeeds. */ -import { chmodSync, mkdirSync, mkdtempSync, renameSync, rmSync, statSync } from "node:fs"; +import { chmodSync, mkdtempSync, renameSync, rmSync, statSync } from "node:fs"; +import { makeOutputFolder } from "../utils/outputFolder.js"; import { open } from "node:fs/promises"; import { dirname, join } from "node:path"; import { setTimeout as sleep } from "node:timers/promises"; @@ -73,7 +74,7 @@ export async function downloadToFile( const ranged = first.status === 206; const total = ranged ? contentRangeTotal(first) : contentLength(first); - mkdirSync(dirname(destPath), { recursive: true }); + makeOutputFolder(dirname(destPath)); // Writes through a caller-selected symlink, as before. const destination = resolveWritePath(destPath); diff --git a/packages/cli/src/commands/batchRender.ts b/packages/cli/src/commands/batchRender.ts index 6594c96a3f..74beeb4313 100644 --- a/packages/cli/src/commands/batchRender.ts +++ b/packages/cli/src/commands/batchRender.ts @@ -1,5 +1,6 @@ import { failCommand } from "../utils/commandResult.js"; -import { mkdirSync, writeFileSync } from "node:fs"; +import { writeFileSync } from "node:fs"; +import { makeOutputFolder } from "../utils/outputFolder.js"; import { readJsonFileText } from "../utils/jsonFlag.js"; import { dirname, join, resolve, sep } from "node:path"; import { c } from "../ui/colors.js"; @@ -319,7 +320,7 @@ function summarizeManifest(manifest: BatchManifest): void { function writeManifest(manifest: BatchManifest): void { summarizeManifest(manifest); - mkdirSync(dirname(manifest.manifestPath), { recursive: true }); + makeOutputFolder(dirname(manifest.manifestPath)); writeFileSync(manifest.manifestPath, JSON.stringify(manifest, null, 2) + "\n", "utf8"); } @@ -341,7 +342,7 @@ async function renderBatchRow( } try { - mkdirSync(dirname(row.outputPath), { recursive: true }); + makeOutputFolder(dirname(row.outputPath)); const result = await options.renderOne(row); manifestRow.status = "completed"; manifestRow.durationMs = result.durationMs ?? null; diff --git a/packages/cli/src/commands/compare.ts b/packages/cli/src/commands/compare.ts index 111a27a5d5..9bb5457dd6 100644 --- a/packages/cli/src/commands/compare.ts +++ b/packages/cli/src/commands/compare.ts @@ -9,6 +9,7 @@ import { rmSync, statSync, } from "node:fs"; +import { makeOutputFolder } from "../utils/outputFolder.js"; import { tmpdir } from "node:os"; import { basename, dirname, extname, join } from "node:path"; import { defineCommand } from "../utils/defineCommand.js"; @@ -364,7 +365,7 @@ async function renderCompareSheet(parsed: ParsedCompareArgs): Promise { const { info, roi: asked } = videoAndRoi(args.video, args.roi); const out = resolve(String(args.out)); - mkdirSync(dirname(out), { recursive: true }); + makeOutputFolder(dirname(out)); if (/\.(m4a|aac|mp3|wav)$/i.test(out)) { const sound = cutSound( info, diff --git a/packages/cli/src/commands/measure/probe.ts b/packages/cli/src/commands/measure/probe.ts index df9a97c052..f331af2eeb 100644 --- a/packages/cli/src/commands/measure/probe.ts +++ b/packages/cli/src/commands/measure/probe.ts @@ -10,8 +10,8 @@ import { type VideoInfo, } from "../../measure/media.js"; import { execFileSync } from "node:child_process"; -import { mkdirSync } from "node:fs"; import { dirname, resolve } from "node:path"; +import { makeOutputFolder } from "../../utils/outputFolder.js"; export const examples: Example[] = [ [ @@ -57,7 +57,7 @@ function normalize(info: VideoInfo, out: string): { path: string; fps: string } "zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv", ); } - mkdirSync(dirname(out), { recursive: true }); + makeOutputFolder(dirname(out)); runFfmpeg([ ...["-y", "-i", info.path, "-vf", [...filters, "format=yuv420p"].join(",")], ...["-c:v", "libx264", "-crf", "12", "-preset", "medium", "-c:a", "aac", "-b:a", "192k", out], diff --git a/packages/cli/src/commands/motionShot.ts b/packages/cli/src/commands/motionShot.ts index b326ce19b1..5ffb7d3c79 100644 --- a/packages/cli/src/commands/motionShot.ts +++ b/packages/cli/src/commands/motionShot.ts @@ -11,7 +11,8 @@ import { launchManagedBrowser, resolveManagedGpuMode } from "../browser/launch.j // exactly what it's editing. All geometry + SVG live in ./motionShotLayout.ts // (pure, tested); this file only drives the browser and SAMPLES. -import { mkdirSync, statSync, writeFileSync } from "node:fs"; +import { statSync, writeFileSync } from "node:fs"; +import { makeOutputFolder } from "../utils/outputFolder.js"; import { dirname, resolve } from "node:path"; import { pageNavigationTimeoutMs } from "../utils/renderArgs.js"; import { gotoWhenSent } from "../capture/navigateForCapture.js"; @@ -59,7 +60,7 @@ export function ensureShotOutputDir(outPath: string, sourcePath?: string): void `--shot output must not overwrite the composition source: ${sourcePath}. Choose a separate .png path.`, ); } - mkdirSync(dirname(outPath), { recursive: true }); + makeOutputFolder(dirname(outPath)); } /** Returned by the in-browser selector resolver: which animated selectors a diff --git a/packages/cli/src/commands/render/execute.ts b/packages/cli/src/commands/render/execute.ts index d5b16af90e..fb594e1805 100644 --- a/packages/cli/src/commands/render/execute.ts +++ b/packages/cli/src/commands/render/execute.ts @@ -1,5 +1,5 @@ // fallow-ignore-file code-duplication -import { mkdirSync, readFileSync } from "node:fs"; +import { readFileSync } from "node:fs"; import { tmpdir } from "node:os"; import type { CanvasResolution, OutputResolutionIssueKind } from "@hyperframes/core"; import { PRORES_PROFILES } from "@hyperframes/engine"; @@ -13,6 +13,7 @@ import { type ProjectLintResult, } from "../../utils/lintProject.js"; import { normalizeErrorMessage } from "../../utils/errorMessage.js"; +import { makeOutputFolder } from "../../utils/outputFolder.js"; import { failCommand, setCommandExitCode } from "../../utils/commandResult.js"; import { reportVariableIssues, @@ -71,7 +72,7 @@ export async function executeRenderPlan( ): Promise { assertRenderActive(cancellation); applyRenderEnvironment(plan); - if (!plan.batchPath) mkdirSync(renderOutputDirectory(plan), { recursive: true }); + if (!plan.batchPath) makeOutputFolder(renderOutputDirectory(plan)); let batchModule: typeof import("../batchRender.js") | undefined; let preparedBatch: import("../batchRender.js").PreparedBatchRender | undefined; diff --git a/packages/cli/src/commands/templates.ts b/packages/cli/src/commands/templates.ts index 287331935b..58b11afc62 100644 --- a/packages/cli/src/commands/templates.ts +++ b/packages/cli/src/commands/templates.ts @@ -12,6 +12,7 @@ import { statSync, writeFileSync, } from "node:fs"; +import { makeOutputFolder } from "../utils/outputFolder.js"; import { dirname, join, resolve } from "node:path"; import { resolveCloudAuthHeaders } from "../cloud/auth.js"; import { billingBaseUrl } from "../heygen/plan.js"; @@ -110,7 +111,7 @@ const PULLED_HASH = ".template-sha256"; /** Unpacks the pack beside `dest` and moves it in whole, so `dest` only ever holds a complete template. */ function unpack(data: Buffer, dest: string, pack: TemplatePack): void { - mkdirSync(dirname(dest), { recursive: true }); + makeOutputFolder(dirname(dest)); const work = mkdtempSync(`${dest}.part-`); try { const out = join(work, "template"); diff --git a/packages/cli/src/fileIds.windows.test.ts b/packages/cli/src/fileIds.windows.test.ts index a3b591f9f1..e0adfe8f59 100644 --- a/packages/cli/src/fileIds.windows.test.ts +++ b/packages/cli/src/fileIds.windows.test.ts @@ -6,10 +6,10 @@ import { join } from "node:path"; // Number-mode stats report one identity for every path, as Windows file ids above 2^53 can. vi.mock("node:fs", async (importOriginal) => { const actual = await importOriginal(); - const statSync = ((path: string, options?: { bigint?: boolean }) => - options?.bigint - ? actual.statSync(path, options as never) - : { ...actual.statSync(path), dev: 1, ino: 2 ** 53 }) as typeof actual.statSync; + const statSync = ((path: string, options?: { bigint?: boolean }) => { + const stats = actual.statSync(path, options as never); + return options?.bigint || !stats ? stats : Object.assign(stats, { dev: 1, ino: 2 ** 53 }); + }) as typeof actual.statSync; return { ...actual, statSync }; }); diff --git a/packages/cli/src/heygen/generate.ts b/packages/cli/src/heygen/generate.ts index 80dde8d230..e9f1ef791a 100644 --- a/packages/cli/src/heygen/generate.ts +++ b/packages/cli/src/heygen/generate.ts @@ -2,15 +2,8 @@ // from one photo, a voice, or music. Every call is priced first with --plan, keyed for idempotency, downloaded into // the project and recorded in the media ledger as generated. import { randomUUID } from "node:crypto"; -import { - existsSync, - mkdirSync, - mkdtempSync, - readFileSync, - renameSync, - rmSync, - writeFileSync, -} from "node:fs"; +import { existsSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"; +import { makeOutputFolder } from "../utils/outputFolder.js"; import { tmpdir } from "node:os"; import { basename, dirname, extname, join, relative, resolve } from "node:path"; import type { @@ -663,7 +656,7 @@ export async function generate( checkRequest(r); const route = ROUTES[r.type]; const out = resolve(r.out); - mkdirSync(dirname(out), { recursive: true }); + makeOutputFolder(dirname(out)); const project = projectOf(dirname(out)); const work = mkdtempSync(join(tmpdir(), "hf-generate-")); try { diff --git a/packages/cli/src/tts/synthesize.ts b/packages/cli/src/tts/synthesize.ts index c1b0390605..500b8e0a20 100644 --- a/packages/cli/src/tts/synthesize.ts +++ b/packages/cli/src/tts/synthesize.ts @@ -1,6 +1,7 @@ // fallow-ignore-file complexity import { execFileSync } from "node:child_process"; import { existsSync, mkdirSync, readdirSync, unlinkSync } from "node:fs"; +import { makeOutputFolder } from "../utils/outputFolder.js"; import { join, dirname, basename } from "node:path"; import { ensureModel, @@ -211,7 +212,7 @@ export async function synthesize( const scriptPath = ensureSynthScript(); // 4. Ensure output directory exists - mkdirSync(dirname(outputPath), { recursive: true }); + makeOutputFolder(dirname(outputPath)); // 5. Run synthesis options?.onProgress?.(`Generating speech with voice ${voice} (${lang})...`); diff --git a/packages/cli/src/utils/outputFolder.test.ts b/packages/cli/src/utils/outputFolder.test.ts new file mode 100644 index 0000000000..1fbb39235c --- /dev/null +++ b/packages/cli/src/utils/outputFolder.test.ts @@ -0,0 +1,50 @@ +import * as fs from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { makeOutputFolder } from "./outputFolder.js"; + +// Windows answers a recursive mkdir of a drive root (D:\) with EPERM though the root exists, so an output there +// failed before it was written. makeOutputFolder leaves an existing folder alone; this mock stands in for Windows. +vi.mock("node:fs", async (importOriginal) => { + const real = await importOriginal(); + return { + ...real, + mkdirSync: vi.fn((path: string, options: fs.MakeDirectoryOptions) => { + if (real.statSync(path, { throwIfNoEntry: false })?.isDirectory()) + throw Object.assign(new Error(`EPERM: operation not permitted, mkdir '${path}'`), { + code: "EPERM", + }); + return real.mkdirSync(path, options); + }), + }; +}); + +describe("makeOutputFolder", () => { + const made: string[] = []; + afterEach(() => { + for (const dir of made.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); + }); + + it("leaves a folder that is already there, as a drive root is, alone", () => { + const root = fs.mkdtempSync(join(tmpdir(), "hf-output-root-")); + made.push(root); + expect(() => makeOutputFolder(root)).not.toThrow(); + }); + + it("makes a missing folder and the folders above it", () => { + const root = fs.mkdtempSync(join(tmpdir(), "hf-output-new-")); + made.push(root); + const dir = join(root, "renders", "today"); + makeOutputFolder(dir); + expect(fs.statSync(dir).isDirectory()).toBe(true); + }); + + it("still refuses a path that is a file", () => { + const root = fs.mkdtempSync(join(tmpdir(), "hf-output-file-")); + made.push(root); + const file = join(root, "film.mp4"); + fs.writeFileSync(file, ""); + expect(() => makeOutputFolder(file)).toThrow(/EEXIST/); + }); +}); diff --git a/packages/cli/src/utils/outputFolder.ts b/packages/cli/src/utils/outputFolder.ts new file mode 100644 index 0000000000..caf9d982f2 --- /dev/null +++ b/packages/cli/src/utils/outputFolder.ts @@ -0,0 +1,7 @@ +import { mkdirSync, statSync } from "node:fs"; + +const isFolder = (dir: string) => statSync(dir, { throwIfNoEntry: false })?.isDirectory() === true; + +export function makeOutputFolder(dir: string): void { + if (!isFolder(dir)) mkdirSync(dir, { recursive: true }); +}