From 0a8ee243f0dfbfe9aeadb3b69a119635551e05b8 Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 2 Aug 2022 09:12:52 -0700 Subject: [PATCH] fix: use binPaths This will allow for a locally installed package to differ from the one requested on the command line, the one requested will now actually run. --- workspaces/libnpmexec/lib/index.js | 12 ++++++------ workspaces/libnpmexec/lib/run-script.js | 8 ++------ workspaces/libnpmexec/test/run-script.js | 1 - 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/workspaces/libnpmexec/lib/index.js b/workspaces/libnpmexec/lib/index.js index 227dc2cdda1ac..0c66a2836aa15 100644 --- a/workspaces/libnpmexec/lib/index.js +++ b/workspaces/libnpmexec/lib/index.js @@ -19,9 +19,9 @@ const noTTY = require('./no-tty.js') const runScript = require('./run-script.js') const isWindows = require('./is-windows.js') -const { delimiter, dirname, resolve } = require('path') +const { dirname, resolve } = require('path') -const pathArr = process.env.PATH.split(delimiter) +const binPaths = [] // when checking the local tree we look up manifests, cache those results by // spec.raw so we don't have to fetch again when we check npxCache @@ -96,7 +96,7 @@ const exec = async (opts) => { locationMsg, output, path, - pathArr, + binPaths, runPath, scriptShell, }) @@ -113,10 +113,10 @@ const exec = async (opts) => { const dir = dirname(dirname(localBin)) const localBinPath = await localFileExists(dir, args[0], '/') if (localBinPath) { - // @npmcli/run-script adds local bin to $PATH itself + binPaths.push(localBinPath) return await run() } else if (await fileExists(`${globalBin}/${args[0]}`)) { - pathArr.unshift(globalBin) + binPaths.push(globalBin) return await run() } @@ -213,7 +213,7 @@ const exec = async (opts) => { add, }) } - pathArr.unshift(resolve(installDir, 'node_modules/.bin')) + binPaths.push(resolve(installDir, 'node_modules/.bin')) } return await run() diff --git a/workspaces/libnpmexec/lib/run-script.js b/workspaces/libnpmexec/lib/run-script.js index 97543e6ff0d08..18dcf7d8356c5 100644 --- a/workspaces/libnpmexec/lib/run-script.js +++ b/workspaces/libnpmexec/lib/run-script.js @@ -1,5 +1,3 @@ -const { delimiter } = require('path') - const chalk = require('chalk') const ciDetect = require('@npmcli/ci-detect') const runScript = require('@npmcli/run-script') @@ -22,7 +20,7 @@ const run = async ({ locationMsg, output = () => {}, path, - pathArr, + binPaths, runPath, scriptShell, }) => { @@ -71,11 +69,9 @@ const run = async ({ // we always run in cwd, not --prefix path: runPath, stdioString: true, + binPaths, event: 'npx', args, - env: { - PATH: pathArr.join(delimiter), - }, stdio: 'inherit', }) } finally { diff --git a/workspaces/libnpmexec/test/run-script.js b/workspaces/libnpmexec/test/run-script.js index 0b97a72ac04cb..9e0db1367fed1 100644 --- a/workspaces/libnpmexec/test/run-script.js +++ b/workspaces/libnpmexec/test/run-script.js @@ -5,7 +5,6 @@ const baseOpts = { call: '', color: false, path: '', - pathArr: [''], runPath: '', shell: process.platform === 'win32' ? process.env.ComSpec || 'cmd'