fix(exec): don't inherit global config when installing to the npx cache - #10042
Open
Sanjays2402 wants to merge 1 commit into
Open
Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
Fixes: npm#9890 When npx runs inside a global install's lifecycle script, npm_config_global=true leaks into its environment. That flag reached the Arborist instance used for the npx cache, so arborist linked bins to the global-style <npxCache>/bin target while exec() looked for them in <cacheDir>/node_modules/.bin, and the command failed with 'command not found'. The npx cache is never a global install, so force global:false on its Arborist.
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.
Summary
When
npxruns inside a global install's lifecycle script,npm_config_global=trueleaks into its environment. That flag reached the Arborist instance used for the npx cache install, so arborist linked bins to the global-style<npxCache>/bintarget whileexec()looked for them in<cacheDir>/node_modules/.bin. The result wassh: <bin>: command not found(and, on newer trees, a broken cache entry with nopackage.json).The npx cache is never a global install, so this forces
global: falseon the npx cache Arborist, matching the approach the issue's root-cause analysis suggested.Fixes: #9890
Changes
workspaces/libnpmexec/lib/index.js- forceglobal: falseon the npx cache Arborist so bins link under the cache dir'snode_modules/.binworkspaces/libnpmexec/test/registry.js- regression test running exec withglobal: trueagainst the mock registry, asserting the bin runs, no<npxCache>/binis created, and bins land in the cache dir'snode_modules/.binVerification
npm_config_global=true npm exec --yes cowsay hellofailed; bins landed in_npx/bin/, nopackage.jsonwritten to the cache dir). After the fix the same command prints the cowsay output and bins land in<hash>/node_modules/.bin.PackageJson.loadENOENT from the broken cache entry) and passes with the fix.libnpmexecsuite: 9/9 suites, 119 assertions, all passing. ESLint clean on both touched files.