Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NODE_OPTIONS='--inspect' not running expected #51467

Merged
merged 4 commits into from
Jun 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { isIPv6 } from 'net'
import * as Log from '../../build/output/log'
import { normalizeRepeatedSlashes } from '../../shared/lib/utils'
import { initialEnv } from '@next/env'
import { genRouterWorkerExecArgv, getNodeOptionsWithoutInspect } from './utils'
import {
genRouterWorkerExecArgv,
getDebugPort,
getNodeOptionsWithoutInspect,
} from './utils'

export interface StartServerOptions {
dir: string
Expand Down Expand Up @@ -155,6 +159,15 @@ export async function startServer({

const appUrl = `http://${host}:${port}`

if (isNodeDebugging) {
const debugPort = getDebugPort()
Log.info(
`the --inspect${
isNodeDebugging === 'brk' ? '-brk' : ''
} option was detected, the Next.js proxy server should be inspected at port ${debugPort}.`
)
}

Log.ready(
`started server on ${normalizedHostname}${
(port + '').startsWith(':') ? '' : ':'
Expand Down
28 changes: 25 additions & 3 deletions packages/next/src/server/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type arg from 'next/dist/compiled/arg/index.js'
import { getFreePort } from './worker-utils'
import * as Log from '../../build/output/log'

export function printAndExit(message: string, code = 1) {
if (code === 0) {
Expand All @@ -11,6 +11,19 @@ export function printAndExit(message: string, code = 1) {
process.exit(code)
}

export const getDebugPort = () => {
const debugPortStr =
process.execArgv
.find(
(localArg) =>
localArg.startsWith('--inspect') ||
localArg.startsWith('--inspect-brk')
)
?.split('=')[1] ??
process.env.NODE_OPTIONS?.match?.(/--inspect(-brk)?(=(\S+))?( |$)/)?.[3]
return debugPortStr ? parseInt(debugPortStr, 10) : 9229
}

export const genRouterWorkerExecArgv = async (
isNodeDebugging: boolean | 'brk'
) => {
Expand All @@ -21,9 +34,18 @@ export const genRouterWorkerExecArgv = async (
})

if (isNodeDebugging) {
const debugPort = await getFreePort()
const isDebuggingWithBrk = isNodeDebugging === 'brk'

let debugPort = getDebugPort() + 1

Log.info(
`the --inspect${
isDebuggingWithBrk ? '-brk' : ''
} option was detected, the Next.js routing server should be inspected at port ${debugPort}.`
)

execArgv.push(
`--inspect${isNodeDebugging === 'brk' ? '-brk' : ''}=${debugPort + 1}`
`--inspect${isNodeDebugging === 'brk' ? '-brk' : ''}=${debugPort}`
)
}

Expand Down
8 changes: 5 additions & 3 deletions packages/next/src/server/lib/worker-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Log from '../../build/output/log'
import http from 'http'
import { getDebugPort } from './utils'

export const getFreePort = async (): Promise<number> => {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -28,7 +29,6 @@ export const genRenderExecArgv = async (
})

if (isNodeDebugging) {
const debugPort = await getFreePort()
const isDebugging =
process.execArgv.some((localArg) => localArg.startsWith('--inspect')) ||
process.env.NODE_OPTIONS?.match?.(/--inspect(=\S+)?( |$)/)
Expand All @@ -39,16 +39,18 @@ export const genRenderExecArgv = async (
) || process.env.NODE_OPTIONS?.match?.(/--inspect-brk(=\S+)?( |$)/)

if (isDebugging || isDebuggingWithBrk) {
let debugPort = getDebugPort()

debugPort += type === 'pages' ? 1 : 2

Log.info(
`the --inspect${
isDebuggingWithBrk ? '-brk' : ''
} option was detected, the Next.js server${
type === 'pages' ? ' for pages' : type === 'app' ? ' for app' : ''
} should be inspected at port ${debugPort}.`
)
}

if (isDebugging || isDebuggingWithBrk) {
execArgv.push(`--inspect${isDebuggingWithBrk ? '-brk' : ''}=${debugPort}`)
}
}
Expand Down