From cc55e36dd39fef134568f53acc66514cbb7175ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Mon, 25 Nov 2024 10:51:30 +0200 Subject: [PATCH] fix: exit code on `SIGTERM` (#18741) --- packages/vite/src/node/preview.ts | 3 ++- packages/vite/src/node/server/index.ts | 3 ++- packages/vite/src/node/utils.ts | 6 ++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index f149fac30d53d5..2ff3984515a292 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -168,10 +168,11 @@ export async function preview( }, } - const closeServerAndExit = async () => { + const closeServerAndExit = async (_: unknown, exitCode?: number) => { try { await server.close() } finally { + process.exitCode ??= exitCode ? 128 + exitCode : undefined process.exit() } } diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index c7b4df4922a7e7..f8fb4386d89dfc 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -729,10 +729,11 @@ export async function _createServer( }, }) - const closeServerAndExit = async () => { + const closeServerAndExit = async (_: unknown, exitCode?: number) => { try { await server.close() } finally { + process.exitCode ??= exitCode ? 128 + exitCode : undefined process.exit() } } diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 0a9d1e37e3f31b..794a333276782f 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1524,7 +1524,9 @@ export function partialEncodeURIPath(uri: string): string { return filePath.replaceAll('%', '%25') + postfix } -export const setupSIGTERMListener = (callback: () => Promise): void => { +export const setupSIGTERMListener = ( + callback: (signal?: 'SIGTERM', exitCode?: number) => Promise, +): void => { process.once('SIGTERM', callback) if (process.env.CI !== 'true') { process.stdin.on('end', callback) @@ -1532,7 +1534,7 @@ export const setupSIGTERMListener = (callback: () => Promise): void => { } export const teardownSIGTERMListener = ( - callback: () => Promise, + callback: Parameters[0], ): void => { process.off('SIGTERM', callback) if (process.env.CI !== 'true') {