Skip to content

Commit c650479

Browse files
committed
fix(cli): the Windows Chrome check launches Chrome instead of trusting chrome.exe --version
1 parent 95aba51 commit c650479

2 files changed

Lines changed: 84 additions & 7 deletions

File tree

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import * as manager from "./manager.js";
1414
import * as linuxDeps from "./linuxDeps.js";
1515

1616
const runProcess = vi.hoisted(() => vi.fn());
17+
const launchManagedBrowser = vi.hoisted(() => vi.fn());
18+
const closeManagedBrowser = vi.hoisted(() => vi.fn());
19+
20+
vi.mock("./launch.js", () => ({ launchManagedBrowser, closeManagedBrowser }));
1721

1822
vi.mock("../utils/cancellableProcess.js", async (importOriginal) => {
1923
const actual = await importOriginal<typeof import("../utils/cancellableProcess.js")>();
@@ -211,6 +215,53 @@ describe("runEnvironmentChecks", () => {
211215
expect(result.browserInstall).toMatchObject({ pathAscii: true });
212216
});
213217

218+
describe("on Windows", () => {
219+
const realPlatform = process.platform;
220+
beforeEach(() => {
221+
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
222+
launchManagedBrowser.mockReset();
223+
closeManagedBrowser.mockReset();
224+
});
225+
afterEach(() => {
226+
Object.defineProperty(process, "platform", { value: realPlatform, configurable: true });
227+
});
228+
229+
it("takes Chrome's version from a launch, not from a `--version` run that exits 21", async () => {
230+
runProcess.mockImplementation((binaryPath: string, args: string[]) => {
231+
if (args[0] === "--version" && binaryPath === process.execPath)
232+
throw Object.assign(new Error("exit 21"), { status: 21 });
233+
return Promise.resolve({ stdout: "ffmpeg version 7.1.1\n", stderr: "" });
234+
});
235+
const browser = { version: async () => "HeadlessChrome/131.0.6778.85" };
236+
launchManagedBrowser.mockResolvedValue(browser);
237+
238+
const result = await runEnvironmentChecks({
239+
includeBrowser: true,
240+
browserPath: process.execPath,
241+
});
242+
243+
expect(result.outcomes.find((outcome) => outcome.name === "Chrome")).toMatchObject({
244+
ok: true,
245+
versionMajor: 131,
246+
});
247+
expect(closeManagedBrowser).toHaveBeenCalledWith(browser);
248+
});
249+
250+
it("reports Chrome cannot start when the launch itself fails", async () => {
251+
launchManagedBrowser.mockRejectedValue(Object.assign(new Error("boom"), { code: "ENOENT" }));
252+
253+
const result = await runEnvironmentChecks({
254+
includeBrowser: true,
255+
browserPath: process.execPath,
256+
});
257+
258+
expect(result.outcomes.find((outcome) => outcome.name === "Chrome")).toMatchObject({
259+
ok: false,
260+
title: "Chrome cannot start",
261+
});
262+
});
263+
});
264+
214265
it("reports Chrome as not found (no throw) when browser discovery throws on a corrupt cache", async () => {
215266
const spy = vi.spyOn(manager, "findBrowser").mockRejectedValue(
216267
Object.assign(new Error("ENOTDIR: not a directory, scandir 'chrome-headless-shell'"), {

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
distroLabel,
1616
parseLddMissingLibs,
1717
} from "./linuxDeps.js";
18+
import { closeManagedBrowser, launchManagedBrowser } from "./launch.js";
1819
import { getFreeDiskMb } from "../telemetry/system.js";
1920
import { runCancellableProcess } from "../utils/cancellableProcess.js";
2021
import { normalizeErrorMessage } from "../utils/errorMessage.js";
@@ -255,6 +256,33 @@ function chromeLaunchFailureDetails(error: unknown): string {
255256
.join(", ");
256257
}
257258

259+
/**
260+
* chrome.exe is a GUI-subsystem binary: `--version` prints nothing and can exit non-zero (21)
261+
* on a Chrome that renders fine. On Windows ask the browser capture launches instead.
262+
*/
263+
async function chromeVersionBanner(executablePath: string, signal?: AbortSignal): Promise<string> {
264+
if (process.platform !== "win32") {
265+
const { stdout } = await runCancellableProcess(executablePath, ["--version"], {
266+
signal,
267+
timeoutMs: FIRST_LAUNCH_TIMEOUT_MS,
268+
maxBufferBytes: 64 * 1024,
269+
});
270+
return stdout;
271+
}
272+
const puppeteer = await import("puppeteer-core");
273+
const browser = await launchManagedBrowser(puppeteer.default, {
274+
headless: true,
275+
executablePath,
276+
timeout: FIRST_LAUNCH_TIMEOUT_MS,
277+
signal,
278+
});
279+
try {
280+
return await browser.version();
281+
} finally {
282+
await closeManagedBrowser(browser);
283+
}
284+
}
285+
258286
async function chromeLaunchOutcome(
259287
executablePath: string,
260288
found: EnvironmentCheckOutcome,
@@ -263,12 +291,10 @@ async function chromeLaunchOutcome(
263291
const libraries = await chromeSharedLibOutcome(executablePath, found, signal);
264292
if (!libraries.ok) return libraries;
265293
try {
266-
const { stdout } = await runCancellableProcess(executablePath, ["--version"], {
267-
signal,
268-
timeoutMs: FIRST_LAUNCH_TIMEOUT_MS,
269-
maxBufferBytes: 64 * 1024,
270-
});
271-
return { ...found, versionMajor: extractMajorVersion(stdout) };
294+
return {
295+
...found,
296+
versionMajor: extractMajorVersion(await chromeVersionBanner(executablePath, signal)),
297+
};
272298
} catch (error) {
273299
if (signal?.aborted) signal.throwIfAborted();
274300
const details = chromeLaunchFailureDetails(error);
@@ -277,7 +303,7 @@ async function chromeLaunchOutcome(
277303
ok: false,
278304
level: "error",
279305
title: "Chrome cannot start",
280-
detail: `Failed to run "${executablePath}" --version${details ? ` (${details})` : ""}.`,
306+
detail: `Failed to start "${executablePath}"${details ? ` (${details})` : ""}.`,
281307
hint:
282308
"Select a working Chrome/Chromium binary for this OS and architecture with " +
283309
"HYPERFRAMES_BROWSER_PATH, or reinstall with: npx hyperframes browser ensure --force",

0 commit comments

Comments
 (0)