|
| 1 | +import { spawn } from "child_process"; |
| 2 | +import path from "path"; |
| 3 | +import { setTimeout as sleep } from "timers/promises"; |
| 4 | + |
| 5 | +// Root `pnpm run dev` starts eager generators and package watch builds in |
| 6 | +// parallel. `generate-openapi-docs:watch` intentionally runs `codegen-docs` |
| 7 | +// once before starting chokidar, because chokidar only responds to future file |
| 8 | +// changes. Without that initial run, dev docs could serve stale OpenAPI JSON |
| 9 | +// from a previous branch, or no generated JSON at all after a clean checkout, |
| 10 | +// until someone edits an API route. |
| 11 | +// |
| 12 | +// That eager OpenAPI generation imports backend modules, and some of those |
| 13 | +// backend modules resolve workspace packages through their built `dist` |
| 14 | +// entrypoints. Package watch scripts update those entrypoints with |
| 15 | +// `tsdown --watch`, but on a cold checkout, after `pnpm clean`, or during the |
| 16 | +// first package watcher build, the entrypoints may not exist yet even though |
| 17 | +// `tsdown --watch` is about to create them. |
| 18 | +// |
| 19 | +// We keep this wait scoped to the eager generator rather than putting it in |
| 20 | +// front of backend `dev`: the long-running Next dev server can tolerate package |
| 21 | +// watchers warming up, while a one-shot generator exits immediately on a missing |
| 22 | +// import and `concurrently -k` then tears down the whole dev command. Package |
| 23 | +// watch scripts also avoid deleting `dist` in dev mode, which removes the |
| 24 | +// common restart race; this probe covers the remaining cold-start case. |
| 25 | +// |
| 26 | +// This probe waits only for the package imports that the backend-side generator |
| 27 | +// needs. It does not hide real runtime errors: we retry missing-module failures |
| 28 | +// while package builds warm up, and fail immediately for other import failures. |
| 29 | +const repoRoot = path.resolve(__dirname, ".."); |
| 30 | +const backendDir = path.join(repoRoot, "apps/backend"); |
| 31 | +const timeoutMs = 60_000; |
| 32 | +const retryDelayMs = 1_000; |
| 33 | + |
| 34 | +const probeScript = ` |
| 35 | +(async () => { |
| 36 | + await import('@stackframe/stack'); |
| 37 | + await import('@stackframe/stack-shared/dist/utils/env'); |
| 38 | +})().then( |
| 39 | + () => undefined, |
| 40 | + (error) => { |
| 41 | + console.error(error); |
| 42 | + process.exit(1); |
| 43 | + }, |
| 44 | +); |
| 45 | +`; |
| 46 | + |
| 47 | +type ProbeResult = { |
| 48 | + exitCode: number | null, |
| 49 | + output: string, |
| 50 | +}; |
| 51 | + |
| 52 | +function runProbe(): Promise<ProbeResult> { |
| 53 | + return new Promise((resolve, reject) => { |
| 54 | + const child = spawn("pnpm", ["exec", "tsx", "-e", probeScript], { |
| 55 | + cwd: backendDir, |
| 56 | + env: process.env, |
| 57 | + stdio: ["ignore", "pipe", "pipe"], |
| 58 | + }); |
| 59 | + |
| 60 | + let output = ""; |
| 61 | + |
| 62 | + child.stdout.setEncoding("utf8"); |
| 63 | + child.stderr.setEncoding("utf8"); |
| 64 | + child.stdout.on("data", (chunk: string) => { |
| 65 | + output += chunk; |
| 66 | + }); |
| 67 | + child.stderr.on("data", (chunk: string) => { |
| 68 | + output += chunk; |
| 69 | + }); |
| 70 | + |
| 71 | + child.on("error", reject); |
| 72 | + child.on("close", (exitCode) => { |
| 73 | + resolve({ exitCode, output }); |
| 74 | + }); |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +function isMissingModuleError(output: string) { |
| 79 | + return output.includes("ERR_MODULE_NOT_FOUND") || output.includes("MODULE_NOT_FOUND"); |
| 80 | +} |
| 81 | + |
| 82 | +async function main() { |
| 83 | + const start = performance.now(); |
| 84 | + let lastOutput = ""; |
| 85 | + let hasLoggedWait = false; |
| 86 | + let isReady = false; |
| 87 | + |
| 88 | + while (performance.now() - start < timeoutMs) { |
| 89 | + const result = await runProbe(); |
| 90 | + if (result.exitCode === 0) { |
| 91 | + isReady = true; |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + lastOutput = result.output; |
| 96 | + if (!isMissingModuleError(result.output)) { |
| 97 | + throw new Error(`Dev package import probe failed with a non-retryable error:\n${result.output}`); |
| 98 | + } |
| 99 | + |
| 100 | + if (!hasLoggedWait) { |
| 101 | + console.log("Waiting for dev package entrypoints to be generated..."); |
| 102 | + hasLoggedWait = true; |
| 103 | + } |
| 104 | + await sleep(retryDelayMs); |
| 105 | + } |
| 106 | + |
| 107 | + if (!isReady) { |
| 108 | + throw new Error(`Timed out waiting for dev package imports to become available. Last probe output:\n${lastOutput}`); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +main().then( |
| 113 | + () => undefined, |
| 114 | + (error) => { |
| 115 | + console.error(error); |
| 116 | + process.exit(1); |
| 117 | + }, |
| 118 | +); |
0 commit comments