Skip to content

Commit b9a286f

Browse files
cursoragentsayonara
andcommitted
Fix: Handle CLI server spawn errors gracefully
Co-authored-by: sayonara <[email protected]>
1 parent 80105c8 commit b9a286f

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

scripts/dev-watch.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,20 +267,28 @@ function startCliServer() {
267267

268268
/**
269269
* Start the actual CLI server process
270-
* @returns {Promise<BunSubprocess | null>}
270+
* @returns {Promise<BunSubprocess | null>} Returns the subprocess on success, null if spawn fails
271271
*/
272272
function startActualCliServer() {
273273
return new Promise((resolve) => {
274274
log('CLI', 'Starting CLI server process...');
275275

276-
const child = Bun.spawn(['bun', 'dist/index.js', 'start'], {
277-
cwd: cliDir,
278-
stdio: ['inherit', 'inherit', 'inherit'],
279-
env: {
280-
...process.env,
281-
NODE_ENV: 'development',
282-
},
283-
});
276+
/** @type {BunSubprocess} */
277+
let child;
278+
try {
279+
child = Bun.spawn(['bun', 'dist/index.js', 'start'], {
280+
cwd: cliDir,
281+
stdio: ['inherit', 'inherit', 'inherit'],
282+
env: {
283+
...process.env,
284+
NODE_ENV: 'development',
285+
},
286+
});
287+
} catch (error) {
288+
log('CLI', `Failed to spawn CLI server: ${error.message}`);
289+
resolve(null);
290+
return;
291+
}
284292

285293
child.exited.then((exitCode) => {
286294
if (!isShuttingDown && !isRebuilding) {
@@ -294,7 +302,8 @@ function startActualCliServer() {
294302
if (!isShuttingDown && !isRebuilding) {
295303
log('CLI', `CLI server error: ${error.message}`);
296304
cleanup('cli-error');
297-
resolve(null);
305+
// NOTE: We don't resolve(null) here because the Promise is already resolved
306+
// with `child` below. Async errors after process spawn are handled via cleanup().
298307
}
299308
});
300309

0 commit comments

Comments
 (0)