Unverified Commit 80bc175d authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Exit `next dev` with a non-zero code when next.config.js is invalid (#83602)

In `next dev`, we're currently ignoring the exit code of the spawned
child process, and always exiting with `0` instead. We should exit the
process with the child's exit code instead. This is relevant when the
`next.config.js` is invalid, for example.
parent 10745888
...@@ -76,6 +76,10 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => { ...@@ -76,6 +76,10 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
if (sessionStopHandled) return if (sessionStopHandled) return
sessionStopHandled = true sessionStopHandled = true
// Capture the child's exit code if it has already exited and caused the
// session stop (via the 'exit' event), otherwise assume success (0).
const exitCode = child?.exitCode || 0
if ( if (
signal != null && signal != null &&
child?.pid && child?.pid &&
...@@ -150,7 +154,7 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => { ...@@ -150,7 +154,7 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
// the program, or the cursor could remain hidden // the program, or the cursor could remain hidden
process.stdout.write('\x1B[?25h') process.stdout.write('\x1B[?25h')
process.stdout.write('\n') process.stdout.write('\n')
process.exit(0) process.exit(exitCode)
} }
process.on('SIGINT', () => handleSessionStop('SIGINT')) process.on('SIGINT', () => handleSessionStop('SIGINT'))
......
...@@ -367,6 +367,7 @@ export async function startServer( ...@@ -367,6 +367,7 @@ export async function startServer(
// Only load env and config in dev to for logging purposes // Only load env and config in dev to for logging purposes
let envInfo: string[] | undefined let envInfo: string[] | undefined
let experimentalFeatures: ConfiguredExperimentalFeature[] | undefined let experimentalFeatures: ConfiguredExperimentalFeature[] | undefined
try {
if (isDev) { if (isDev) {
const startServerInfo = await getStartServerInfo({ dir, dev: isDev }) const startServerInfo = await getStartServerInfo({ dir, dev: isDev })
envInfo = startServerInfo.envInfo envInfo = startServerInfo.envInfo
...@@ -382,7 +383,6 @@ export async function startServer( ...@@ -382,7 +383,6 @@ export async function startServer(
Log.event(`Starting...`) Log.event(`Starting...`)
try {
let cleanupStarted = false let cleanupStarted = false
let closeUpgraded: (() => void) | null = null let closeUpgraded: (() => void) | null = null
const cleanup = () => { const cleanup = () => {
......
/* eslint-env jest */ /* eslint-env jest */
import { join } from 'path' import { join } from 'path'
import { retry } from 'next-test-utils' import { retry } from 'next-test-utils'
import { nextTestSetup, isNextDev, isNextStart } from 'e2e-utils' import { nextTestSetup, isNextDev, isNextDeploy } from 'e2e-utils'
const installCheckVisible = (browser) => { const installCheckVisible = (browser) => {
return browser.eval(`(function() { return browser.eval(`(function() {
...@@ -19,24 +19,34 @@ const installCheckVisible = (browser) => { ...@@ -19,24 +19,34 @@ const installCheckVisible = (browser) => {
describe('Build Activity Indicator', () => { describe('Build Activity Indicator', () => {
// Use describe.skip so that this suite does not fail with "no tests" during deploy tests. // Use describe.skip so that this suite does not fail with "no tests" during deploy tests.
;(isNextStart ? describe : describe.skip)('Invalid position config', () => { ;(isNextDeploy ? describe.skip : describe)('Invalid position config', () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: join(__dirname, '..'), files: join(__dirname, '..'),
skipStart: true, skipStart: true,
startServerTimeout: 1000,
nextConfig: { nextConfig: {
devIndicators: { devIndicators: {
// Intentionally invalid position to test error // Intentionally invalid position to test error
position: 'ttop-leff' as any, // @ts-expect-error
position: 'ttop-leff',
}, },
}, },
}) })
it('should validate position config', async () => { it('should validate position config', async () => {
if (isNextDev) {
try {
await next.start()
} catch (err) {
expect(err).toEqual(
new Error('next dev exited unexpectedly with code/signal 1')
)
}
} else {
const result = await next.build() const result = await next.build()
expect(result.exitCode).toBe(1) expect(result.exitCode).toBe(1)
expect(result.cliOutput).toContain( }
expect(next.cliOutput).toContain(
`Invalid "devIndicator.position" provided, expected one of top-left, top-right, bottom-left, bottom-right, received ttop-leff` `Invalid "devIndicator.position" provided, expected one of top-left, top-right, bottom-left, bottom-right, received ttop-leff`
) )
}) })
......
...@@ -81,20 +81,24 @@ export class NextDevInstance extends NextInstance { ...@@ -81,20 +81,24 @@ export class NextDevInstance extends NextInstance {
this.emit('stderr', [msg]) this.emit('stderr', [msg])
}) })
const serverReadyTimeoutId = this.setServerReadyTimeout(
reject,
this.startServerTimeout
)
this.childProcess.on('close', (code, signal) => { this.childProcess.on('close', (code, signal) => {
if (this.isStopping) return if (this.isStopping) return
if (code || signal) { if (code || signal) {
require('console').error( this.childProcess = undefined
const error = new Error(
`next dev exited unexpectedly with code/signal ${code || signal}` `next dev exited unexpectedly with code/signal ${code || signal}`
) )
clearTimeout(serverReadyTimeoutId)
require('console').error(error)
reject(error)
} }
}) })
const serverReadyTimeoutId = this.setServerReadyTimeout(
reject,
this.startServerTimeout
)
const readyCb = (msg) => { const readyCb = (msg) => {
const resolveServer = () => { const resolveServer = () => {
clearTimeout(serverReadyTimeoutId) clearTimeout(serverReadyTimeoutId)
......
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