Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/src/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
10 changes: 6 additions & 4 deletions packages/next/src/lib/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
RESTART_EXIT_CODE,
getFormattedDebugAddress,
getNodeDebugType,
isDebugAddressEphemeral,
} from './utils'
import { formatHostname } from './format-hostname'
import { initialize } from './router-server'
Expand Down Expand Up @@ -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}.`)
)
}

Expand Down
7 changes: 7 additions & 0 deletions packages/next/src/server/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions test/integration/cli/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down
Loading