Skip to content

Commit c2702e4

Browse files
committed
fix(cli): verify Chrome can execute during preflight
1 parent 3a7fcd1 commit c2702e4

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,41 @@ describe("runEnvironmentChecks", () => {
125125
}
126126
});
127127

128+
it.each([
129+
{ failure: { status: 133, signal: "SIGTRAP" }, detail: "SIGTRAP" },
130+
{ failure: { code: "EACCES" }, detail: "EACCES" },
131+
{ failure: { code: "ETIMEDOUT", signal: "SIGKILL" }, detail: "ETIMEDOUT" },
132+
])("rejects an existing browser that fails --version: $detail", async ({ failure, detail }) => {
133+
execFileSync.mockImplementation((_path, args) => {
134+
if (args[0] === "--version") throw Object.assign(new Error("cannot execute"), failure);
135+
return "ffmpeg version 7.1.1\n";
136+
});
137+
const findBrowser = vi.spyOn(manager, "findBrowser").mockResolvedValue({
138+
executablePath: process.execPath,
139+
source: "cache",
140+
});
141+
try {
142+
for (const browserPath of [undefined, process.execPath]) {
143+
const result = await runEnvironmentChecks({ includeBrowser: true, browserPath });
144+
const chrome = result.outcomes.find((outcome) => outcome.name === "Chrome");
145+
expect(chrome).toMatchObject({ ok: false, level: "error", title: "Chrome cannot start" });
146+
expect(chrome?.detail).toContain(detail);
147+
expect(result.browser).toBeUndefined();
148+
}
149+
expect(execFileSync).toHaveBeenCalledWith(
150+
process.execPath,
151+
["--version"],
152+
expect.objectContaining({
153+
timeout: 5000,
154+
killSignal: "SIGKILL",
155+
windowsHide: true,
156+
}),
157+
);
158+
} finally {
159+
findBrowser.mockRestore();
160+
}
161+
});
162+
128163
it("reports an explicit missing browser path before render starts", async () => {
129164
const result = await runEnvironmentChecks({
130165
includeBrowser: true,

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,56 @@ function chromeSharedLibOutcome(
180180
};
181181
}
182182

183+
function chromeLaunchFailureDetails(error: unknown): string {
184+
if (typeof error !== "object" || error === null) return "";
185+
const status = "status" in error ? error.status : undefined;
186+
const signal = "signal" in error ? error.signal : undefined;
187+
const code = "code" in error ? error.code : undefined;
188+
return [
189+
typeof status === "number" ? `exit code ${status}` : "",
190+
typeof signal === "string" ? `signal ${signal}` : "",
191+
typeof code === "string" ? code : "",
192+
]
193+
.filter(Boolean)
194+
.join(", ");
195+
}
196+
197+
function chromeLaunchOutcome(
198+
executablePath: string,
199+
found: EnvironmentCheckOutcome,
200+
): EnvironmentCheckOutcome {
201+
const libraries = chromeSharedLibOutcome(executablePath, found);
202+
if (!libraries.ok) return libraries;
203+
try {
204+
execFileSync(executablePath, ["--version"], {
205+
encoding: "utf-8",
206+
stdio: ["ignore", "pipe", "pipe"],
207+
timeout: 5000,
208+
killSignal: "SIGKILL",
209+
maxBuffer: 64 * 1024,
210+
windowsHide: true,
211+
});
212+
return found;
213+
} catch (error) {
214+
const details = chromeLaunchFailureDetails(error);
215+
return {
216+
name: "Chrome",
217+
ok: false,
218+
level: "error",
219+
title: "Chrome cannot start",
220+
detail: `Failed to run "${executablePath}" --version${details ? ` (${details})` : ""}.`,
221+
hint:
222+
"Select a working Chrome/Chromium binary for this OS and architecture with " +
223+
"HYPERFRAMES_BROWSER_PATH, or reinstall with: npx hyperframes browser ensure --force",
224+
path: executablePath,
225+
};
226+
}
227+
}
228+
183229
async function checkChrome(browserPath?: string): Promise<EnvironmentCheckOutcome> {
184230
if (browserPath) {
185231
if (existsSync(browserPath)) {
186-
return chromeSharedLibOutcome(browserPath, {
232+
return chromeLaunchOutcome(browserPath, {
187233
name: "Chrome",
188234
ok: true,
189235
level: "ok",
@@ -213,7 +259,7 @@ async function checkChrome(browserPath?: string): Promise<EnvironmentCheckOutcom
213259
info = undefined;
214260
}
215261
if (info) {
216-
return chromeSharedLibOutcome(info.executablePath, {
262+
return chromeLaunchOutcome(info.executablePath, {
217263
name: "Chrome",
218264
ok: true,
219265
level: "ok",

0 commit comments

Comments
 (0)