Skip to content

Commit 9097d53

Browse files
fix(cli): hide Windows child process consoles (#3529)
Rebase #3529 onto current main. Preserve all 16 issue-scoped Studio server, lint, and CLI child-process windowsHide options, including main's PowerShell null guards and stderr suppression in orphanCleanup. Regression tests continue to assert windowsHide at each scoped spawn site. #3476 and #3430 remain out of scope. Co-authored-by: heygengenesis[bot] <262951085+heygengenesis[bot]@users.noreply.github.com> Co-authored-by: miguel.sierra <[email protected]>
1 parent 73aa71c commit 9097d53

23 files changed

Lines changed: 322 additions & 37 deletions

‎packages/cli/src/background-removal/pipeline.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ function spawnFfmpeg(
341341
label: string,
342342
stdio: StdioTuple,
343343
): FfmpegProc {
344-
const proc = spawn(ffmpegPath, args, { stdio });
344+
const proc = spawn(ffmpegPath, args, { stdio, windowsHide: true });
345345
let stderrBuf = "";
346346
proc.stderr?.on("data", (d: Buffer) => {
347347
stderrBuf += d.toString();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { EventEmitter } from "node:events";
2+
import { Readable } from "node:stream";
3+
import { describe, expect, it, vi } from "vitest";
4+
5+
const { spawnMock, processFrameMock, closeSessionMock } = vi.hoisted(() => ({
6+
spawnMock: vi.fn(),
7+
processFrameMock: vi.fn(async () => ({ fg: Buffer.alloc(4) })),
8+
closeSessionMock: vi.fn(async () => undefined),
9+
}));
10+
11+
vi.mock("node:child_process", () => ({ spawn: spawnMock }));
12+
vi.mock("../browser/ffmpeg.js", () => ({
13+
findFFmpeg: () => "/fake/bin/ffmpeg",
14+
findFFprobe: () => "/fake/bin/ffprobe",
15+
getFFmpegInstallHint: () => "install ffmpeg",
16+
}));
17+
vi.mock("./inference.js", () => ({
18+
createSession: async () => ({
19+
provider: "test",
20+
process: processFrameMock,
21+
close: closeSessionMock,
22+
}),
23+
}));
24+
vi.mock("@hyperframes/engine", () => ({
25+
DEFAULT_VP9_CPU_USED: 4,
26+
renderProvenanceArgs: () => [],
27+
extractMediaMetadata: async () => ({ width: 1, height: 1, fps: 1, durationSeconds: 1 }),
28+
}));
29+
30+
import { render } from "./pipeline.js";
31+
32+
function fakeFfmpeg(stdout: Readable) {
33+
const proc = new EventEmitter() as EventEmitter & {
34+
stdout: Readable;
35+
stderr: EventEmitter;
36+
stdin: EventEmitter & { write: ReturnType<typeof vi.fn>; end: ReturnType<typeof vi.fn> };
37+
kill: ReturnType<typeof vi.fn>;
38+
};
39+
proc.stdout = stdout;
40+
proc.stderr = new EventEmitter();
41+
proc.stdin = Object.assign(new EventEmitter(), { write: vi.fn(() => true), end: vi.fn() });
42+
proc.kill = vi.fn();
43+
queueMicrotask(() => proc.emit("exit", 0, null));
44+
return proc;
45+
}
46+
47+
describe("background-removal FFmpeg child-process options", () => {
48+
it("hides every FFmpeg console window", async () => {
49+
spawnMock
50+
.mockImplementationOnce(() => fakeFfmpeg(Readable.from([Buffer.alloc(3)])))
51+
.mockImplementationOnce(() => fakeFfmpeg(Readable.from([])));
52+
53+
await render({ inputPath: "/tmp/input.mp4", outputPath: "/tmp/output.webm" });
54+
55+
expect(spawnMock).toHaveBeenCalledTimes(2);
56+
for (const call of spawnMock.mock.calls) {
57+
expect(call[2]).toEqual(expect.objectContaining({ windowsHide: true }));
58+
}
59+
});
60+
});

‎packages/cli/src/browser/ffmpeg.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe("resolveH264EncoderMode", () => {
7575
expect(mockExecFile).toHaveBeenCalledWith(
7676
"/custom/ffmpeg",
7777
["-hide_banner", "-encoders"],
78-
expect.objectContaining({ encoding: "utf-8" }),
78+
expect.objectContaining({ encoding: "utf-8", windowsHide: true }),
7979
);
8080
});
8181
});

‎packages/cli/src/browser/ffmpeg.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function detectH264EncoderMode(ffmpegPath: string, gpuRequested: boolean)
2828
encoding: "utf-8",
2929
stdio: ["ignore", "pipe", "pipe"],
3030
timeout: 5000,
31+
windowsHide: true,
3132
});
3233
return resolveH264EncoderMode(encoders, gpuRequested);
3334
}

‎packages/cli/src/browser/manager.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,25 @@ describe("findBrowser — cache resolution", () => {
210210
expect(result).toEqual({ executablePath: HF_BINARY, source: "cache" });
211211
});
212212

213+
it("hides the Windows console used by the where lookup", async () => {
214+
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
215+
installFsMocks({ existing: new Set() });
216+
installPuppeteerBrowsersMock();
217+
const execSync = vi.fn((command: string) =>
218+
command === "where google-chrome" ? "C:\\Chrome\\chrome.exe\n" : "",
219+
);
220+
vi.doMock("node:child_process", () => ({ execSync, spawnSync: vi.fn() }));
221+
222+
const { findBrowser } = await import("./manager.js");
223+
const result = await findBrowser();
224+
225+
expect(result).toEqual({ executablePath: "C:\\Chrome\\chrome.exe", source: "system" });
226+
expect(execSync).toHaveBeenCalledWith(
227+
"where google-chrome",
228+
expect.objectContaining({ windowsHide: true }),
229+
);
230+
});
231+
213232
it("does not resolve to a hyperframes-cache build from an older CHROME_VERSION pin", async () => {
214233
// A build downloaded by a prior hyperframes version (this pin has moved
215234
// 131 -> 151 -> 152 across releases) must not satisfy resolution, or an

‎packages/cli/src/browser/manager.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ function whichBinary(name: string): string | undefined {
254254
encoding: "utf-8",
255255
stdio: ["pipe", "pipe", "pipe"],
256256
timeout: 5000,
257+
windowsHide: true,
257258
});
258259
const first = output
259260
.split(/\r?\n/)

‎packages/cli/src/browser/preflight.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ describe("runEnvironmentChecks", () => {
3535
expect(result.outcomes.find((outcome) => outcome.name === "FFprobe")?.ok).toBe(true);
3636
expect(result.ffmpegPath).toBe(process.execPath);
3737
expect(result.ffprobePath).toBe(process.execPath);
38+
expect(execFileSync).toHaveBeenCalledTimes(2);
39+
for (const call of execFileSync.mock.calls) {
40+
expect(call[2]).toEqual(expect.objectContaining({ windowsHide: true }));
41+
}
3842
});
3943

4044
it("reports ffprobe as a render-blocking error when the explicit path is missing", async () => {

‎packages/cli/src/browser/preflight.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ type ToolVersionResult = { ok: true; detail: string } | { ok: false; detail: str
6161
function readToolVersion(binaryPath: string): ToolVersionResult {
6262
try {
6363
const raw =
64-
execFileSync(binaryPath, ["-version"], { encoding: "utf-8", timeout: 5000 }).split("\n")[0] ??
65-
"";
64+
execFileSync(binaryPath, ["-version"], {
65+
encoding: "utf-8",
66+
timeout: 5000,
67+
windowsHide: true,
68+
}).split("\n")[0] ?? "";
6669
const version = parseToolVersion(raw);
6770
return { ok: true, detail: version ? `${version} at ${binaryPath}` : binaryPath };
6871
} catch (error) {

‎packages/cli/src/capture/captureCompositionFrame.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ export async function runFfmpegOnce(
510510
timeoutMs: number,
511511
): Promise<FfmpegRunResult> {
512512
return await new Promise((resolvePromise) => {
513-
const ff = spawn(ffmpegPath, args);
513+
const ff = spawn(ffmpegPath, args, { windowsHide: true });
514514
let stderr = "";
515515
let timedOut = false;
516516
const timer = setTimeout(() => {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { EventEmitter } from "node:events";
2+
import { describe, expect, it, vi } from "vitest";
3+
4+
const { spawnMock } = vi.hoisted(() => ({ spawnMock: vi.fn() }));
5+
6+
vi.mock("node:child_process", () => ({ spawn: spawnMock }));
7+
8+
import { runFfmpegOnce } from "./captureCompositionFrame.js";
9+
10+
describe("runFfmpegOnce child-process options", () => {
11+
it("hides the FFmpeg console window", async () => {
12+
const proc = new EventEmitter() as EventEmitter & {
13+
stderr: EventEmitter;
14+
kill: ReturnType<typeof vi.fn>;
15+
};
16+
proc.stderr = new EventEmitter();
17+
proc.kill = vi.fn();
18+
spawnMock.mockReturnValue(proc);
19+
20+
const result = runFfmpegOnce("/fake/bin/ffmpeg", ["-version"], 1000);
21+
proc.emit("close", 0);
22+
await result;
23+
24+
expect(spawnMock.mock.calls[0]?.[2]).toEqual(expect.objectContaining({ windowsHide: true }));
25+
});
26+
});

0 commit comments

Comments
 (0)