Skip to content

Commit

Permalink
Fix conflict between debug and http port
Browse files Browse the repository at this point in the history
  • Loading branch information
Cow258 committed Jun 18, 2023
1 parent 67853aa commit 5758ced
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
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 server for proxy 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 server for router 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

0 comments on commit 5758ced

Please sign in to comment.