History

fix(selfhost): ship @cloudflare/worker-bundler in the docker runtime (#1390)

The dynamic Worker bundler change made plugin-apps resolve @cloudflare/worker-bundler at runtime via import.meta.resolve, but package-runtime.ts never copied the package into the self-host image, so serve.js crashed on startup with a module-not-found error. Copy the package alongside the other runtime externals and pre-bundle dist/index.bundled.js at build time (the npm package does not ship it and the distroless runtime has no esbuild for the fallback), mirroring what apps/cli/src/build.ts does for the packed CLI. The CI smoke job only built the image, which is exactly why this slipped through: boot the container and require a healthy /api/health as well.

Rhys Sullivan committed 4 months ago

Commit d95e63c1 parent 9e389283 files · +66 −1Browse files

Unified · Split

3 files
  1. .changeset/selfhost-worker-bundler-runtime.md +5 −0
  2. .github/workflows/ci.yml +31 −0
  3. apps/host-selfhost/scripts/package-runtime.ts +30 −1
.changeset/selfhost-worker-bundler-runtime.md+5−0
@@ -0,0 +1,5 @@
1+---
2+"executor": patch
3+---
4+
5+Ship @cloudflare/worker-bundler in the self-host Docker runtime so the server starts; it was resolved at runtime since the dynamic Worker bundler change but never copied into the image.
.github/workflows/ci.yml+31−0
@@ -414,4 +414,35 @@ jobs: · 413 unmodified lines
414414 context: .
415415 file: apps/host-selfhost/Dockerfile
416416 push: false
417+ load: true
417418 tags: executor-selfhost:ci
419+
420+ # Building alone doesn't catch startup crashes (e.g. a runtime-resolved
421+ # package missing from the packaged runtime), so boot the container and
422+ # require a healthy /api/health before calling the image good.
423+ - name: Boot image and check health
424+ shell: bash
425+ run: |
426+ set -euo pipefail
427+ docker run -d --name selfhost-smoke -p 4788:4788 \
428+ -e BETTER_AUTH_SECRET="$(openssl rand -hex 32)" \
429+ -e EXECUTOR_BOOTSTRAP_ADMIN_EMAIL=smoke@example.com \
430+ -e EXECUTOR_BOOTSTRAP_ADMIN_PASSWORD="$(openssl rand -hex 16)" \
431+ -e EXECUTOR_WEB_BASE_URL=http://localhost:4788 \
432+ executor-selfhost:ci
433+ for i in $(seq 1 30); do
434+ if [ "$(docker inspect -f '{{.State.Running}}' selfhost-smoke)" != "true" ]; then
435+ echo "container exited during startup"
436+ docker logs selfhost-smoke
437+ exit 1
438+ fi
439+ if curl -fsS http://localhost:4788/api/health >/dev/null 2>&1; then
440+ echo "healthy after ${i}s"
441+ docker logs selfhost-smoke
442+ exit 0
443+ fi
444+ sleep 1
445+ done
446+ echo "health check never passed"
447+ docker logs selfhost-smoke
448+ exit 1
apps/host-selfhost/scripts/package-runtime.ts+30−1
@@ -1,6 +1,7 @@
1−import { cpSync, existsSync, mkdirSync, rmSync } from "node:fs";
1+import { cpSync, existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
22 import { dirname, join } from "node:path";
33 import { createRequire } from "node:module";
4+import { pathToFileURL } from "node:url";
45
56 const root = process.cwd();
67 const out = join(root, ".selfhost-runtime");
@@ -36,6 +37,7 @@ const libsqlNativePackage = (): string => { · 29 unmodified lines
3637 };
3738
3839 const externalPackages = [
40+ "@cloudflare/worker-bundler",
3941 "quickjs-emscripten",
4042 "quickjs-emscripten-core",
4143 "@jitl/quickjs-ffi-types",
@@ -64,12 +66,39 @@ const copyPackage = (name: string): void => { · 22 unmodified lines
6466 cpSync(packageDir(name), destination, { recursive: true, dereference: true });
6567 };
6668
69+// The worker-bundler backend (packages/plugins/apps/src/pipeline/
70+// worker-bundler-artifact.ts) loads `dist/index.bundled.js` from the resolved
71+// package, falling back to bundling `dist/index.js` with esbuild at runtime.
72+// The npm package doesn't ship the bundled entry and the runtime image has no
73+// esbuild, so produce it here, the same way apps/cli/src/build.ts does for the
74+// packed CLI.
75+const writeBundledWorkerBundler = async (): Promise<void> => {
76+ const distPath = join(packageDir("@cloudflare/worker-bundler"), "dist");
77+ const esbuildEntry = requireFromSelfHost.resolve("esbuild", {
78+ paths: [join(root, "node_modules/.bun/node_modules")],
79+ });
80+ const { build } = await import(pathToFileURL(esbuildEntry).href);
81+ const result = await build({
82+ entryPoints: [join(distPath, "index.js")],
83+ bundle: true,
84+ format: "esm",
85+ platform: "browser",
86+ external: ["./esbuild.wasm"],
87+ logLevel: "silent",
88+ write: false,
89+ });
90+ const source = result.outputFiles[0]?.text;
91+ if (source === undefined) throw new Error("failed to bundle @cloudflare/worker-bundler");
92+ writeFileSync(join(out, "node_modules/@cloudflare/worker-bundler/dist/index.bundled.js"), source);
93+};
94+
6795 rmSync(out, { recursive: true, force: true });
6896 mkdirSync(serverOut, { recursive: true });
6997
7098 await Bun.$`bun build apps/host-selfhost/src/serve.ts --target=bun --format=esm --outdir=${serverOut} ${quickJsExternals.map((name) => `--external=${name}`)}`;
7199
72100 for (const name of externalPackages) copyPackage(name);
101+await writeBundledWorkerBundler();
73102
74103 if (!existsSync(join(serverOut, "serve.js"))) {
75104 throw new Error(

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t