Unverified Commit bdfe247b authored by Sam Ko's avatar Sam Ko Committed by GitHub

chore(cli): fix allowRetry when using port 3000 (#62840)

parent 6045048b
...@@ -170,9 +170,12 @@ program ...@@ -170,9 +170,12 @@ program
'Reports a subset of the debugging trace to a remote HTTP URL. Includes sensitive data.' 'Reports a subset of the debugging trace to a remote HTTP URL. Includes sensitive data.'
) )
.addOption(new Option('--experimental-test-proxy').hideHelp()) .addOption(new Option('--experimental-test-proxy').hideHelp())
.action((directory, options) => .action((directory, options, { _optionValueSources }) => {
import('../cli/next-dev.js').then((mod) => mod.nextDev(options, directory)) const portSource = _optionValueSources.port
) import('../cli/next-dev.js').then((mod) =>
mod.nextDev(options, portSource, directory)
)
})
.usage('[directory] [options]') .usage('[directory] [options]')
program program
......
...@@ -46,6 +46,8 @@ type NextDevOptions = { ...@@ -46,6 +46,8 @@ type NextDevOptions = {
experimentalTestProxy?: boolean experimentalTestProxy?: boolean
} }
type PortSource = 'cli' | 'default' | 'env'
let dir: string let dir: string
let child: undefined | ChildProcess let child: undefined | ChildProcess
let config: NextConfigComplete let config: NextConfigComplete
...@@ -127,7 +129,11 @@ process.on('SIGTERM', () => handleSessionStop('SIGKILL')) ...@@ -127,7 +129,11 @@ process.on('SIGTERM', () => handleSessionStop('SIGKILL'))
// exit event must be synchronous // exit event must be synchronous
process.on('exit', () => child?.kill('SIGKILL')) process.on('exit', () => child?.kill('SIGKILL'))
const nextDev = async (options: NextDevOptions, directory?: string) => { const nextDev = async (
options: NextDevOptions,
portSource: PortSource,
directory?: string
) => {
dir = getProjectDir(process.env.NEXT_PRIVATE_DEV_DIR || directory) dir = getProjectDir(process.env.NEXT_PRIVATE_DEV_DIR || directory)
// Check if pages dir exists and warn if not // Check if pages dir exists and warn if not
...@@ -180,8 +186,7 @@ const nextDev = async (options: NextDevOptions, directory?: string) => { ...@@ -180,8 +186,7 @@ const nextDev = async (options: NextDevOptions, directory?: string) => {
} }
// If neither --port nor PORT were specified, it's okay to retry new ports. // If neither --port nor PORT were specified, it's okay to retry new ports.
const allowRetry = const allowRetry = portSource === 'default'
options.port === undefined && process.env.PORT === undefined
// We do not set a default host value here to prevent breaking // We do not set a default host value here to prevent breaking
// some set-ups that rely on listening on other interfaces // some set-ups that rely on listening on other interfaces
......
...@@ -579,6 +579,26 @@ describe('CLI Usage', () => { ...@@ -579,6 +579,26 @@ describe('CLI Usage', () => {
expect(stripAnsi(stdout).trim()).toBeFalsy() expect(stripAnsi(stdout).trim()).toBeFalsy()
}) })
test('Allow retry if port 3000 is already in use', async () => {
let output = ''
let appOne
let appTwo
try {
appOne = await runNextCommandDev([dirBasic], undefined, {})
appTwo = await runNextCommandDev([dirBasic], undefined, {
onStderr(msg) {
output += stripAnsi(msg)
},
})
} finally {
await killApp(appOne).catch(console.error)
await killApp(appTwo).catch(console.error)
}
expect(output).toMatch('⚠ Port 3000 is in use, trying 3001 instead.')
})
test('-p reserved', async () => { test('-p reserved', async () => {
const TCP_MUX_PORT = 1 const TCP_MUX_PORT = 1
const { stderr, stdout } = await runAndCaptureOutput({ const { stderr, stdout } = await runAndCaptureOutput({
......
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