Skip to content

Commit 39a8c8e

Browse files
committed
fix(cli): the Windows Chrome check reads the file version, not chrome.exe --version
1 parent 402f0bd commit 39a8c8e

2 files changed

Lines changed: 46 additions & 47 deletions

File tree

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ 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 }));
2117

2218
vi.mock("../utils/cancellableProcess.js", async (importOriginal) => {
2319
const actual = await importOriginal<typeof import("../utils/cancellableProcess.js")>();
@@ -221,24 +217,18 @@ describe("runEnvironmentChecks", () => {
221217
});
222218

223219
describe("on Windows", () => {
224-
const realPlatform = process.platform;
225220
beforeEach(() => {
226221
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
227-
launchManagedBrowser.mockReset();
228-
closeManagedBrowser.mockReset();
229-
});
230-
afterEach(() => {
231-
Object.defineProperty(process, "platform", { value: realPlatform, configurable: true });
232222
});
233223

234-
it("takes Chrome's version from a launch, not from a `--version` run that exits 21", async () => {
224+
it("reads Chrome's version from the file, never from a `--version` run that exits 21", async () => {
235225
runProcess.mockImplementation((binaryPath: string, args: string[]) => {
236-
if (args[0] === "--version" && binaryPath === process.execPath)
226+
if (binaryPath === process.execPath && args[0] === "--version")
237227
throw Object.assign(new Error("exit 21"), { status: 21 });
228+
if (binaryPath === "powershell.exe")
229+
return Promise.resolve({ stdout: "131.0.6778.85\r\n", stderr: "" });
238230
return Promise.resolve({ stdout: "ffmpeg version 7.1.1\n", stderr: "" });
239231
});
240-
const browser = { version: async () => "HeadlessChrome/131.0.6778.85" };
241-
launchManagedBrowser.mockResolvedValue(browser);
242232

243233
const result = await runEnvironmentChecks({
244234
includeBrowser: true,
@@ -249,21 +239,22 @@ describe("runEnvironmentChecks", () => {
249239
ok: true,
250240
versionMajor: 131,
251241
});
252-
expect(closeManagedBrowser).toHaveBeenCalledWith(browser);
253242
});
254243

255-
it("reports Chrome cannot start when the launch itself fails", async () => {
256-
launchManagedBrowser.mockRejectedValue(Object.assign(new Error("boom"), { code: "ENOENT" }));
244+
it("still reports Chrome usable when its version cannot be read", async () => {
245+
runProcess.mockImplementation((binaryPath: string) => {
246+
if (binaryPath === "powershell.exe") throw new Error("powershell unavailable");
247+
return Promise.resolve({ stdout: "ffmpeg version 7.1.1\n", stderr: "" });
248+
});
257249

258250
const result = await runEnvironmentChecks({
259251
includeBrowser: true,
260252
browserPath: process.execPath,
261253
});
262254

263-
expect(result.outcomes.find((outcome) => outcome.name === "Chrome")).toMatchObject({
264-
ok: false,
265-
title: "Chrome cannot start",
266-
});
255+
const chrome = result.outcomes.find((outcome) => outcome.name === "Chrome");
256+
expect(chrome?.ok).toBe(true);
257+
expect(chrome?.versionMajor).toBeUndefined();
267258
});
268259
});
269260

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

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
distroLabel,
1616
parseLddMissingLibs,
1717
} from "./linuxDeps.js";
18-
import { closeManagedBrowser, launchManagedBrowser } from "./launch.js";
1918
import { getFreeDiskMb } from "../telemetry/system.js";
2019
import { runCancellableProcess } from "../utils/cancellableProcess.js";
2120
import { normalizeErrorMessage } from "../utils/errorMessage.js";
@@ -256,27 +255,31 @@ function chromeLaunchFailureDetails(error: unknown): string {
256255
.join(", ");
257256
}
258257

259-
// Windows chrome.exe prints nothing for --version and can exit 21 while rendering fine: launch it as capture does.
260-
async function chromeVersionBanner(executablePath: string, signal?: AbortSignal): Promise<string> {
261-
if (process.platform !== "win32") {
262-
const { stdout } = await runCancellableProcess(executablePath, ["--version"], {
263-
signal,
264-
timeoutMs: FIRST_LAUNCH_TIMEOUT_MS,
265-
maxBufferBytes: 64 * 1024,
266-
});
267-
return stdout;
268-
}
269-
const puppeteer = await import("puppeteer-core");
270-
const browser = await launchManagedBrowser(puppeteer.default, {
271-
headless: true,
272-
executablePath,
273-
timeout: FIRST_LAUNCH_TIMEOUT_MS,
274-
signal,
275-
});
258+
// Windows chrome.exe prints nothing for --version and can exit 21 while rendering fine: read the file's version.
259+
async function windowsChromeVersionMajor(
260+
executablePath: string,
261+
signal?: AbortSignal,
262+
): Promise<number | undefined> {
276263
try {
277-
return await browser.version();
278-
} finally {
279-
await closeManagedBrowser(browser);
264+
const { stdout } = await runCancellableProcess(
265+
"powershell.exe",
266+
[
267+
"-NoProfile",
268+
"-NonInteractive",
269+
"-Command",
270+
"(Get-Item -LiteralPath $env:HF_CHROME).VersionInfo.ProductVersion",
271+
],
272+
{
273+
signal,
274+
timeoutMs: 10_000,
275+
maxBufferBytes: 4 * 1024,
276+
env: { ...process.env, HF_CHROME: executablePath },
277+
},
278+
);
279+
return extractMajorVersion(stdout);
280+
} catch {
281+
if (signal?.aborted) signal.throwIfAborted();
282+
return undefined;
280283
}
281284
}
282285

@@ -287,11 +290,16 @@ async function chromeLaunchOutcome(
287290
): Promise<EnvironmentCheckOutcome> {
288291
const libraries = await chromeSharedLibOutcome(executablePath, found, signal);
289292
if (!libraries.ok) return libraries;
293+
if (process.platform === "win32") {
294+
return { ...found, versionMajor: await windowsChromeVersionMajor(executablePath, signal) };
295+
}
290296
try {
291-
return {
292-
...found,
293-
versionMajor: extractMajorVersion(await chromeVersionBanner(executablePath, signal)),
294-
};
297+
const { stdout } = await runCancellableProcess(executablePath, ["--version"], {
298+
signal,
299+
timeoutMs: FIRST_LAUNCH_TIMEOUT_MS,
300+
maxBufferBytes: 64 * 1024,
301+
});
302+
return { ...found, versionMajor: extractMajorVersion(stdout) };
295303
} catch (error) {
296304
if (signal?.aborted) signal.throwIfAborted();
297305
const details = chromeLaunchFailureDetails(error);
@@ -300,7 +308,7 @@ async function chromeLaunchOutcome(
300308
ok: false,
301309
level: "error",
302310
title: "Chrome cannot start",
303-
detail: `Failed to start "${executablePath}"${details ? ` (${details})` : ""}.`,
311+
detail: `Failed to run "${executablePath}" --version${details ? ` (${details})` : ""}.`,
304312
hint:
305313
"Select a working Chrome/Chromium binary for this OS and architecture with " +
306314
"HYPERFRAMES_BROWSER_PATH, or reinstall with: npx hyperframes browser ensure --force",

0 commit comments

Comments
 (0)