GeezerVison is showing github.com — fix(sdk): handle Windows opencode spawn and shutdown (#20772) · HashNuke/opencode@7b8dc80
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bun.lock +8-2Lines changed: 8 additions & 2 deletions Collapse file Copy file name to clipboardExpand all lines: package.json +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 27 | 27 | "catalog": { | 28 | 28 | "@effect/platform-node": "4.0.0-beta.43", | 29 | 29 | "@types/bun": "1.3.11", | | 30 | + "@types/cross-spawn": "6.0.6", | 30 | 31 | "@octokit/rest": "22.0.0", | 31 | 32 | "@hono/zod-validator": "0.4.2", | 32 | 33 | "ulid": "3.0.1", | | 47 | 48 | "drizzle-orm": "1.0.0-beta.19-d95b7a4", | 48 | 49 | "effect": "4.0.0-beta.43", | 49 | 50 | "ai": "6.0.138", | | 51 | + "cross-spawn": "7.0.6", | 50 | 52 | "hono": "4.10.7", | 51 | 53 | "hono-openapi": "1.1.2", | 52 | 54 | "fuzzysort": "3.1.0", | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/package.json +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 51 | 51 | "@tsconfig/bun": "catalog:", | 52 | 52 | "@types/babel__core": "7.20.5", | 53 | 53 | "@types/bun": "catalog:", | 54 | | - "@types/cross-spawn": "6.0.6", | | 54 | + "@types/cross-spawn": "catalog:", | 55 | 55 | "@types/mime-types": "3.0.1", | 56 | 56 | "@types/npmcli__arborist": "6.3.3", | 57 | 57 | "@types/semver": "^7.5.8", | | 118 | 118 | "bun-pty": "0.4.8", | 119 | 119 | "chokidar": "4.0.3", | 120 | 120 | "clipboardy": "4.0.0", | 121 | | - "cross-spawn": "^7.0.6", | | 121 | + "cross-spawn": "catalog:", | 122 | 122 | "decimal.js": "10.5.0", | 123 | 123 | "diff": "catalog:", | 124 | 124 | "drizzle-orm": "catalog:", | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/util/process.ts +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -144,7 +144,11 @@ export namespace Process { | 144 | 144 | throw new RunFailedError(cmd, out.code, out.stdout, out.stderr) | 145 | 145 | } | 146 | 146 |
| | 147 | + // Duplicated in `packages/sdk/js/src/process.ts` because the SDK cannot import | | 148 | + // `opencode` without creating a cycle. Keep both copies in sync. | 147 | 149 | export async function stop(proc: ChildProcess) { | | 150 | + if (proc.exitCode !== null || proc.signalCode !== null) return | | 151 | + | 148 | 152 | if (process.platform !== "win32" || !proc.pid) { | 149 | 153 | proc.kill() | 150 | 154 | return | |
Collapse file Copy file name to clipboardExpand all lines: packages/sdk/js/package.json +6-3Lines changed: 6 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 | "devDependencies": { | 24 | 24 | "@hey-api/openapi-ts": "0.90.10", | 25 | 25 | "@tsconfig/node22": "catalog:", | | 26 | + "@types/cross-spawn": "catalog:", | 26 | 27 | "@types/node": "catalog:", | 27 | | - "typescript": "catalog:", | 28 | | - "@typescript/native-preview": "catalog:" | | 28 | + "@typescript/native-preview": "catalog:", | | 29 | + "typescript": "catalog:" | 29 | 30 | }, | 30 | | - "dependencies": {} | | 31 | + "dependencies": { | | 32 | + "cross-spawn": "catalog:" | | 33 | + } | 31 | 34 | } |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { type ChildProcess, spawnSync } from "node:child_process" | | 2 | + | | 3 | +// Duplicated from `packages/opencode/src/util/process.ts` because the SDK cannot | | 4 | +// import `opencode` without creating a cycle (`opencode` depends on `@opencode-ai/sdk`). | | 5 | +export function stop(proc: ChildProcess) { | | 6 | + if (proc.exitCode !== null || proc.signalCode !== null) return | | 7 | + if (process.platform === "win32" && proc.pid) { | | 8 | + const out = spawnSync("taskkill", ["/pid", String(proc.pid), "/T", "/F"], { windowsHide: true }) | | 9 | + if (!out.error && out.status === 0) return | | 10 | + } | | 11 | + proc.kill() | | 12 | +} | | 13 | + | | 14 | +export function bindAbort(proc: ChildProcess, signal?: AbortSignal, onAbort?: () => void) { | | 15 | + if (!signal) return () => {} | | 16 | + const abort = () => { | | 17 | + clear() | | 18 | + stop(proc) | | 19 | + onAbort?.() | | 20 | + } | | 21 | + const clear = () => { | | 22 | + signal.removeEventListener("abort", abort) | | 23 | + proc.off("exit", clear) | | 24 | + proc.off("error", clear) | | 25 | + } | | 26 | + signal.addEventListener("abort", abort, { once: true }) | | 27 | + proc.on("exit", clear) | | 28 | + proc.on("error", clear) | | 29 | + if (signal.aborted) abort() | | 30 | + return clear | | 31 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/sdk/js/src/server.ts +25-14Lines changed: 25 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { spawn } from "node:child_process" | | 1 | +import launch from "cross-spawn" | 2 | 2 | import { type Config } from "./gen/types.gen.js" | | 3 | +import { stop, bindAbort } from "./process.js" | 3 | 4 |
| 4 | 5 | export type ServerOptions = { | 5 | 6 | hostname?: string | @@ -31,29 +32,38 @@ export async function createOpencodeServer(options?: ServerOptions) { | 31 | 32 | const args = [`serve`, `--hostname=${options.hostname}`, `--port=${options.port}`] | 32 | 33 | if (options.config?.logLevel) args.push(`--log-level=${options.config.logLevel}`) | 33 | 34 |
| 34 | | - const proc = spawn(`opencode`, args, { | 35 | | - signal: options.signal, | | 35 | + const proc = launch(`opencode`, args, { | 36 | 36 | env: { | 37 | 37 | ...process.env, | 38 | 38 | OPENCODE_CONFIG_CONTENT: JSON.stringify(options.config ?? {}), | 39 | 39 | }, | 40 | 40 | }) | | 41 | + let clear = () => {} | 41 | 42 |
| 42 | 43 | const url = await new Promise<string>((resolve, reject) => { | 43 | 44 | const id = setTimeout(() => { | | 45 | + clear() | | 46 | + stop(proc) | 44 | 47 | reject(new Error(`Timeout waiting for server to start after ${options.timeout}ms`)) | 45 | 48 | }, options.timeout) | 46 | 49 | let output = "" | | 50 | + let resolved = false | 47 | 51 | proc.stdout?.on("data", (chunk) => { | | 52 | + if (resolved) return | 48 | 53 | output += chunk.toString() | 49 | 54 | const lines = output.split("\n") | 50 | 55 | for (const line of lines) { | 51 | 56 | if (line.startsWith("opencode server listening")) { | 52 | 57 | const match = line.match(/on\s+(https?:\/\/[^\s]+)/) | 53 | 58 | if (!match) { | 54 | | - throw new Error(`Failed to parse server url from output: ${line}`) | | 59 | + clear() | | 60 | + stop(proc) | | 61 | + clearTimeout(id) | | 62 | + reject(new Error(`Failed to parse server url from output: ${line}`)) | | 63 | + return | 55 | 64 | } | 56 | 65 | clearTimeout(id) | | 66 | + resolved = true | 57 | 67 | resolve(match[1]!) | 58 | 68 | return | 59 | 69 | } | @@ -74,18 +84,17 @@ export async function createOpencodeServer(options?: ServerOptions) { | 74 | 84 | clearTimeout(id) | 75 | 85 | reject(error) | 76 | 86 | }) | 77 | | - if (options.signal) { | 78 | | - options.signal.addEventListener("abort", () => { | 79 | | - clearTimeout(id) | 80 | | - reject(new Error("Aborted")) | 81 | | - }) | 82 | | - } | | 87 | + clear = bindAbort(proc, options.signal, () => { | | 88 | + clearTimeout(id) | | 89 | + reject(options.signal?.reason) | | 90 | + }) | 83 | 91 | }) | 84 | 92 |
| 85 | 93 | return { | 86 | 94 | url, | 87 | 95 | close() { | 88 | | - proc.kill() | | 96 | + clear() | | 97 | + stop(proc) | 89 | 98 | }, | 90 | 99 | } | 91 | 100 | } | @@ -106,18 +115,20 @@ export function createOpencodeTui(options?: TuiOptions) { | 106 | 115 | args.push(`--agent=${options.agent}`) | 107 | 116 | } | 108 | 117 |
| 109 | | - const proc = spawn(`opencode`, args, { | 110 | | - signal: options?.signal, | | 118 | + const proc = launch(`opencode`, args, { | 111 | 119 | stdio: "inherit", | 112 | 120 | env: { | 113 | 121 | ...process.env, | 114 | 122 | OPENCODE_CONFIG_CONTENT: JSON.stringify(options?.config ?? {}), | 115 | 123 | }, | 116 | 124 | }) | 117 | 125 |
| | 126 | + const clear = bindAbort(proc, options?.signal) | | 127 | + | 118 | 128 | return { | 119 | 129 | close() { | 120 | | - proc.kill() | | 130 | + clear() | | 131 | + stop(proc) | 121 | 132 | }, | 122 | 133 | } | 123 | 134 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/sdk/js/src/v2/server.ts +25-14Lines changed: 25 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { spawn } from "node:child_process" | | 1 | +import launch from "cross-spawn" | 2 | 2 | import { type Config } from "./gen/types.gen.js" | | 3 | +import { stop, bindAbort } from "../process.js" | 3 | 4 |
| 4 | 5 | export type ServerOptions = { | 5 | 6 | hostname?: string | @@ -31,29 +32,38 @@ export async function createOpencodeServer(options?: ServerOptions) { | 31 | 32 | const args = [`serve`, `--hostname=${options.hostname}`, `--port=${options.port}`] | 32 | 33 | if (options.config?.logLevel) args.push(`--log-level=${options.config.logLevel}`) | 33 | 34 |
| 34 | | - const proc = spawn(`opencode`, args, { | 35 | | - signal: options.signal, | | 35 | + const proc = launch(`opencode`, args, { | 36 | 36 | env: { | 37 | 37 | ...process.env, | 38 | 38 | OPENCODE_CONFIG_CONTENT: JSON.stringify(options.config ?? {}), | 39 | 39 | }, | 40 | 40 | }) | | 41 | + let clear = () => {} | 41 | 42 |
| 42 | 43 | const url = await new Promise<string>((resolve, reject) => { | 43 | 44 | const id = setTimeout(() => { | | 45 | + clear() | | 46 | + stop(proc) | 44 | 47 | reject(new Error(`Timeout waiting for server to start after ${options.timeout}ms`)) | 45 | 48 | }, options.timeout) | 46 | 49 | let output = "" | | 50 | + let resolved = false | 47 | 51 | proc.stdout?.on("data", (chunk) => { | | 52 | + if (resolved) return | 48 | 53 | output += chunk.toString() | 49 | 54 | const lines = output.split("\n") | 50 | 55 | for (const line of lines) { | 51 | 56 | if (line.startsWith("opencode server listening")) { | 52 | 57 | const match = line.match(/on\s+(https?:\/\/[^\s]+)/) | 53 | 58 | if (!match) { | 54 | | - throw new Error(`Failed to parse server url from output: ${line}`) | | 59 | + clear() | | 60 | + stop(proc) | | 61 | + clearTimeout(id) | | 62 | + reject(new Error(`Failed to parse server url from output: ${line}`)) | | 63 | + return | 55 | 64 | } | 56 | 65 | clearTimeout(id) | | 66 | + resolved = true | 57 | 67 | resolve(match[1]!) | 58 | 68 | return | 59 | 69 | } | @@ -74,18 +84,17 @@ export async function createOpencodeServer(options?: ServerOptions) { | 74 | 84 | clearTimeout(id) | 75 | 85 | reject(error) | 76 | 86 | }) | 77 | | - if (options.signal) { | 78 | | - options.signal.addEventListener("abort", () => { | 79 | | - clearTimeout(id) | 80 | | - reject(new Error("Aborted")) | 81 | | - }) | 82 | | - } | | 87 | + clear = bindAbort(proc, options.signal, () => { | | 88 | + clearTimeout(id) | | 89 | + reject(options.signal?.reason) | | 90 | + }) | 83 | 91 | }) | 84 | 92 |
| 85 | 93 | return { | 86 | 94 | url, | 87 | 95 | close() { | 88 | | - proc.kill() | | 96 | + clear() | | 97 | + stop(proc) | 89 | 98 | }, | 90 | 99 | } | 91 | 100 | } | @@ -106,18 +115,20 @@ export function createOpencodeTui(options?: TuiOptions) { | 106 | 115 | args.push(`--agent=${options.agent}`) | 107 | 116 | } | 108 | 117 |
| 109 | | - const proc = spawn(`opencode`, args, { | 110 | | - signal: options?.signal, | | 118 | + const proc = launch(`opencode`, args, { | 111 | 119 | stdio: "inherit", | 112 | 120 | env: { | 113 | 121 | ...process.env, | 114 | 122 | OPENCODE_CONFIG_CONTENT: JSON.stringify(options?.config ?? {}), | 115 | 123 | }, | 116 | 124 | }) | 117 | 125 |
| | 126 | + const clear = bindAbort(proc, options?.signal) | | 127 | + | 118 | 128 | return { | 119 | 129 | close() { | 120 | | - proc.kill() | | 130 | + clear() | | 131 | + stop(proc) | 121 | 132 | }, | 122 | 133 | } | 123 | 134 | } |
|