Skip to content

Commit 18b930f

Browse files
authored
fix(exec): prevent shared binPaths pollution across workspace runs (#9650)
**Description:** related: #9640 **Bug:** When running `npm exec` across multiple workspaces, the binary from the first workspace would accidentally bleed into the second one if they shared the same name. **Root Cause:** In `libnpmexec`, the `binPaths` array was scoped at the module level instead of inside the `exec()` function. Since `execWorkspaces` runs everything in a single process, `binPaths` just kept accumulating paths. When the second workspace ran, the first workspace's `.bin` path was still sitting at the front of `PATH`, so it hijacked the execution. **Fix:** Moved `binPaths` inside `exec()` so it gets a fresh array on every call. I left the `manifests` map at the module level (keyed by `spec.raw`) because we still want to cache registry specs across calls, and absolute paths won't collide anyway. **Testing:** - Added a regression test in `test/local.js` with two workspaces using the exact same local bin name (`shared-bin`). - Gave each test call its own `args` array to stop `@npmcli/run-script` from mutating it and hanging the tests. - Fails correctly without the fix. With the fix, all tests pass 100%.
1 parent 6da6555 commit 18b930f

2 files changed

Lines changed: 70 additions & 3 deletions

File tree

‎workspaces/libnpmexec/lib/index.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ const runScript = require('./run-script.js')
1919
const isWindows = require('./is-windows.js')
2020
const withLock = require('./with-lock.js')
2121

22-
const binPaths = []
23-
2422
// when checking the local tree we look up manifests, cache those results by
2523
// spec.raw so we don't have to fetch again when we check npxCache
2624
const manifests = new Map()
@@ -113,6 +111,8 @@ const exec = async (opts) => {
113111
...flatOptions
114112
} = opts
115113

114+
const binPaths = []
115+
116116
let pkgPaths = opts.pkgPath
117117
if (typeof pkgPaths === 'string') {
118118
pkgPaths = [pkgPaths]
@@ -196,7 +196,11 @@ const exec = async (opts) => {
196196
let commandManifest
197197
await Promise.all(packages.map(async (pkg, i) => {
198198
const spec = npa(pkg, path)
199-
const { manifest, node } = await missingFromTree({ spec, tree: localTree, flatOptions })
199+
const { manifest, node } = await missingFromTree({
200+
spec,
201+
tree: localTree,
202+
flatOptions,
203+
})
200204
if (manifest) {
201205
// Package does not exist in the local tree
202206
needInstall.push({ spec, manifest })

‎workspaces/libnpmexec/test/local.js‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,66 @@ for (const allowDirectory of ['none', 'root']) {
469469
})
470470
})
471471
}
472+
473+
t.test('npm exec sequential workspace runs with same-named local bins', async t => {
474+
t.plan(2)
475+
const { path, readOutput, rmOutput, registry } = setup(t, {
476+
testdir: {
477+
packages: {
478+
a: {
479+
'package.json': {
480+
name: 'workspace-a',
481+
version: '1.0.0',
482+
bin: { 'shared-bin': 'cli.js' },
483+
},
484+
'cli.js': { key: 'shared-bin', value: 'A' },
485+
},
486+
b: {
487+
'package.json': {
488+
name: 'workspace-b',
489+
version: '1.0.0',
490+
bin: { 'shared-bin': 'cli.js' },
491+
},
492+
'cli.js': { key: 'shared-bin', value: 'B' },
493+
},
494+
},
495+
},
496+
})
497+
498+
// We mock the module exactly once. This simulates two sequential calls
499+
// hitting the same module instance, allowing us to prove that state
500+
// (like binPaths) is correctly cleared between runs and NOT preserved.
501+
const libnpmexec = t.mock('../lib/index.js')
502+
503+
const baseOpts = {
504+
path,
505+
runPath: path,
506+
npxCache: resolve(path, 'npxCache'),
507+
registry: registry.origin + '/',
508+
localBin: resolve(path, 'node_modules', '.bin'),
509+
call: '',
510+
scriptShell: 'sh',
511+
yes: true, // skip interactive prompt for npxCache install
512+
}
513+
514+
// Workspace A
515+
await libnpmexec({
516+
...baseOpts,
517+
pkgPath: resolve(path, 'packages/a'),
518+
args: ['shared-bin'],
519+
})
520+
521+
const outputA = await readOutput('shared-bin')
522+
t.equal(outputA.value, 'A', 'should run workspace A bin')
523+
await rmOutput('shared-bin')
524+
525+
// Workspace B
526+
await libnpmexec({
527+
...baseOpts,
528+
pkgPath: resolve(path, 'packages/b'),
529+
args: ['shared-bin'],
530+
})
531+
532+
const outputB = await readOutput('shared-bin')
533+
t.equal(outputB.value, 'B', 'should run workspace B bin, not cached workspace A bin')
534+
})

0 commit comments

Comments
 (0)