Unverified Commit f771c6d7 authored by Jack Hsu's avatar Jack Hsu Committed by GitHub

fix(cli): respect SIGTERM and SIGINT exit codes in next build (#64871)

This PR updates the SIGTERM and SIGINT exit codes to 143 and 130
respectively. These updated codes are what other tooling, such as
Nx/Turborepo, expects when running commands that did not completely
successfully due to receiving SIGTERM and SIGINT.

For Nx, the SIGINT (e.g. `Ctrl+C`) causes a bad build cache. Both Nx and
Turborepo will have bad cache when SIGTERM is received, which can happen
in a CI environment if a job times out, runs out of memory, etc.

I have a [repo here](https://github.com/jaysoo/next-cli-signals) that
demonstrates the wrong behavior with Turborepo. By exiting with the
wrong code, it can result in bad cache in CI or Docker.

Closes: https://github.com/vercel/next.js/issues/62906

---------
Co-authored-by: 's avatarSam Ko <sam@vercel.com>
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent 4be2fb18
...@@ -24,8 +24,8 @@ export type NextBuildOptions = { ...@@ -24,8 +24,8 @@ export type NextBuildOptions = {
} }
const nextBuild = (options: NextBuildOptions, directory?: string) => { const nextBuild = (options: NextBuildOptions, directory?: string) => {
process.on('SIGTERM', () => process.exit(0)) process.on('SIGTERM', () => process.exit(143))
process.on('SIGINT', () => process.exit(0)) process.on('SIGINT', () => process.exit(130))
const { const {
debug, debug,
......
...@@ -52,7 +52,8 @@ const runAndCaptureOutput = async ({ port }) => { ...@@ -52,7 +52,8 @@ const runAndCaptureOutput = async ({ port }) => {
const testExitSignal = async ( const testExitSignal = async (
killSignal = '', killSignal = '',
args = [], args = [],
readyRegex = /Creating an optimized production/ readyRegex = /Creating an optimized production/,
expectedCode = 0
) => { ) => {
let instance let instance
const killSigint = (inst) => { const killSigint = (inst) => {
...@@ -76,7 +77,7 @@ const testExitSignal = async ( ...@@ -76,7 +77,7 @@ const testExitSignal = async (
// See: https://nodejs.org/api/process.html#process_signal_events // See: https://nodejs.org/api/process.html#process_signal_events
const expectedExitSignal = process.platform === `win32` ? killSignal : null const expectedExitSignal = process.platform === `win32` ? killSignal : null
expect(signal).toBe(expectedExitSignal) expect(signal).toBe(expectedExitSignal)
expect(code).toBe(0) expect(code).toBe(expectedCode)
} }
describe('CLI Usage', () => { describe('CLI Usage', () => {
...@@ -342,11 +343,11 @@ describe('CLI Usage', () => { ...@@ -342,11 +343,11 @@ describe('CLI Usage', () => {
}) })
test('should exit when SIGINT is signalled', async () => { test('should exit when SIGINT is signalled', async () => {
await testExitSignal('SIGINT', ['build', dirBasic]) await testExitSignal('SIGINT', ['build', dirBasic], undefined, 130)
}) })
test('should exit when SIGTERM is signalled', async () => { test('should exit when SIGTERM is signalled', async () => {
await testExitSignal('SIGTERM', ['build', dirBasic]) await testExitSignal('SIGTERM', ['build', dirBasic], undefined, 143)
}) })
test('invalid directory', async () => { test('invalid directory', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment