Repository navigation
fix(vscode): run npm.cmd through the shell on Windows (spawn EINVAL breaks Sharp install and ReadImage) - #399
Merged
Conversation
…l stops failing with EINVAL Node >= 18.20 / 20.12 refuses to spawn .cmd/.bat files without a shell (CVE-2024-27980) and throws EINVAL synchronously, which bypasses child.on("error") and surfaces as a bare 'spawn EINVAL'. ReadImage failed with 'Unable to read image: spawn EINVAL' and image prompts died with 'Request failed: spawn EINVAL' whenever the Sharp cache was cold, e.g. after an extension update bumped sharpVersion. Build the npm invocation once (buildNpmInvocation) and use it for both the lazy install and the 'npm root -g' anchor lookup, which previously threw and was swallowed. Failures raised synchronously by spawn are now reported as 'Failed to start npm: ...' instead of a raw error code. Fixes lessweb#398
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.
Fixes #398.
Problem
On Windows the Sharp loader cannot bootstrap on a cold cache.
installSharp()spawnednpm.cmdwithoutshell: true, and Node >= 18.20 / 20.12 refuses to spawn.cmd/.batin that case (CVE-2024-27980 hardening) — it throwsEINVALsynchronously. The throw happens inside thenew Promiseexecutor, sochild.on("error")never fires and the caller receives a barespawn EINVAL:ReadImage→Unable to read image: spawn EINVALRequest failed: spawn EINVALThis is reached whenever the Sharp cache for the required version is missing, i.e. right after an extension update bumps
sharpVersion.findCliAnchors()had the same bug inexecFileSync("npm.cmd", ["root", "-g"]), where it was swallowed by the surroundingcatch {}, so the global-npm anchor never resolved on Windows.Fix
buildNpmInvocation(args, platform): on Windows the invocation becomesnpm.cmd "<arg>" "<arg>" ...withshell: true. Arguments are folded into the command string because shell mode concatenates them unescaped (NodeDEP0190), and quoting keeps paths with spaces — such as the VS Code storage root — intact. Everywhere else the behaviour is unchanged (npm+ argv,shell: false).installSharp()and thenpm root -glookup infindCliAnchors().spawnNpm()converts synchronous spawn failures intoFailed to start npm: ..., so a future sync throw is reported the same way as the asyncerrorevent instead of leaking a raw error code.Tests
packages/vscode-ide-companion: 88/88 pass (node src/tests/run-tests.mjs), including 3 new tests covering the Windows invocation (shell flag, quoting, and a prefix path containing a space) and the unchanged posix argv form.Also clean:
tsc -p ./ --noEmit,eslint,prettier --checkon the changed files.Manual verification (Windows 11, VS Code 1.144.1, cold cache)
Driving
createSharpLoader()against a fresh storage root, so the realinstallSharp()path runs:spawn EINVALsharp 0.35.5installed and loaded in 3.1 s, progress 10% → 100%npm root -ganchor now resolves toC:\Users\<user>\AppData\Roaming\npm\node_modulesinstead of throwing