Skip to content

Commit 45f92e5

Browse files
fix(cli): a failed docker render names the container's last error (#4168)
1 parent 35a4fe1 commit 45f92e5

3 files changed

Lines changed: 88 additions & 3 deletions

File tree

‎packages/cli/src/commands/render.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import { bytesToMb } from "../telemetry/system.js";
9292
import { VERSION } from "../version.js";
9393
import { isDevMode } from "../utils/env.js";
9494
import { buildDockerRunArgs, resolveDockerPlatform } from "../utils/dockerRunArgs.js";
95+
import { createStderrTail, DockerRenderExitError } from "../utils/dockerStderrTail.js";
9596
import { normalizeErrorMessage } from "../utils/errorMessage.js";
9697
import { runEnvironmentChecks } from "../browser/preflight.js";
9798
import {
@@ -828,13 +829,18 @@ async function renderDocker(
828829

829830
try {
830831
await new Promise<void>((resolvePromise, reject) => {
832+
const stderrTail = createStderrTail();
833+
// stderr is piped so the failure can name its cause; it is still echoed live.
831834
const child = spawn("docker", dockerArgs, {
832-
// When quiet, still show stderr so container errors surface
833-
stdio: options.quiet ? ["pipe", "pipe", "inherit"] : "inherit",
835+
stdio: options.quiet ? ["pipe", "pipe", "pipe"] : ["inherit", "inherit", "pipe"],
836+
});
837+
child.stderr?.on("data", (chunk: Buffer) => {
838+
process.stderr.write(chunk);
839+
stderrTail.push(chunk.toString());
834840
});
835841
child.on("close", (code) => {
836842
if (code === 0) resolvePromise();
837-
else reject(new Error(`Docker render exited with code ${code}`));
843+
else reject(new DockerRenderExitError(code, stderrTail.tail()));
838844
});
839845
child.on("error", (err) => reject(err));
840846
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, expect, it } from "vitest";
2+
import { createStderrTail, DockerRenderExitError } from "./dockerStderrTail.js";
3+
4+
describe("dockerStderrTail", () => {
5+
it("keeps only a bounded tail of stderr", () => {
6+
const tail = createStderrTail();
7+
tail.push("a".repeat(5000));
8+
tail.push("END");
9+
expect(tail.tail().length).toBeLessThanOrEqual(2000);
10+
expect(tail.tail().endsWith("END")).toBe(true);
11+
});
12+
13+
it("prefers the last error-looking line over trailing noise", () => {
14+
const text = "starting\nError: Page crashed\nstack frame\n";
15+
expect(new DockerRenderExitError(1, text).message).toBe(
16+
"Docker render exited with code 1: Error: Page crashed",
17+
);
18+
});
19+
20+
it("falls back to the last non-empty line", () => {
21+
expect(new DockerRenderExitError(1, "one\ntwo\n\n").message).toBe(
22+
"Docker render exited with code 1: two",
23+
);
24+
});
25+
26+
it("skips stack frames, splits carriage-return progress, and caps a long line", () => {
27+
const msg = (tail: string) => new DockerRenderExitError(1, tail).message;
28+
expect(msg("Error: Page crashed\n at CaptureFailure.run (x.js:1)")).toBe(
29+
"Docker render exited with code 1: Error: Page crashed",
30+
);
31+
expect(msg("10%\r50%\rfailed to launch")).toBe(
32+
"Docker render exited with code 1: failed to launch",
33+
);
34+
expect(msg(`error ${"x".repeat(500)}`).length).toBeLessThan(200);
35+
});
36+
37+
it("names the exit code and the cause in the failure message", () => {
38+
const error = new DockerRenderExitError(137, "frame 10\nKilled");
39+
expect(error.message).toBe("Docker render exited with code 137: Killed");
40+
expect(new DockerRenderExitError(1, "").message).toBe("Docker render exited with code 1");
41+
});
42+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const MAX_TAIL_CHARS = 2000;
2+
const MAX_LINE_CHARS = 160;
3+
const STACK_FRAME = /^at\s/;
4+
const ERROR_LINE = /error|fail|exception|cannot|not found|denied|killed|no space/i;
5+
6+
interface StderrTail {
7+
push(chunk: string): void;
8+
tail(): string;
9+
}
10+
11+
export function createStderrTail(): StderrTail {
12+
let buffer = "";
13+
return {
14+
push(chunk) {
15+
buffer = (buffer + chunk).slice(-MAX_TAIL_CHARS);
16+
},
17+
tail: () => buffer.trim(),
18+
};
19+
}
20+
21+
// Last error-looking stderr line, else the last non-empty line.
22+
function lastErrorLine(tail: string): string {
23+
const lines = tail
24+
.split(/[\r\n]+/)
25+
.map((line) => line.trim())
26+
.filter((line) => line && !STACK_FRAME.test(line));
27+
const line = [...lines].reverse().find((l) => ERROR_LINE.test(l)) ?? lines.at(-1) ?? "";
28+
return line.slice(0, MAX_LINE_CHARS);
29+
}
30+
31+
export class DockerRenderExitError extends Error {
32+
constructor(exitCode: number | null, stderrTail: string) {
33+
const last = lastErrorLine(stderrTail);
34+
super(`Docker render exited with code ${exitCode}${last ? `: ${last}` : ""}`);
35+
this.name = "DockerRenderExitError";
36+
}
37+
}

0 commit comments

Comments
 (0)