import { fileURLToPath } from "node:url"; /** * Child-process isolation for framework production builds. * * Framework builds load and execute USER code — `astro.config.*`, * `nuxt.config.ts` modules, `vite.config.*` plugins, `open-next.config.ts` — * which may read `process.cwd()`, mutate `process.env`, or `process.chdir`. * Some toolchains additionally require `cwd === project root` outright * (kit's postbuild workers re-derive `outDir` from the cwd; waku/rolldown * resolve inputs from the cwd). None of that may run inside the engine * process, where many deploys share one event loop. The same class of * problem alchemy solved for Vite (#1102) and Next.js (`runner.mjs`). * * Instead, every production build runs in a disposable child process whose * working directory IS the project root — no chdir anywhere, no lock, * unbounded build concurrency: * * - The **parent** ({@link runBuildChild}) spawns the shared * `core/BuildChildRunner.ts` entry, forwards the build logs, and reads the * {@link BuildOutput} back from a temp `build.json` (`readBuildOutput` * revives Buffers and the workspaces Set). * - The **child** (`BuildChildRunner`) imports the framework's source module * and calls its exported `buildInChild(config)` — a PURE function * returning an Effect (see {@link BuildChildModule}), so importing any * source module never has side effects; process concerns (argv, exit * codes) live only in the runner entry, which is never imported. */ import * as NodeChildProcessSpawner from "@effect/platform-node/NodeChildProcessSpawner"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; import type { PlatformError } from "effect/PlatformError"; import * as ChildProcess from "effect/process/ChildProcess"; import * as Redacted from "effect/Redacted"; import * as Stream from "effect/Stream"; import { readBuildOutput } from "./BuildOutput.ts"; import type { BuildOutput } from "./BuildOutput.ts"; import { logChildOutput } from "./ChildOutput.ts"; import { childCommand, type ChildRuntime } from "./ChildRuntime.ts"; import { FrameworkError } from "./Framework.ts"; /** * The contract a framework source module fulfills to be buildable in a * child: a pure `buildInChild` export that turns the JSON config into the * build Effect. Tests can call it directly in-process; `runBuildChild` * executes it in the shared runner entry. */ export interface BuildChildModule { readonly buildInChild: ( config: never, ) => Effect.Effect; } /** The runner's `argv[2]` payload (JSON). */ export interface BuildChildPayload { /** File URL of the framework source module exporting `buildInChild`. */ readonly module: string; /** JSON-serializable config handed to the module's `buildInChild`. */ readonly config: unknown; /** Where the runner persists the resulting `BuildOutput` (build.json). */ readonly outputPath: string; /** Where the runner persists the failure message when the build fails. */ readonly errorPath: string; } export interface BuildChildOptions { /** The runtime the child runs under. @default this process's runtime */ readonly runtime?: ChildRuntime | undefined; /** * File URL of the module exporting `buildInChild` — pass * `import.meta.url`. The shared runner entry (resolved as a sibling of * this module's `core/` directory) imports it in the child. */ readonly module: string; /** Project root — becomes the child's working directory. */ readonly rootDir: string; /** JSON-serializable config handed to the module's `buildInChild`. */ readonly config: unknown; /** Framework name for error attribution (e.g. "sveltekit", "waku"). */ readonly framework: string; /** * Extra process env for the child only. Merged over the parent's env * at spawn time so Vite/nitro/`import.meta.env` see site `env` without * the parent mutating `process.env`. NODE_ENV defaults to production; * an explicit value here overrides that default (plugins in the child may still * mutate theirs — that is why this is a child). */ readonly env?: Record | undefined; } /** * Resolve a site's raw `env` into the plain strings a build child sees on * `process.env`, so frameworks inline declared public values * (`NEXT_PUBLIC_*`, `PUBLIC_*`, `VITE_*`, ...) at build time. Strings pass * through, `Redacted`s are unwrapped, numbers and booleans are * stringified; bindings and other values are dropped. */ export const resolveBuildEnv = ( env: Record | undefined, ): Record => { const out: Record = {}; for (const [key, value] of Object.entries(env ?? {})) { if (typeof value === "string") { out[key] = value; } else if (Redacted.isRedacted(value) && typeof Redacted.value(value) === "string") { out[key] = Redacted.value(value) as string; } else if (typeof value === "number" || typeof value === "boolean") { out[key] = String(value); } } return out; }; /** * Run a framework production build in a disposable child process with * `cwd = rootDir` and return its {@link BuildOutput}. * * Build output becomes log records — NOT `stdio: "inherit"` — so the * process that renders logs shows it and in-process capture (a test * runner's log file) sees it. */ export const runBuildChild = ( options: BuildChildOptions, ): Effect.Effect => Effect.scoped( Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const fail = (message: string) => (cause?: unknown): FrameworkError => new FrameworkError({ framework: options.framework, message, cause }); const outputDir = yield* fs .makeTempDirectoryScoped({ prefix: "alchemy-framework-build-" }) .pipe(Effect.mapError(fail("Failed to create the build child's temp directory"))); const outputPath = path.join(outputDir, "build.json"); const errorPath = path.join(outputDir, "error.txt"); // The runner entry lives beside this module's core/ directory in both // layouts (src/{fw}/source.ts → src/core/BuildChildRunner.ts, // dist/{fw}/source.js → dist/core/BuildChildRunner.js), so it resolves // relative to the FRAMEWORK module's own stable URL. const entry = fileURLToPath( new URL( options.module.endsWith(".ts") ? "../core/BuildChildRunner.ts" : "../core/BuildChildRunner.js", options.module, ), ); const { executable, args: runnerArgs } = childCommand(options.runtime, entry); const payload: BuildChildPayload = { module: options.module, config: options.config, outputPath, errorPath, }; const args = [...runnerArgs, JSON.stringify(payload)]; const spawnerLayer = NodeChildProcessSpawner.layer.pipe( Layer.provide( Layer.merge(Layer.succeed(FileSystem.FileSystem)(fs), Layer.succeed(Path.Path)(path)), ), ); const exitCode = yield* Effect.gen(function* () { const child = yield* ChildProcess.make(executable, args, { cwd: options.rootDir, stdin: "ignore", stdout: "pipe", stderr: "pipe", // Default builds to production rather than inheriting the CLI/test // runner's mode, while preserving deliberate build-env overrides. env: { ...process.env, NODE_ENV: "production", ...options.env }, }).pipe( Effect.mapError( fail(`Failed to spawn the ${options.framework} build child (${executable})`), ), ); const forward = ( stream: Stream.Stream, name: "stdout" | "stderr", ) => stream.pipe( Stream.decodeText, Stream.splitLines, Stream.runForEach((line) => logChildOutput(line, name)), ); const { code } = yield* Effect.all( { code: child.exitCode, stdout: forward(child.stdout, "stdout"), stderr: forward(child.stderr, "stderr"), }, { concurrency: "unbounded" }, ).pipe( Effect.mapError(fail(`Failed reading the ${options.framework} build child's output`)), ); return code; }).pipe(Effect.provide(spawnerLayer)); if (exitCode !== 0) { // The child's own failure message (e.g. a framework config the target // rejects) beats the bare exit code; its stack already went to stderr. const reason = yield* fs .readFileString(errorPath) .pipe(Effect.orElseSucceed(() => undefined)); return yield* fail( reason ?? `The ${options.framework} build child exited with code ${exitCode}`, )(undefined); } return yield* readBuildOutput(outputPath).pipe( Effect.mapError( fail( `The ${options.framework} build child exited 0 but produced no readable build output`, ), ), ); }), );