Skip to content

Commit

Permalink
misc: fix injecting --inspect with NODE_OPTIONS (#57159)
Browse files Browse the repository at this point in the history
This PR fixes the passing of the `--inspect` option when calling Next.js with it. It's still not great because you need to target the next file in node_modules directly but I'll add a `next --inspect` option in the future.
  • Loading branch information
feedthejim committed Oct 21, 2023
1 parent 1ffef0f commit 4e27b90
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 29 deletions.
19 changes: 18 additions & 1 deletion packages/next/src/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

import '../server/lib/cpu-profile'
import type { StartServerOptions } from '../server/lib/start-server'
import { RESTART_EXIT_CODE, getPort, printAndExit } from '../server/lib/utils'
import {
RESTART_EXIT_CODE,
checkNodeDebugType,
getDebugPort,
getNodeOptionsWithoutInspect,
getPort,
printAndExit,
} from '../server/lib/utils'
import * as Log from '../build/output/log'
import type { CliCommand } from '../lib/commands'
import { getProjectDir } from '../lib/get-project-dir'
Expand Down Expand Up @@ -223,6 +230,15 @@ const nextDev: CliCommand = async (args) => {
let resolved = false
const defaultEnv = (initialEnv || process.env) as typeof process.env

let NODE_OPTIONS = getNodeOptionsWithoutInspect()
let nodeDebugType = checkNodeDebugType()

if (nodeDebugType) {
NODE_OPTIONS = `${NODE_OPTIONS} --${nodeDebugType}=${
getDebugPort() + 1
}`
}

child = fork(startServerPath, {
stdio: 'inherit',
env: {
Expand All @@ -232,6 +248,7 @@ const nextDev: CliCommand = async (args) => {
NODE_EXTRA_CA_CERTS: options.selfSignedCertificate
? options.selfSignedCertificate.rootCA
: defaultEnv.NODE_EXTRA_CA_CERTS,
NODE_OPTIONS,
},
})

Expand Down
14 changes: 0 additions & 14 deletions packages/next/src/server/lib/is-node-debugging.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ export async function initialize(opts: {
experimentalTestProxy?: boolean
experimentalHttpsServer?: boolean
}): Promise<[WorkerRequestHandler, WorkerUpgradeHandler]> {
process.title = 'next-router-worker'

if (!process.env.NODE_ENV) {
// @ts-ignore not readonly
process.env.NODE_ENV = opts.dev ? 'development' : 'production'
Expand Down
14 changes: 6 additions & 8 deletions packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import https from 'https'
import Watchpack from 'watchpack'
import * as Log from '../../build/output/log'
import setupDebug from 'next/dist/compiled/debug'
import { RESTART_EXIT_CODE, getDebugPort } from './utils'
import { RESTART_EXIT_CODE, checkNodeDebugType, getDebugPort } from './utils'
import { formatHostname } from './format-hostname'
import { initialize } from './router-server'
import { checkIsNodeDebugging } from './is-node-debugging'
import { CONFIG_FILES } from '../../shared/lib/constants'
import { getStartServerInfo, logStartInfo } from './app-info-log'

Expand Down Expand Up @@ -86,6 +85,7 @@ export async function startServer({
isExperimentalTestProxy,
selfSignedCertificate,
}: StartServerOptions): Promise<void> {
process.title = 'next-server'
let handlersReady = () => {}
let handlersError = () => {}

Expand Down Expand Up @@ -183,7 +183,7 @@ export async function startServer({
}
})

const isNodeDebugging = checkIsNodeDebugging()
const nodeDebugType = checkNodeDebugType()

await new Promise<void>((resolve) => {
server.on('listening', async () => {
Expand All @@ -207,12 +207,10 @@ export async function startServer({
selfSignedCertificate ? 'https' : 'http'
}://${formattedHostname}:${port}`

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

Expand Down Expand Up @@ -243,7 +241,7 @@ export async function startServer({
server,
hostname,
minimalMode,
isNodeDebugging: Boolean(isNodeDebugging),
isNodeDebugging: Boolean(nodeDebugType),
keepAliveTimeout,
experimentalTestProxy: !!isExperimentalTestProxy,
experimentalHttpsServer: !!selfSignedCertificate,
Expand Down
20 changes: 20 additions & 0 deletions packages/next/src/server/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,23 @@ export function getPort(args: arg.Result<arg.Spec>): number {
}

export const RESTART_EXIT_CODE = 77

export function checkNodeDebugType() {
let nodeDebugType = undefined

if (
process.execArgv.some((localArg) => localArg.startsWith('--inspect')) ||
process.env.NODE_OPTIONS?.match?.(/--inspect(=\S+)?( |$)/)
) {
nodeDebugType = 'inspect'
}

if (
process.execArgv.some((localArg) => localArg.startsWith('--inspect-brk')) ||
process.env.NODE_OPTIONS?.match?.(/--inspect-brk(=\S+)?( |$)/)
) {
nodeDebugType = 'inspect-brk'
}

return nodeDebugType
}
4 changes: 2 additions & 2 deletions packages/next/src/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { PHASE_PRODUCTION_SERVER } from '../shared/lib/constants'
import { getTracer } from './lib/trace/tracer'
import { NextServerSpan } from './lib/trace/constants'
import { formatUrl } from '../shared/lib/router/utils/format-url'
import { checkIsNodeDebugging } from './lib/is-node-debugging'
import { checkNodeDebugType } from './lib/utils'

let ServerImpl: typeof Server

Expand Down Expand Up @@ -275,7 +275,7 @@ class NextCustomServer extends NextServer {
const { getRequestHandlers } =
require('./lib/start-server') as typeof import('./lib/start-server')

const isNodeDebugging = checkIsNodeDebugging()
const isNodeDebugging = !!checkNodeDebugType()

const initResult = await getRequestHandlers({
dir: this.options.dir!,
Expand Down
4 changes: 2 additions & 2 deletions test/development/basic/node-builtins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ createNextDescribe(
expect(parsedData.https).toBe(true)
expect(parsedData.os).toBe('\n')
expect(parsedData.path).toBe('/hello/world/test.txt')
expect(parsedData.process).toInclude('next-router-worker')
expect(parsedData.process).toInclude('next-server')
expect(parsedData.querystring).toBe('a=b')
expect(parsedData.stringDecoder).toBe(true)
expect(parsedData.sys).toBe(true)
Expand All @@ -112,7 +112,7 @@ createNextDescribe(
expect(parsedData.https).toBe(true)
expect(parsedData.os).toBe('\n')
expect(parsedData.path).toBe('/hello/world/test.txt')
expect(parsedData.process).toInclude('next-router-worker')
expect(parsedData.process).toInclude('next-server')
expect(parsedData.querystring).toBe('a=b')
expect(parsedData.stringDecoder).toBe(true)
expect(parsedData.sys).toBe(true)
Expand Down

0 comments on commit 4e27b90

Please sign in to comment.