From f14764fd3c408e1c5c508570cf77532261c8fc94 Mon Sep 17 00:00:00 2001 From: Shinya Fujino Date: Sun, 6 Dec 2026 10:12:55 +0900 Subject: [PATCH 1/3] fix(sandbox): point the default image at the packaged Dockerfile Since 1.10.0, an app that imports `void/sandbox` and sets no `sandbox.image` gets a default image that does not exist, `node_modules/void/dist/sandbox.Dockerfile`. The package ships the file at its root. `vp build` records the missing path, and `vp dev` stops before it serves anything: Error: ENOENT: no such file or directory, open '/node_modules/void/dist/sandbox.Dockerfile' `void deploy --platform cloudflare` fails at `docker build` and says "Sandbox image preparation failed. Start Docker and retry the deployment." while Docker is running. 1.9.1 works. Deploys to a managed platform are not affected: they never build the Dockerfile. `getDefaultSandboxImage()` finds the file relative to the `void/sandbox` entry, as `../../sandbox.Dockerfile`. 38db82c30 (#841) moved that entry from `dist/runtime/sandbox.mjs` to `dist/runtime/sandbox/index.mjs`, so the same relative URL now ends in `dist/`. ## Fix Name the Dockerfile in the package's private `imports`, as `#self/sandbox.Dockerfile`, and resolve it from there. The path no longer depends on where a built module sits in `dist/`, and the packed checks already verify that every `imports` target is in the tarball. The file stays at the package root, so a config that works around the bug with `sandbox.image: './node_modules/void/sandbox.Dockerfile'` keeps working. ## Tests The default-image test checked only the file name. It now reads the resolved path and compares it with the packaged Dockerfile. The kitchen-sink build test checks that the Dockerfile recorded in `dist/ssr/wrangler.json` exists. Both fail without the fix. ## Verification - By hand, with the packed build installed in a new starter and Docker running: `vp build` records `node_modules/void/sandbox.Dockerfile`, `vp dev` builds the image, and the "Run a command" route of the Sandboxes guide answers `{"exitCode":0,"stdout":"v26.12.1\n"}`. `void deploy --platform cloudflare`, against a local stand-in for the Cloudflare API, builds the image and goes on to the Worker upload. The same steps on the published 1.13.1 fail as described above. - `node .github/scripts/packed-consumer.mjs` passes for npm and pnpm, and with `VOID_PACKED_SUITE=full`. A pnpm install resolves the default image to an existing file too. Co-Authored-By: Claude Fable 5.2 --- packages/void/package.json | 1 + packages/void/src/sandbox/config.ts | 4 +++- packages/void/test/integration/kitchen-sink-build.test.ts | 6 +++++- packages/void/test/unit/config.test.ts | 5 +++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/void/package.json b/packages/void/package.json index a4733bf90..9c975a898 100644 --- a/packages/void/package.json +++ b/packages/void/package.json @@ -37,6 +37,7 @@ "#self/remote": "./dist/runtime/remote/index.mjs", "#self/remote/binding-handler": "./dist/runtime/remote/binding-handler.mjs", "#self/sandbox": "./dist/runtime/sandbox/index.mjs", + "#self/sandbox.Dockerfile": "./sandbox.Dockerfile", "#self/runtime/live-server": "./dist/runtime/live/server.mjs", "#self/runtime/fetch": "./dist/runtime/client/fetch.mjs", "#self/runtime/fetch-server": "./dist/runtime/client/fetch-server.mjs", diff --git a/packages/void/src/sandbox/config.ts b/packages/void/src/sandbox/config.ts index 00fdf39c1..6c80ef211 100644 --- a/packages/void/src/sandbox/config.ts +++ b/packages/void/src/sandbox/config.ts @@ -42,7 +42,9 @@ export function isSandboxEnabled(config: VoidConfig): boolean { } function getDefaultSandboxImage(): string { - return fileURLToPath(new URL('../../sandbox.Dockerfile', import.meta.resolve('void/sandbox'))); + // Docker reads this file from the installed package. The private import names it in + // package.json, so the path does not depend on where a built module sits in dist. + return fileURLToPath(import.meta.resolve('#self/sandbox.Dockerfile')); } function getDefaultSandboxPlatformImage(): string { diff --git a/packages/void/test/integration/kitchen-sink-build.test.ts b/packages/void/test/integration/kitchen-sink-build.test.ts index e91f96380..afd258979 100644 --- a/packages/void/test/integration/kitchen-sink-build.test.ts +++ b/packages/void/test/integration/kitchen-sink-build.test.ts @@ -1,5 +1,5 @@ import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'; -import { join } from 'node:path'; +import { join, resolve } from 'node:path'; import spawn from 'cross-spawn'; import { toSlash } from 'pathslash'; import { afterAll, beforeAll, describe, expect, it } from 'vite-plus/test'; @@ -141,6 +141,10 @@ describe('playground/kitchen-sink build', () => { class_name: 'SandboxV1', }), ]); + // The playground sets no `sandbox.image`. Docker builds the default image from this path. + const dockerfile: unknown = wranglerConfig.containers[0].images.default.dockerfile; + expect(dockerfile).toMatch(/sandbox\.Dockerfile$/); + expect(existsSync(resolve(dist, 'ssr', String(dockerfile)))).toBe(true); expect(wranglerConfig.migrations).toContainEqual({ tag: 'void-sandbox-v2', new_sqlite_classes: ['SandboxV1'], diff --git a/packages/void/test/unit/config.test.ts b/packages/void/test/unit/config.test.ts index 2829f8e03..c6d7edae6 100644 --- a/packages/void/test/unit/config.test.ts +++ b/packages/void/test/unit/config.test.ts @@ -513,6 +513,11 @@ describe('readConfig', () => { const config = resolveSandboxConfig({ sandbox: true }, tmpDir); expect(config.image).toMatch(/sandbox\.Dockerfile$/); + // Docker reads this path during `vp dev` and native deploys, so it must name the file the + // package publishes at its root. + expect(readFileSync(config.image, 'utf8')).toBe( + readFileSync(join(import.meta.dirname, '..', '..', 'sandbox.Dockerfile'), 'utf8'), + ); expect(config.platformImage).toBe('cloudflare/debian-trixie'); }); From 79fde561c50c5fe14dc81c8f92e4514c19b2488c Mon Sep 17 00:00:00 2001 From: Shinya Fujino Date: Sun, 6 Dec 2026 10:13:04 +0900 Subject: [PATCH 2/3] fix(deploy): report a failed Sandbox image build when Docker is running `void deploy --platform cloudflare` answered every failed `docker build` of a Sandbox image with "Sandbox image preparation failed. Start Docker and retry the deployment.". That was also the answer while Docker was running and the build itself failed, for example on a missing Dockerfile or a failing step. When the Docker CLI could not be started at all, the deploy printed the raw spawn error instead (`spawn docker ENOENT`). ## Fix After a failed build, ask the daemon with `docker info`. When it answers, name the failure: `docker build`'s exit code and the Dockerfile, with a pointer to Docker's output above. Keep the "Start Docker" advice for the cases it fits: the daemon does not answer, or the CLI cannot be started. A build that succeeds runs no extra command. ## Tests `container-image.test.ts` runs `buildContainerImage()` with a stand-in Docker CLI: a build that succeeds, a build that fails while the daemon answers, a daemon that does not answer, and a CLI that cannot be started. ## Verification - `vpr test:all` passes on this branch: 267/267 tasks, 19,660 tests pass, 35 skipped. - By hand, `void deploy --platform cloudflare` against a local stand-in for the Cloudflare API, with Docker running and a Dockerfile whose `RUN` step fails: Sandbox image preparation failed: `docker build` exited with code 1 for /Dockerfile.broken. See the Docker output above. With `DOCKER_HOST` set to a socket that does not exist, and with a Docker CLI that cannot be started, it prints the "Start Docker" message. Co-Authored-By: Claude Fable 5.2 --- packages/void/src/cli/container-image.ts | 30 +++--- .../void/test/unit/container-image.test.ts | 99 +++++++++++++++++++ 2 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 packages/void/test/unit/container-image.test.ts diff --git a/packages/void/src/cli/container-image.ts b/packages/void/src/cli/container-image.ts index 174307edc..aef7e0edf 100644 --- a/packages/void/src/cli/container-image.ts +++ b/packages/void/src/cli/container-image.ts @@ -2,19 +2,14 @@ import { spawn } from 'node:child_process'; import { randomUUID } from 'node:crypto'; import { dirname } from 'pathslash'; -function docker(args: Array, quiet = false): Promise { - return new Promise((resolve, reject) => { +/** Resolves the exit code; `null` when the Docker CLI cannot start or a signal ends it. */ +function docker(args: Array, quiet = false): Promise { + return new Promise((resolve) => { const child = spawn(process.env.WRANGLER_DOCKER_BIN ?? 'docker', args, { stdio: quiet ? 'ignore' : 'inherit', }); - child.once('error', reject); - child.once('exit', (code) => - code === 0 - ? resolve() - : reject( - new Error('Sandbox image preparation failed. Start Docker and retry the deployment.'), - ), - ); + child.once('error', () => resolve(null)); + child.once('exit', resolve); }); } @@ -24,7 +19,7 @@ export async function buildContainerImage( buildContext: string = dirname(dockerfile), ): Promise<{ localReference: string; dispose(): Promise }> { const localReference = `void-sandbox-build:${randomUUID()}`; - await docker([ + const code = await docker([ 'build', '--platform', 'linux/amd64', @@ -34,8 +29,19 @@ export async function buildContainerImage( localReference, buildContext, ]); + if (code !== 0) { + // Starting Docker helps only when it is not running. Otherwise Docker has printed the cause. + if (code === null || (await docker(['info'], true)) !== 0) { + throw new Error('Sandbox image preparation failed. Start Docker and retry the deployment.'); + } + throw new Error( + `Sandbox image preparation failed: \`docker build\` exited with code ${code} for ${dockerfile}. See the Docker output above.`, + ); + } return { localReference, - dispose: () => docker(['image', 'rm', localReference], true).catch(() => {}), + dispose: async () => { + await docker(['image', 'rm', localReference], true); + }, }; } diff --git a/packages/void/test/unit/container-image.test.ts b/packages/void/test/unit/container-image.test.ts new file mode 100644 index 000000000..49d04578f --- /dev/null +++ b/packages/void/test/unit/container-image.test.ts @@ -0,0 +1,99 @@ +import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { afterEach, describe, expect, it, vi } from 'vite-plus/test'; +import { buildContainerImage } from '../../src/cli/container-image.ts'; + +const START_DOCKER = 'Sandbox image preparation failed. Start Docker and retry the deployment.'; + +const roots: Array = []; +afterEach(() => { + vi.unstubAllEnvs(); + for (const root of roots.splice(0)) { + rmSync(root, { recursive: true, force: true }); + } +}); + +/** Install a Docker CLI stand-in that records its arguments and exits `build` and `info` as given. */ +function stubDocker(exits: { build: number; info: number }): { + root: string; + dockerfile: string; + calls: () => Array; +} { + const root = mkdtempSync(join(tmpdir(), 'void-container-image-')); + roots.push(root); + const log = join(root, 'docker.log'); + const binary = join(root, 'docker-stub'); + writeFileSync(log, ''); + writeFileSync( + binary, + '#!/usr/bin/env node\n' + + 'const args = process.argv.slice(2);\n' + + `require("node:fs").appendFileSync(${JSON.stringify(log)}, JSON.stringify(args) + "\\n");\n` + + `process.exit(${JSON.stringify(exits)}[args[0]] ?? 0);\n`, + ); + chmodSync(binary, 0o755); + vi.stubEnv('WRANGLER_DOCKER_BIN', binary); + return { + root, + dockerfile: join(root, 'Dockerfile'), + calls: () => + readFileSync(log, 'utf8') + .split('\n') + .filter(Boolean) + .map((line): unknown => JSON.parse(line)), + }; +} + +function buildArgs(dockerfile: string, context: string): Array { + return [ + 'build', + '--platform', + 'linux/amd64', + '--file', + dockerfile, + '--tag', + expect.stringMatching(/^void-sandbox-build:/), + context, + ]; +} + +// The stand-in is an executable script, which Windows does not launch by path. +describe.skipIf(process.platform === 'win32')('buildContainerImage', () => { + it('builds without checking the Docker daemon and removes the local tag on dispose', async () => { + const docker = stubDocker({ build: 0, info: 0 }); + + const image = await buildContainerImage(docker.dockerfile); + expect(docker.calls()).toEqual([buildArgs(docker.dockerfile, docker.root)]); + + await image.dispose(); + expect(docker.calls()).toEqual([ + buildArgs(docker.dockerfile, docker.root), + ['image', 'rm', image.localReference], + ]); + }); + + it('reports a failed build as a build failure while Docker is running', async () => { + const docker = stubDocker({ build: 1, info: 0 }); + + await expect(buildContainerImage(docker.dockerfile)).rejects.toThrow( + `Sandbox image preparation failed: \`docker build\` exited with code 1 for ${docker.dockerfile}. See the Docker output above.`, + ); + expect(docker.calls()).toEqual([buildArgs(docker.dockerfile, docker.root), ['info']]); + }); + + it('asks to start Docker when the daemon does not answer', async () => { + const docker = stubDocker({ build: 1, info: 1 }); + + await expect(buildContainerImage(docker.dockerfile)).rejects.toThrow(START_DOCKER); + expect(docker.calls()).toEqual([buildArgs(docker.dockerfile, docker.root), ['info']]); + }); + + it('asks to start Docker when its CLI cannot be launched', async () => { + const docker = stubDocker({ build: 0, info: 0 }); + vi.stubEnv('WRANGLER_DOCKER_BIN', join(docker.root, 'missing-docker')); + + await expect(buildContainerImage(docker.dockerfile)).rejects.toThrow(START_DOCKER); + expect(docker.calls()).toEqual([]); + }); +}); From 320198891105073378f05f80c49704f2d7b4ffe8 Mon Sep 17 00:00:00 2001 From: Shinya Fujino Date: Sun, 6 Dec 2026 11:48:44 +0900 Subject: [PATCH 3/3] fix(deploy): say "Start Docker" only when the daemon does not answer The previous commit reports a failed `docker build` of a Sandbox image as a build failure while Docker is running. `void deploy --platform cloudflare` still said "Sandbox image preparation failed. Start Docker and retry the deployment." in two cases that starting Docker does not fix: the Docker CLI could not be started, for example because Docker is not installed, and a signal stopped the build. The check of the daemon also had no time limit. After a failed build the deploy runs `docker info` to tell a failed build from a daemon that is not running. A daemon that accepts the connection and never answers kept the deploy waiting, without a report of the failed build. ## Fix - When the CLI cannot be started, name the cause: "the Docker CLI could not be started (spawn docker ENOENT). Check that Docker is installed and retry the deployment." - When a signal stopped the build, name the signal and do not ask the daemon: "`docker build` was stopped by SIGTERM for ." - Give `docker info` 5 seconds. When it does not finish in time, or cannot run, report the build failure: Docker has printed the cause above. "Start Docker" stays for the daemon that is known not to answer, when `docker info` exits with a non-zero code. At the deadline the deploy kills only the CLI process. `docker info` starts the CLI plugins just to read their metadata, before it asks the daemon, so it has no child process while it waits. Checked with Docker 29.8.2 against a socket that accepts connections and never answers. ## Tests `container-image.test.ts` adds a `docker info` that hangs, with the deadline reached through fake timers, and a build that a signal stops. The case of a CLI that cannot be started expects the new message. ## Verification - `vpr test:all` passes on this branch: 267/267 tasks, 19,662 tests pass, 35 skipped. - By hand, `buildContainerImage()` with the real Docker CLI (29.8.2). With Docker running and no Dockerfile: Sandbox image preparation failed: `docker build` exited with code 1 for /Dockerfile. See the Docker output above. With a failed build and `docker info` sent to a socket that never answers, the same report comes after 5 seconds, and no Docker process is left. With a CLI path that does not exist: Sandbox image preparation failed: the Docker CLI could not be started (spawn ENOENT). Check that Docker is installed and retry the deployment. Co-Authored-By: Claude Fable 5.2 --- packages/void/src/cli/container-image.ts | 67 +++++++++++--- .../void/test/unit/container-image.test.ts | 91 ++++++++++++++++--- 2 files changed, 131 insertions(+), 27 deletions(-) diff --git a/packages/void/src/cli/container-image.ts b/packages/void/src/cli/container-image.ts index aef7e0edf..ae77c0950 100644 --- a/packages/void/src/cli/container-image.ts +++ b/packages/void/src/cli/container-image.ts @@ -2,24 +2,67 @@ import { spawn } from 'node:child_process'; import { randomUUID } from 'node:crypto'; import { dirname } from 'pathslash'; -/** Resolves the exit code; `null` when the Docker CLI cannot start or a signal ends it. */ -function docker(args: Array, quiet = false): Promise { +/** A daemon that does not answer must not keep back the report of a failed build. */ +const DAEMON_TIMEOUT_MS = 5000; + +/** The exit code, the signal that stopped the command, or the error that kept it from starting. */ +type DockerResult = number | NodeJS.Signals | Error; + +/** + * Run a Docker command. With `timeoutMs`, a CLI that is still running by then is killed. That is + * enough for `docker info`, the one command with a deadline: it starts plugins only to read their + * metadata, before it asks the daemon, so it has no child process while it waits. + */ +function docker( + args: Array, + options: { quiet?: boolean; timeoutMs?: number } = {}, +): Promise { return new Promise((resolve) => { const child = spawn(process.env.WRANGLER_DOCKER_BIN ?? 'docker', args, { - stdio: quiet ? 'ignore' : 'inherit', + stdio: options.quiet ? 'ignore' : 'inherit', + }); + const deadline = + options.timeoutMs === undefined + ? undefined + : setTimeout(() => { + child.kill('SIGKILL'); + }, options.timeoutMs); + const settle = (result: DockerResult) => { + clearTimeout(deadline); + resolve(result); + }; + child.once('error', settle); + child.once('exit', (code, signal) => { + // Node reports exactly one of the two. + settle(code ?? signal ?? 1); }); - child.once('error', () => resolve(null)); - child.once('exit', resolve); }); } +/** Say why `docker build` produced no image. */ +async function describeBuildFailure(build: DockerResult, dockerfile: string): Promise { + if (build instanceof Error) { + return `Sandbox image preparation failed: the Docker CLI could not be started (${build.message}). Check that Docker is installed and retry the deployment.`; + } + if (typeof build === 'string') { + return `Sandbox image preparation failed: \`docker build\` was stopped by ${build} for ${dockerfile}.`; + } + // Docker has printed why the build failed. Starting Docker helps only when its daemon is known + // not to answer: a probe that times out or cannot run leaves the build failure as the report. + const daemon = await docker(['info'], { quiet: true, timeoutMs: DAEMON_TIMEOUT_MS }); + if (typeof daemon === 'number' && daemon !== 0) { + return 'Sandbox image preparation failed. Start Docker and retry the deployment.'; + } + return `Sandbox image preparation failed: \`docker build\` exited with code ${build} for ${dockerfile}. See the Docker output above.`; +} + /** Build locally; the pinned cf uploader pushes this tag to the managed registry. */ export async function buildContainerImage( dockerfile: string, buildContext: string = dirname(dockerfile), ): Promise<{ localReference: string; dispose(): Promise }> { const localReference = `void-sandbox-build:${randomUUID()}`; - const code = await docker([ + const build = await docker([ 'build', '--platform', 'linux/amd64', @@ -29,19 +72,13 @@ export async function buildContainerImage( localReference, buildContext, ]); - if (code !== 0) { - // Starting Docker helps only when it is not running. Otherwise Docker has printed the cause. - if (code === null || (await docker(['info'], true)) !== 0) { - throw new Error('Sandbox image preparation failed. Start Docker and retry the deployment.'); - } - throw new Error( - `Sandbox image preparation failed: \`docker build\` exited with code ${code} for ${dockerfile}. See the Docker output above.`, - ); + if (build !== 0) { + throw new Error(await describeBuildFailure(build, dockerfile)); } return { localReference, dispose: async () => { - await docker(['image', 'rm', localReference], true); + await docker(['image', 'rm', localReference], { quiet: true }); }, }; } diff --git a/packages/void/test/unit/container-image.test.ts b/packages/void/test/unit/container-image.test.ts index 49d04578f..2e6e9ad6b 100644 --- a/packages/void/test/unit/container-image.test.ts +++ b/packages/void/test/unit/container-image.test.ts @@ -6,31 +6,50 @@ import { buildContainerImage } from '../../src/cli/container-image.ts'; const START_DOCKER = 'Sandbox image preparation failed. Start Docker and retry the deployment.'; +// The wait for a real process also runs in the test that replaces `setTimeout`. +const realSetTimeout = setTimeout; + +/** What the stand-in does for a command: exit with a code, stop itself with a signal, or hang. */ +type Outcome = number | NodeJS.Signals | 'hang'; + +type DockerStub = { + root: string; + dockerfile: string; + calls: () => Array; + /** The process id of a command that hangs, once it runs. */ + hungPid: () => number | undefined; +}; + const roots: Array = []; afterEach(() => { + vi.useRealTimers(); vi.unstubAllEnvs(); for (const root of roots.splice(0)) { rmSync(root, { recursive: true, force: true }); } }); -/** Install a Docker CLI stand-in that records its arguments and exits `build` and `info` as given. */ -function stubDocker(exits: { build: number; info: number }): { - root: string; - dockerfile: string; - calls: () => Array; -} { +/** Install a Docker CLI stand-in that records its arguments and ends each command as given. */ +function stubDocker(outcomes: { build: Outcome; info?: Outcome }): DockerStub { const root = mkdtempSync(join(tmpdir(), 'void-container-image-')); roots.push(root); const log = join(root, 'docker.log'); + const pidFile = join(root, 'docker.pid'); const binary = join(root, 'docker-stub'); writeFileSync(log, ''); + writeFileSync(pidFile, ''); writeFileSync( binary, '#!/usr/bin/env node\n' + + 'const { appendFileSync } = require("node:fs");\n' + 'const args = process.argv.slice(2);\n' + - `require("node:fs").appendFileSync(${JSON.stringify(log)}, JSON.stringify(args) + "\\n");\n` + - `process.exit(${JSON.stringify(exits)}[args[0]] ?? 0);\n`, + `appendFileSync(${JSON.stringify(log)}, JSON.stringify(args) + "\\n");\n` + + `const outcome = ${JSON.stringify(outcomes)}[args[0]] ?? 0;\n` + + 'if (typeof outcome === "number") process.exit(outcome);\n' + + `if (outcome === "hang") appendFileSync(${JSON.stringify(pidFile)}, process.pid + "\\n");\n` + + 'else process.kill(process.pid, outcome);\n' + + '// Stay until a signal ends the process. A failed test leaves it no longer than this.\n' + + 'setTimeout(() => {}, 30000);\n', ); chmodSync(binary, 0o755); vi.stubEnv('WRANGLER_DOCKER_BIN', binary); @@ -42,6 +61,11 @@ function stubDocker(exits: { build: number; info: number }): { .split('\n') .filter(Boolean) .map((line): unknown => JSON.parse(line)), + hungPid: () => { + const text = readFileSync(pidFile, 'utf8'); + // The line ends once the whole id is written. + return text.endsWith('\n') ? Number(text) : undefined; + }, }; } @@ -58,6 +82,20 @@ function buildArgs(dockerfile: string, context: string): Array { ]; } +/** Wait until the command that hangs runs, and return its process id. */ +async function hung(docker: DockerStub): Promise { + for (let tries = 0; tries < 400; tries += 1) { + const pid = docker.hungPid(); + if (pid !== undefined) { + return pid; + } + await new Promise((resolve) => { + realSetTimeout(resolve, 10); + }); + } + throw new Error('The Docker stand-in did not start in time'); +} + // The stand-in is an executable script, which Windows does not launch by path. describe.skipIf(process.platform === 'win32')('buildContainerImage', () => { it('builds without checking the Docker daemon and removes the local tag on dispose', async () => { @@ -89,11 +127,40 @@ describe.skipIf(process.platform === 'win32')('buildContainerImage', () => { expect(docker.calls()).toEqual([buildArgs(docker.dockerfile, docker.root), ['info']]); }); - it('asks to start Docker when its CLI cannot be launched', async () => { - const docker = stubDocker({ build: 0, info: 0 }); - vi.stubEnv('WRANGLER_DOCKER_BIN', join(docker.root, 'missing-docker')); + it('reports the failed build when `docker info` does not finish in time', async () => { + const docker = stubDocker({ build: 1, info: 'hang' }); + // The deadline is a `setTimeout` in container-image.ts. Reach it without the real wait. + vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] }); - await expect(buildContainerImage(docker.dockerfile)).rejects.toThrow(START_DOCKER); + const failure = expect(buildContainerImage(docker.dockerfile)).rejects.toThrow( + `Sandbox image preparation failed: \`docker build\` exited with code 1 for ${docker.dockerfile}. See the Docker output above.`, + ); + const pid = await hung(docker); + vi.runOnlyPendingTimers(); + await failure; + + expect(docker.calls()).toEqual([buildArgs(docker.dockerfile, docker.root), ['info']]); + // The deadline stopped the command: signal 0 only asks whether the process exists. + expect(() => process.kill(pid, 0)).toThrow(); + }); + + it('names the signal that stopped the build, without asking the daemon', async () => { + const docker = stubDocker({ build: 'SIGTERM' }); + + await expect(buildContainerImage(docker.dockerfile)).rejects.toThrow( + `Sandbox image preparation failed: \`docker build\` was stopped by SIGTERM for ${docker.dockerfile}.`, + ); + expect(docker.calls()).toEqual([buildArgs(docker.dockerfile, docker.root)]); + }); + + it('names the cause when the Docker CLI is missing', async () => { + const docker = stubDocker({ build: 0 }); + const missing = join(docker.root, 'missing-docker'); + vi.stubEnv('WRANGLER_DOCKER_BIN', missing); + + await expect(buildContainerImage(docker.dockerfile)).rejects.toThrow( + `Sandbox image preparation failed: the Docker CLI could not be started (spawn ${missing} ENOENT). Check that Docker is installed and retry the deployment.`, + ); expect(docker.calls()).toEqual([]); }); });