Unverified Commit 04c962cf authored by Caleb Webber's avatar Caleb Webber Committed by GitHub

use port zero for --inspect when forking, if used by parent (#85128)

Co-authored-by: 's avatarSebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
parent 30503c14
......@@ -282,7 +282,7 @@ const nextDev = async (
if (nodeDebugType) {
const address = getParsedDebugAddress()
address.port = address.port + 1
address.port = address.port === 0 ? 0 : address.port + 1
nodeOptions[nodeDebugType] = formatDebugAddress(address)
}
......
......@@ -85,10 +85,12 @@ export class Worker {
if (nodeDebugType) {
const address = getParsedDebugAddress()
address.port =
address.port +
// current process runs on `address.port`
1 +
debuggerPortOffset
address.port === 0
? 0
: address.port +
// current process runs on `address.port`
1 +
debuggerPortOffset
nodeOptions[nodeDebugType] = formatDebugAddress(address)
}
}
......
......@@ -26,6 +26,7 @@ import {
RESTART_EXIT_CODE,
getFormattedDebugAddress,
getNodeDebugType,
isDebugAddressEphemeral,
} from './utils'
import { formatHostname } from './format-hostname'
import { initialize } from './router-server'
......@@ -347,8 +348,12 @@ export async function startServer(
if (nodeDebugType) {
const formattedDebugAddress = getFormattedDebugAddress()
const isEphemeral = isDebugAddressEphemeral()
Log.info(
`the --${nodeDebugType} option was detected, the Next.js router server should be inspected at ${formattedDebugAddress}.`
`the --${nodeDebugType} option was detected` +
(isEphemeral
? ''
: `, the Next.js router server should be inspected at ${formattedDebugAddress}.`)
)
}
......
......@@ -171,6 +171,13 @@ export const getParsedDebugAddress = (): DebugAddress => {
return { host: undefined, port: parseInt(address, 10) }
}
/**
* Checks if the debug address from `NODE_OPTIONS` specifies to use a random port (e.g., the port set to 0).
*
* @returns A boolean indicating whether or not the debug address is assigned to a random port.
*/
export const isDebugAddressEphemeral = () => getParsedDebugAddress()?.port === 0
/**
* Get the debug address from the `NODE_OPTIONS` environment variable and format
* it into a string.
......
......@@ -571,6 +571,35 @@ describe('CLI Usage', () => {
}
})
test("NODE_OPTIONS='--inspect=:0'", async () => {
const port = await findPort()
let output = ''
let errOutput = ''
const app = await runNextCommandDev(
[dirBasic, '--port', port],
undefined,
{
onStdout(msg) {
output += stripAnsi(msg)
},
onStderr(msg) {
errOutput += stripAnsi(msg)
},
env: { NODE_OPTIONS: '--inspect=:0' },
}
)
try {
await check(() => output, new RegExp(`http://localhost:${port}`))
await check(() => errOutput, /Debugger listening on/)
expect(errOutput).not.toContain('address already in use')
expect(errOutput).toContain('Debugger listening on')
console.log(output)
expect(output).toContain('the --inspect option was detected')
} finally {
await killApp(app)
}
})
test("NODE_OPTIONS='--require=file with spaces to-require-with-node-require-option.js'", async () => {
const port = await findPort()
let output = ''
......
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