| 1 | # Plan 013: Use a free port and guaranteed cleanup for build's temporary servers |
| 2 | |
| 3 | > **Executor instructions**: Follow this plan step by step. Run every |
| 4 | > verification command and confirm the expected result. If anything in "STOP |
| 5 | > conditions" occurs, stop and report. When done, update the status row in |
| 6 | > `plans/README.md`. |
| 7 | > |
| 8 | > **Drift check (run first)**: `git diff --stat c63cb120..HEAD -- packages/slidev/node/commands/build.ts` |
| 9 | > On a mismatch with the excerpts below, treat it as a STOP condition. |
| 10 | |
| 11 | ## Status |
| 12 | |
| 13 | - **Priority**: P2 |
| 14 | - **Effort**: S-M |
| 15 | - **Risk**: MED |
| 16 | - **Depends on**: none (pairs well after 007) |
| 17 | - **Category**: bug |
| 18 | - **Planned at**: commit `c63cb120`, 2026-07-10 |
| 19 | |
| 20 | ## Why this matters |
| 21 | |
| 22 | `slidev build` spins up two temporary static servers (og-image generation and |
| 23 | `--download` PDF) on a **hardcoded** port `12445`, with **no `'error'` handler** |
| 24 | and `server.close()` only on the success path. If `12445` is busy (e.g. a |
| 25 | concurrent `slidev export`, which itself uses `getPort(12445)`), the unhandled |
| 26 | `'error'` event crashes the build; and if `exportSlides` throws, the `connect` |
| 27 | server leaks. The rest of the CLI already picks a free port via `getPort`. |
| 28 | |
| 29 | ## Current state |
| 30 | |
| 31 | `packages/slidev/node/commands/build.ts` — two near-identical blocks: |
| 32 | |
| 33 | og-image (`:76-121`): |
| 34 | ```ts |
| 35 | const port = 12445 |
| 36 | const app = connect() |
| 37 | const server = http.createServer(app) |
| 38 | app.use(config.base, sirv(outDir, { etag: true, single: true, dev: true })) |
| 39 | server.listen(port) |
| 40 | |
| 41 | const { exportSlides } = await import('./export') |
| 42 | const tempDir = resolve(outDir, 'temp') |
| 43 | await fs.mkdir(tempDir, { recursive: true }) |
| 44 | await exportSlides({ port, /* ... */ }) |
| 45 | // ... copy png ... |
| 46 | await fs.rm(tempDir, { recursive: true, force: true }) |
| 47 | server.close() |
| 48 | ``` |
| 49 | |
| 50 | `--download` (`:137-156`): |
| 51 | ```ts |
| 52 | const port = 12445 |
| 53 | const app = connect() |
| 54 | const server = http.createServer(app) |
| 55 | app.use(config.base, sirv(outDir, { etag: true, single: true, dev: true })) |
| 56 | server.listen(port) |
| 57 | const filename = options.data.config.exportFilename || 'slidev-exported' |
| 58 | await exportSlides({ port, base: config.base, ...getExportOptions(args, options, join(outDir, `${filename}.pdf`)) }) |
| 59 | server.close() |
| 60 | ``` |
| 61 | |
| 62 | The free-port helper is already imported and used elsewhere: |
| 63 | `packages/slidev/node/cli.ts:13` → `import { getPort } from 'get-port-please'`; |
| 64 | `cli.ts:479` / `:553` → `const candidatePort = await getPort(12445)`. |
| 65 | `get-port-please` is a prod dependency (`pnpm-workspace.yaml`). |
| 66 | |
| 67 | ## Commands you will need |
| 68 | |
| 69 | | Purpose | Command | Expected | |
| 70 | |---------|---------|----------| |
| 71 | | Install | `pnpm install` | exit 0 | |
| 72 | | Build | `pnpm build` | exit 0 | |
| 73 | | Typecheck | `pnpm typecheck` | exit 0 | |
| 74 | | Lint | `pnpm lint` | exit 0 | |
| 75 | |
| 76 | ## Scope |
| 77 | |
| 78 | **In scope**: |
| 79 | - `packages/slidev/node/commands/build.ts` |
| 80 | |
| 81 | **Out of scope**: |
| 82 | - `commands/export.ts` browser teardown (plan 007). |
| 83 | - The `exportFilename` traversal concern (plan 016) — do not add path |
| 84 | confinement here; just don't regress it. |
| 85 | - Behavior of the generated output (og image, download PDF) — keep identical. |
| 86 | |
| 87 | ## Git workflow |
| 88 | |
| 89 | - Branch: `fix/build-temp-server-port`. |
| 90 | - Conventional commit: `fix(build): use free port and always close temp servers`. |
| 91 | - Do NOT push/PR unless instructed. |
| 92 | |
| 93 | ## Steps |
| 94 | |
| 95 | ### Step 1: Import `getPort` |
| 96 | |
| 97 | Add to `build.ts` imports: |
| 98 | ```ts |
| 99 | import { getPort } from 'get-port-please' |
| 100 | ``` |
| 101 | |
| 102 | ### Step 2: og-image block — free port + try/finally |
| 103 | |
| 104 | Replace `const port = 12445` (og-image, `:76`) with `const port = await getPort(12445)`, |
| 105 | and wrap the export + copy + temp-dir cleanup so the server is always closed: |
| 106 | ```ts |
| 107 | const port = await getPort(12445) |
| 108 | const app = connect() |
| 109 | const server = http.createServer(app) |
| 110 | app.use(config.base, sirv(outDir, { etag: true, single: true, dev: true })) |
| 111 | server.listen(port) |
| 112 | try { |
| 113 | const { exportSlides } = await import('./export') |
| 114 | const tempDir = resolve(outDir, 'temp') |
| 115 | await fs.mkdir(tempDir, { recursive: true }) |
| 116 | await exportSlides({ port, /* ...unchanged... */ }) |
| 117 | const tempFiles = await fs.readdir(tempDir) |
| 118 | const pngFile = tempFiles.find(file => file.endsWith('.png')) |
| 119 | if (pngFile) { |
| 120 | const generatedPath = resolve(tempDir, pngFile) |
| 121 | await fs.copyFile(generatedPath, projectOgImagePath) |
| 122 | await fs.copyFile(generatedPath, outputOgImagePath) |
| 123 | } |
| 124 | await fs.rm(tempDir, { recursive: true, force: true }) |
| 125 | } |
| 126 | finally { |
| 127 | server.close() |
| 128 | } |
| 129 | ``` |
| 130 | Keep the `exportSlides({ ... })` argument object exactly as it is today. |
| 131 | |
| 132 | ### Step 3: `--download` block — free port + try/finally |
| 133 | |
| 134 | Replace `const port = 12445` (`:137`) with `const port = await getPort(12445)` |
| 135 | and wrap the `exportSlides(...)` call in `try { ... } finally { server.close() }`. |
| 136 | |
| 137 | **Verify**: `grep -n "12445" packages/slidev/node/commands/build.ts` shows both |
| 138 | occurrences now inside `getPort(12445)`; `grep -n "server.close()" build.ts` |
| 139 | shows both inside `finally` blocks. |
| 140 | |
| 141 | ### Step 4: Build / typecheck / lint |
| 142 | |
| 143 | **Verify**: `pnpm build && pnpm typecheck && pnpm lint` exit 0. |
| 144 | |
| 145 | ## Test plan |
| 146 | |
| 147 | - `build` needs Playwright + a full Vite build, so no unit test is added |
| 148 | (consistent with `build.ts` having none). Verification is structural |
| 149 | (free-port + finally-close, Step 3 grep) plus build/typecheck. If a local |
| 150 | environment with `playwright-chromium` exists, optionally run |
| 151 | `pnpm demo:build` (or `slidev build` on the demo with `download: true`) and |
| 152 | confirm the PDF is produced and no port error occurs when run twice |
| 153 | concurrently. |
| 154 | |
| 155 | ## Done criteria |
| 156 | |
| 157 | - [ ] Both temp servers bind to `await getPort(12445)`, not a hardcoded literal |
| 158 | - [ ] Both `server.close()` calls are in `finally` blocks |
| 159 | - [ ] Output artifacts (og image, download PDF) are produced exactly as before |
| 160 | - [ ] `pnpm build`, `pnpm typecheck`, `pnpm lint` exit 0 |
| 161 | - [ ] Only `build.ts` modified (`git status`) |
| 162 | - [ ] `plans/README.md` status row updated |
| 163 | |
| 164 | ## STOP conditions |
| 165 | |
| 166 | Stop and report if: |
| 167 | |
| 168 | - `getPort`'s chosen port is not actually threaded into `exportSlides` (the |
| 169 | `port` variable must be the one passed) — verify both call sites pass the new |
| 170 | `port`. |
| 171 | - The og-image/download output changes shape or location after the refactor. |
| 172 | |
| 173 | ## Maintenance notes |
| 174 | |
| 175 | - If both blocks are later unified into a helper (`serveOutDir(outDir, base)`), |
| 176 | keep the free-port + finally-close semantics. |
| 177 | - Reviewer: confirm no `'error'`-less `server.listen` remains and that |
| 178 | concurrent builds/exports no longer collide on `12445`. |
| 179 |