Skip to content

fix: match Node's argument handling in patched child_process - #335

Merged
robertsLando merged 2 commits into
mainfrom
fix/child-process-undefined-options
Nov 25, 2026
Merged

robertsLando merged 2 commits into
mainfrom
fix/child-process-undefined-options

Conversation

@robertsLando

@robertsLando robertsLando commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Closes #334

Problem

The child_process patch in prelude/bootstrap-shared.js (setOptsEnv) guessed where the options object sat. When that slot held an explicit undefined, it inserted a new {} after it instead of replacing it:

exec('echo hi', undefined, cb)
// pkg passed: ['echo hi', undefined, {}, cb]  -> {} becomes the callback
// ERR_INVALID_ARG_TYPE: The "callback" argument must be of type function

This is not a regression. The logic dates back to vercel/pkg (2017); #229 only moved it into bootstrap-shared.js. It surfaced now because systeminformation 5.33.23 calls exec(cmd, undefined, cb).

Other differences from plain Node in the same code:

Call Node pkg before
exec(cmd, null, cb), execSync(cmd, null) OK TypeError on null.env
execFile(f, undefined, cb) OK args shifted, throws
spawn(f, args, null) ERR_INVALID_ARG_TYPE TypeError on null.env
any call with options/args caller's objects untouched opts.env, env and the args array mutated

Fix

Each family now has a normalizer that mirrors Node's own (normalizeExecArgs, normalizeExecFileArgs, normalizeSpawnArguments in lib/child_process.js; these are the same in v22 and v24). Each one produces the canonical argument list, and PKG_EXECPATH is injected into a copy of the options. Some details follow Node exactly:

  • exec/execSync accept any options value (Node spreads it), so execSync(cmd, 'str') still gets PKG_EXECPATH.
  • Options are copied before env is read, so an inherited options.env is ignored, as in Node. Env keys are copied with for-in, so inherited env keys are kept, as in Node.

For spawn/execFile input that Node rejects, the normalizer returns null and the call passes through untouched, so Node throws its own error. Because the args array is always canonical now, modifyShort no longer inserts [].

Standard and SEA modes share this code.

Tests

  • test/unit/child-process.test.ts: 36 call forms run on stock Node first, then patched, and must give the same outcome (error code and message, or stdout). This includes the reject paths and inherited env/options. Further checks: PKG_EXECPATH reaches the child for all six functions, a caller-set value is kept, and the caller's options, env and args are not mutated. 23 of the 45 tests fail on main.
  • test/test-50-spawn/test-exec-4.js: the reproduction from the issue. Node and the packaged binary must print the same output. Checked by hand in both standard and SEA mode.

The child_process patch guessed where the options object sat. With an
explicit undefined/null options argument it inserted a new {} after it,
so exec(cmd, undefined, cb) passed {} as the callback and threw
ERR_INVALID_ARG_TYPE. null options crashed on null.env, and the
caller's options, env and args array were mutated.

Normalize each call the way Node does (normalizeExecArgs,
normalizeExecFileArgs, normalizeSpawnArguments), inject PKG_EXECPATH
into a copy of the options, and pass input Node rejects through
untouched so Node throws its own error.

Closes #334
@robertsLando

Copy link
Copy Markdown
Member Author

finalize-pr — 1 round, merge-ready

Declined

  • [Nit] prelude/bootstrap-shared.js:for-in-inherited-env-keys: kept for-in over Object.assign. Node copies env with for-in on purpose ("Prototype values are intentionally included"), and this PR aims for Node parity.
  • [Nit] test/unit/child-process.test.ts:sync-throw-cases-wait-for-timeout: not a real issue. runAsync catches sync throws at once, so those cases don't wait for the timeout.

Noted, not fixed (dropped by the author)

  • [Minor] normalizeSpawnArgs / normalizeExecFileArgs share their args/options handling. Kept separate, as Node keeps them.
  • [Nit] typeof file !== 'string' guards in the spawn/execFile normalizers repeat Node's own check. Harmless.
  • [Nit] test-exec-4.js also covers execSync. It is the one repro file for the issue.

@robertsLando
robertsLando merged commit feda456 into main Nov 25, 2026
28 checks passed
@robertsLando
robertsLando deleted the fix/child-process-undefined-options branch November 25, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant