fix(npm): merge NODE_OPTIONS and drop quotes in shadow --node-options#1433
Open
John-David Dalton (jdalton) wants to merge 1 commit into
Open
fix(npm): merge NODE_OPTIONS and drop quotes in shadow --node-options#1433John-David Dalton (jdalton) wants to merge 1 commit into
John-David Dalton (jdalton) wants to merge 1 commit into
Conversation
The shadow npm wrapper built npm's `--node-options` value as:
--node-options='<user value><perm flags>'
Two bugs on that one line:
1. Node >= 24 crash (#1160). spawn() runs npm without a shell, so the
literal single quotes became part of the NODE_OPTIONS value npm set
for lifecycle scripts. Consumers that re-tokenize NODE_OPTIONS on
whitespace (e.g. Next.js) only understand `"`, not `'`, so they
dropped `'--permission` (leading quote) while keeping the `--allow-*`
flags. Node 24 then throws `ERR_MISSING_OPTION: --permission is
required` because the allow-list flags are orphaned. Dropping the
quotes keeps `--permission` a clean, standalone token.
2. NODE_OPTIONS override (#1036). The value only folded in a
`--node-options=` npm arg, never the caller's existing
`process.env.NODE_OPTIONS`, so npm replaced the user's global
NODE_OPTIONS instead of extending it. We now merge, in order,
process.env.NODE_OPTIONS, any `--node-options=` npm arg, and our
permission flags.
Extracted the construction into a pure, exported `buildNpmNodeOptionsArg`
helper and unit-tested both fixes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
socket npmbuilds the--node-optionsvalue it hands to npm for lifecycle scripts. Two bugs lived on that single line insrc/shadow/npm-base.mts:`--node-options='${nodeOptionsArg ? nodeOptionsArg.slice(15) : ''}${cmdFlagsToString(permArgs)}'`Fixes #1160 and #1036.
Bug 1 —
socket npm run buildcrashes on Node >= 24 (#1160)shadowNpmBasespawns npm without a shell, so the literal single quotes are not interpreted — they become part of the value npm assigns toNODE_OPTIONSfor lifecycle scripts:Node tolerates the garbled tokens, but consumers that re-tokenize
NODE_OPTIONSon whitespace and only honour"(e.g. Next.js's TypeScript build worker) drop'--permission(leading quote) while keeping the valid--allow-*flags. Node 24 then throws:because the allow-list flags are orphaned. Removing the quotes keeps
--permissiona clean, standalone token. Quotes were never needed here (no shell).Bug 2 —
socket npmoverrides userNODE_OPTIONS(#1036)The value only merged a
--node-options=npm argument, never the caller's existingprocess.env.NODE_OPTIONS. Since npm's--node-optionsconfig replaces the inheritedNODE_OPTIONSfor scripts, a globally-configuredNODE_OPTIONSwas silently dropped:We now merge, in order:
process.env.NODE_OPTIONS, any--node-options=npm arg, then our permission flags.How
Extracted the construction into a pure, exported
buildNpmNodeOptionsArg(envNodeOptions, nodeOptionsArg, permArgs)helper so both behaviours are unit-testable without spawning, and swapped the call site to use it.Result for
NODE_OPTIONS=--max-old-space-size=4096 socket npm run buildon Node 24Before:
--node-options='--permission --allow-child-process ...'→--permissiondropped downstream → crash, and--max-old-space-sizelost.After:
--node-options=--max-old-space-size=4096 --permission --allow-child-process --allow-fs-read=* ...→ all tokens clean, user value preserved.Tests
New
src/shadow/npm-base.test.mts(7 cases) covers:--permissionstays a standalone token (socket npm run build crashes with ERR_MISSING_OPTION: --permission on Node v24 + Next.js #1160)process.env.NODE_OPTIONSis merged ahead of the permission flags ('socket npm' overrides NODE_OPTIONS #1036)--node-options=npm arg has its prefix stripped and is preservedVerification
pnpm exec vitest run src/shadow/npm-base.test.mts— 7/7 passpnpm run check:tsc— cleanoxlinton changed files — 0 errors (only pre-existingno-named-as-default-memberwarnings on unchanged lines)Notes
Targets
v1.x(the branch where the shadow-npm wrapper lives; the issues reproduce on v1.1.78). Onmain,socket npmhands off to Socket Firewall and no longer builds this argument, somainis unaffected.Note
Medium Risk
Changes NODE_OPTIONS passed into npm lifecycle scripts (build/run), which can affect all downstream script processes, but the logic is isolated, well-tested, and targets known production bugs.
Overview
Fixes how
socket npmbuilds the--node-optionsflag for npm lifecycle scripts by replacing inline string templating withbuildNpmNodeOptionsArg.The value is no longer wrapped in single quotes, so
--permissionstays a real token when tools re-splitNODE_OPTIONSon whitespace (fixes Node ≥ 24ERR_MISSING_OPTIONon builds like Next.js, #1160). The helper also mergesprocess.env.NODE_OPTIONS, any existing npm--node-options=…arg, and Socket’s permission flags in that order, because npm’s flag replaces inheritedNODE_OPTIONSfor scripts (#1036).Adds
npm-base.test.mtswith seven unit tests for quoting, merge order, prefix stripping, and empty inputs.Reviewed by Cursor Bugbot for commit 3f272fb. Configure here.