Unverified Commit aad56b54 authored by Anuj Bolewar's avatar Anuj Bolewar Committed by GitHub

fix: preserve repeated --require/--import flags when forking workers (#96651)

Repeated --require/--import flags in NODE_OPTIONS are corrupted when
Next.js reparses and reformats the options for forked workers.

## What changed

- Declare `require` and `import` as repeatable options in
`parseNodeArgs` so `parseArgs` accumulates every occurrence into an
array instead of keeping only the last value (equals form) or joining
the space form into one bogus specifier.
- Emit one flag per array element in `formatArg`/round-trip.
- Widened `NodeOptions` and `getParsedDebugAddress` to accept
`string[]`.

## Why

Two preloads in `NODE_OPTIONS` (e.g. an APM agent plus an env loader)
silently lose one in forked workers (`--require=a --require=b`), or
crash the build (`--require a --require b`, joining paths into a single
MODULE_NOT_FOUND). Node itself runs every occurrence; the round-trip
through Next must preserve them.

Fixes #96582

## Validation

- Unit tests in `packages/next/src/server/lib/utils.test.ts` cover both
spellings, repeated `--import`, quoted values, and array formatting (22
passing).
- `pnpm --filter=next types` clean.
Signed-off-by: 's avatarAnuj Bolewar <bolewara@gmail.com>
parent e44c5f19
...@@ -77,6 +77,19 @@ describe('formatNodeOptions', () => { ...@@ -77,6 +77,19 @@ describe('formatNodeOptions', () => {
], ],
}) })
}) })
it('emits one flag per element for array values', () => {
const result = formatNodeOptions({
require: ['/path/a.cjs', '/path/b.cjs'],
import: '/path/c.mjs',
})
expect(result).toEqual({
execArgv: [],
nodeOptions:
'--require=/path/a.cjs --require=/path/b.cjs --import=/path/c.mjs',
})
})
}) })
describe('getMemoryRestartStats', () => { describe('getMemoryRestartStats', () => {
...@@ -192,4 +205,37 @@ describe('getFormattedNodeOptionsWithoutInspect', () => { ...@@ -192,4 +205,37 @@ describe('getFormattedNodeOptionsWithoutInspect', () => {
expect(result).toBe('--other --inspect-port=0.0.0.0:1234 --additional') expect(result).toBe('--other --inspect-port=0.0.0.0:1234 --additional')
}) })
it('preserves repeated --require options', () => {
process.env.NODE_OPTIONS =
'--require=/path/a.cjs --require=/path/b.cjs --other'
const result = getFormattedNodeOptionsWithoutInspect()
expect(result).toBe('--require=/path/a.cjs --require=/path/b.cjs --other')
})
it('preserves repeated --require options with space-separated values', () => {
process.env.NODE_OPTIONS = '--require /path/a.cjs --require /path/b.cjs'
const result = getFormattedNodeOptionsWithoutInspect()
expect(result).toBe('--require=/path/a.cjs --require=/path/b.cjs')
})
it('preserves repeated --import options', () => {
process.env.NODE_OPTIONS =
'--import=/path/a.mjs --import=/path/b.mjs --other'
const result = getFormattedNodeOptionsWithoutInspect()
expect(result).toBe('--import=/path/a.mjs --import=/path/b.mjs --other')
})
it('preserves repeated --require with quoted values', () => {
process.env.NODE_OPTIONS =
'--require "./a with spaces.js" --require "./b with spaces.js"'
const result = getFormattedNodeOptionsWithoutInspect()
expect(result).toBe(
'--require="./a with spaces.js" --require="./b with spaces.js"'
)
})
}) })
...@@ -11,10 +11,32 @@ export function printAndExit(message: string, code = 1) { ...@@ -11,10 +11,32 @@ export function printAndExit(message: string, code = 1) {
return process.exit(code) return process.exit(code)
} }
export type NodeOptions = Record<string, string | boolean | undefined> export type NodeOptions = Record<
string,
string | string[] | boolean | undefined
>
/**
* Node.js CLI flags that can be repeated. Node runs every occurrence of these
* flags, so they need to be preserved individually rather than collapsed into
* a single value keyed by option name.
* https://nodejs.org/api/cli.html#--requiremodule
* https://nodejs.org/api/cli.html#--importmodule
*/
const REPEATABLE_OPTIONS: Record<string, { type: 'string'; multiple: true }> = {
require: { type: 'string', multiple: true },
import: { type: 'string', multiple: true },
}
const parseNodeArgs = (args: string[]): NodeOptions => { const parseNodeArgs = (args: string[]): NodeOptions => {
const { values, tokens } = parseArgs({ args, strict: false, tokens: true }) const parsed = parseArgs({
args,
strict: false,
tokens: true,
options: REPEATABLE_OPTIONS,
})
const values: NodeOptions = parsed.values
const tokens = parsed.tokens
// For the `NODE_OPTIONS`, we support arguments with values without the `=` // For the `NODE_OPTIONS`, we support arguments with values without the `=`
// sign. We need to parse them manually. // sign. We need to parse them manually.
...@@ -46,12 +68,27 @@ const parseNodeArgs = (args: string[]): NodeOptions => { ...@@ -46,12 +68,27 @@ const parseNodeArgs = (args: string[]): NodeOptions => {
continue continue
} }
const name = orphan.name
// If the option is repeatable, accumulate each occurrence instead of
// collapsing them into a single value.
if (name in REPEATABLE_OPTIONS) {
if (Array.isArray(values[name])) {
values[name].push(token.value)
} else if (typeof values[name] === 'string') {
values[name] = [values[name], token.value]
} else {
values[name] = token.value
}
continue
}
// If the token is a positional one, and it has a value, so add it to the // If the token is a positional one, and it has a value, so add it to the
// values object. If it already exists, append it with a space. // values object. If it already exists, append it with a space.
if (orphan.name in values && typeof values[orphan.name] === 'string') { if (name in values && typeof values[name] === 'string') {
values[orphan.name] += ` ${token.value}` values[name] += ` ${token.value}`
} else { } else {
values[orphan.name] = token.value values[name] = token.value
} }
} }
...@@ -150,7 +187,7 @@ export const formatDebugAddress = ({ host, port }: DebugAddress): string => { ...@@ -150,7 +187,7 @@ export const formatDebugAddress = ({ host, port }: DebugAddress): string => {
* @returns An object with the host and port of the debug address. * @returns An object with the host and port of the debug address.
*/ */
export const getParsedDebugAddress = ( export const getParsedDebugAddress = (
address: string | boolean | undefined address: string | string[] | boolean | undefined
): DebugAddress => { ): DebugAddress => {
if (!address || typeof address !== 'string') { if (!address || typeof address !== 'string') {
return { host: undefined, port: 9229 } return { host: undefined, port: 9229 }
...@@ -183,12 +220,23 @@ const EXEC_ARGV_ONLY_OPTIONS = new Set([ ...@@ -183,12 +220,23 @@ const EXEC_ARGV_ONLY_OPTIONS = new Set([
function formatArg( function formatArg(
key: string, key: string,
value: string | boolean | undefined value: string | string[] | boolean | undefined
): string | null { ): string | null {
if (value === true) { if (value === true) {
return `--${key}` return `--${key}`
} }
if (Array.isArray(value)) {
const formatted: string[] = []
for (const item of value) {
const formattedItem = formatArg(key, item)
if (formattedItem !== null) {
formatted.push(formattedItem)
}
}
return formatted.length > 0 ? formatted.join(' ') : null
}
if (value) { if (value) {
return `--${key}=${ return `--${key}=${
// Values with spaces need to be quoted. We use JSON.stringify to // Values with spaces need to be quoted. We use JSON.stringify to
...@@ -210,9 +258,10 @@ function formatArg( ...@@ -210,9 +258,10 @@ function formatArg(
* @param args The arguments to be stringified. * @param args The arguments to be stringified.
* @returns An object with `nodeOptions` string and `execArgv` array. * @returns An object with `nodeOptions` string and `execArgv` array.
*/ */
export function formatNodeOptions( export function formatNodeOptions(args: NodeOptions): {
args: Record<string, string | boolean | undefined> nodeOptions: string
): { nodeOptions: string; execArgv: string[] } { execArgv: string[]
} {
const nodeOptionsParts: string[] = [] const nodeOptionsParts: string[] = []
const execArgv: string[] = [] const execArgv: string[] = []
...@@ -230,10 +279,7 @@ export function formatNodeOptions( ...@@ -230,10 +279,7 @@ export function formatNodeOptions(
return { nodeOptions: nodeOptionsParts.join(' '), execArgv } return { nodeOptions: nodeOptionsParts.join(' '), execArgv }
} }
export function getParsedNodeOptions(): Record< export function getParsedNodeOptions(): NodeOptions {
string,
string | boolean | undefined
> {
const args = [...process.execArgv, ...getNodeOptionsArgs()] const args = [...process.execArgv, ...getNodeOptionsArgs()]
if (args.length === 0) return {} if (args.length === 0) return {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment