Skip to content

Write HTML. Render video. Built for agents.

fix(cli): CLI tests and a refused preview stop killing orphaned Chrome (#6022)

Miguel Ángel5641a9179acd
diff --git a/packages/cli/src/commands/preview.test.ts b/packages/cli/src/commands/preview.test.ts
index ef07570b74..1e052dbac8 100644
--- a/packages/cli/src/commands/preview.test.ts
+++ b/packages/cli/src/commands/preview.test.ts
@@ -36,6 +36,12 @@ vi.mock("../utils/cancellableProcess.js", async (importOriginal) => ({
...(await importOriginal<typeof import("../utils/cancellableProcess.js")>()),
runRenderSetupWorker: lint.worker,
}));
+// The real cleanup kills every orphaned Chrome the user owns, so no test may reach it.
+const killOrphanedProcesses = vi.hoisted(() => vi.fn(() => 0));
+vi.mock("../utils/orphanCleanup.js", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../utils/orphanCleanup.js")>()),
+ killOrphanedProcesses,
+}));
const tempDirs: string[] = [];
@@ -633,5 +639,6 @@ describe("preview inside a host app's agent run", () => {
}
const payload = JSON.parse(printed);
expect(payload).toMatchObject({ ok: false, error: { code: "host-app-owns-preview" } });
+ expect(killOrphanedProcesses).not.toHaveBeenCalled();
});
});
diff --git a/packages/cli/src/commands/preview.ts b/packages/cli/src/commands/preview.ts
index 244dfd2287..1f8800f123 100644
--- a/packages/cli/src/commands/preview.ts
+++ b/packages/cli/src/commands/preview.ts
@@ -477,17 +477,6 @@ export default defineCommand({
// modes all receive identical --proxy/--no-proxy + config semantics.
const autoProxy = resolveAutoProxy(dir, args.proxy as boolean | undefined);
- // Kill orphaned chrome-headless-shell processes from previous crashed
- // sessions. Deliberately last: this reaches outside the process and kills
- // other people's PIDs, so it must not run for an invocation that turns out
- // to be a validation error and never starts anything.
- const orphansKilled = killOrphanedProcesses();
- if (orphansKilled > 0 && !args.json) {
- console.log(
- ` ${c.dim(`Cleaned up ${orphansKilled} orphaned process${orphansKilled === 1 ? "" : "es"} from a previous session.`)}`,
- );
- }
-
const launchMode = previewLaunchMode({
background: Boolean(args.background),
foreground: Boolean(args.foreground),
@@ -502,6 +491,17 @@ export default defineCommand({
return;
}
+ // Kill orphaned chrome-headless-shell processes from previous crashed
+ // sessions. Deliberately last: this reaches outside the process and kills
+ // other people's PIDs, so it must not run for an invocation that turns out
+ // to be a validation error or a refusal and never starts anything.
+ const orphansKilled = killOrphanedProcesses();
+ if (orphansKilled > 0 && !args.json) {
+ console.log(
+ ` ${c.dim(`Cleaned up ${orphansKilled} orphaned process${orphansKilled === 1 ? "" : "es"} from a previous session.`)}`,
+ );
+ }
+
// Runs in the CLI worker while Studio starts, so it never delays the opening; --json skips it.
const startupLint = args.json ? null : printStartupLint(dir, Boolean(args["lint-verbose"]));
diff --git a/packages/cli/src/utils/orphanCleanup.test.ts b/packages/cli/src/utils/orphanCleanup.test.ts
index 41c8898723..dc69edfba9 100644
--- a/packages/cli/src/utils/orphanCleanup.test.ts
+++ b/packages/cli/src/utils/orphanCleanup.test.ts
@@ -3,7 +3,6 @@ import { spawn } from "node:child_process";
import {
isProcessDescendant,
killProcessTree,
- killOrphanedProcesses,
processIdentity,
windowsProcessTreeKillArgs,
} from "./orphanCleanup.js";
@@ -110,18 +109,3 @@ describe.skipIf(!IS_UNIX)("killProcessTree", () => {
expect(() => process.kill(proc.pid!, 0)).toThrow();
}, 5000);
});
-
-describe.skipIf(!IS_UNIX)("killOrphanedProcesses", () => {
- it("returns 0 when no orphans exist", () => {
- const killed = killOrphanedProcesses();
- expect(killed).toBe(0);
- });
-
- it("does not kill non-orphaned Chrome processes", () => {
- // Our current process is not an orphan (PPID !== 1), so any
- // chrome-headless-shell processes we'd find with our PID as
- // ancestor wouldn't be killed.
- const killed = killOrphanedProcesses();
- expect(killed).toBe(0);
- });
-});