Skip to content

Commit

Permalink
Remove passing of NODE_OPTIONS='--inspect' to subprocesses (#11041)
Browse files Browse the repository at this point in the history
* fix(debugging): do not pass NODE_OPTIONS='--inspect' to subprocesses

fixes #11030

* fix(debugger): use a regex to remove bad NODE_OPTIONS flags

Co-authored-by: Alec Larson <[email protected]>
  • Loading branch information
vvo and aleclarson authored Apr 1, 2020
1 parent f9cd7c5 commit fa5da6d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/next/server/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ export function printAndExit(message: string, code = 1) {

process.exit(code)
}

export function getNodeOptionsWithoutInspect() {
const NODE_INSPECT_RE = /--inspect(-brk)?(=\S+)? ?/
return (process.env.NODE_OPTIONS || '').replace(NODE_INSPECT_RE, '')
}
11 changes: 11 additions & 0 deletions packages/next/server/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { Telemetry } from '../telemetry/storage'
import ErrorDebug from './error-debug'
import HotReloader from './hot-reloader'
import { findPageFile } from './lib/find-page-file'
import { getNodeOptionsWithoutInspect } from './lib/utils'

if (typeof React.Suspense === 'undefined') {
throw new Error(
Expand Down Expand Up @@ -87,6 +88,16 @@ export default class DevServer extends Server {
{
maxRetries: 0,
numWorkers: this.nextConfig.experimental.cpus,
forkOptions: {
env: {
...process.env,
// discard --inspect/--inspect-brk flags from process.env.NODE_OPTIONS. Otherwise multiple Node.js debuggers
// would be started if user launch Next.js in debugging mode. The number of debuggers is linked to
// the number of workers Next.js tries to launch. The only worker users are interested in debugging
// is the main Next.js one
NODE_OPTIONS: getNodeOptionsWithoutInspect(),
},
},
}
) as Worker & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
Expand Down
10 changes: 10 additions & 0 deletions test/integration/cli/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ describe('CLI Usage', () => {
expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})

test("NODE_OPTIONS='--inspect'", async () => {
// this test checks that --inspect works by launching a single debugger for the main Next.js process,
// not for its subprocesses
const port = await findPort()
const output = await runNextCommandDev([dir, '--port', port], true, {
env: { NODE_OPTIONS: '--inspect' },
})
expect(output).toMatch(new RegExp(`ready on http://localhost:${port}`))
})

test('-p', async () => {
const port = await findPort()
const output = await runNextCommandDev([dir, '-p', port], true)
Expand Down

0 comments on commit fa5da6d

Please sign in to comment.