GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(exec): prevent shared binPaths pollution across workspace runs by arjun-vegeta · Pull Request #9650 · npm/cli

Conversation

@arjun-vegeta

@arjun-vegeta arjun-vegeta commented Jun 24, 2026 •
edited by owlstronaut
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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%.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@arjun-vegeta
fix(exec): prevent shared binPaths pollution across workspace runs
a6f9044
@arjun-vegeta
arjun-vegeta requested review from a team as code owners June 24, 2026 20:07
owlstronaut
owlstronaut suggested changes Jun 24, 2026
View reviewed changes

@owlstronaut owlstronaut left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for digging into this, but I ran your exact repro on the branch and it still prints  A bin / A bin  under workspace hoisting only one  shared-bin  link gets created (→tool-a), so the  binPaths  change doesn't actually fix #9640

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
arjun-vegeta added 2 commits June 25, 2026 16:05
@arjun-vegeta
fix(libnpmexec): correctly resolve binaries from workspace dependencies
94a0d84
Fixes npm#9640 by preventing fallback to hoisted .bin directories for workspace packages.
@arjun-vegeta
test(libnpmexec): fix branch coverage drop on Windows runners
d1ed92d
@arjun-vegeta

arjun-vegeta commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for digging into this, but I ran your exact repro on the branch and it still prints  A bin / A bin  under workspace hoisting only one  shared-bin  link gets created (→tool-a), so the  binPaths  change doesn't actually fix #9640

Thanks for testing it out. The old binPaths approach was a dead end because of how the hoisted .bin symlink gets overwritten at the root

I just pushed a complete rewrite. I dropped the binPaths idea entirely and instead hooked into the Arborist tree. Now, if it detects it's running inside a workspace, it walks the actual dependency graph (edgesOut) for that specific workspace node to find the correct binary path directly (e.g., node_modules/tool-b/cli.js), completely bypassing the hoisted .bin folder

The repro now outputs A bin / B bin correctly, and I've added full integration tests for the hoisted edge-case. Can you test it out once more and let me know how it goes

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@arjun-vegeta arjun-vegeta changed the title fix(exec): prevent shared binPaths pollution across workspace runs fix(libnpmexec): correctly resolve workspace dependency binaries from graph Jun 25, 2026
@arjun-vegeta
arjun-vegeta requested a review from owlstronaut June 25, 2026 10:53
@manzoorwanijk

manzoorwanijk commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

@owlstronaut I think the issue here is the bin-hoisting collision in hoisted mode. For that, we need only the changes from the earlier version of the PR, a6f9044.

Then we can create a separate issue for bin collision.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@manzoorwanijk

manzoorwanijk commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Thanks for iterating on this. A few concerns with the graph + node <binFile> approach:

1. It assumes every bin is a Node script. Running the resolved bin as node <binFile> breaks non-node bins — shell scripts, native binaries, or a non-node shebang. The .bin shim path (symlink / cmd-shim) honors the shebang; node <binFile> does not.

2. It bypasses the hoisted .bin for all workspace execs, not just collisions. Any npm exec -w <ws> -- <dep-bin> now runs node <binFile> directly even when the root shim is already correct, a broad behavior change that drops shim/shebang handling for cases that were fine.

3. Same-named bins across multiple direct deps resolve non-deterministically. The first matching edgesOut entry wins, i.e. by package.json order, not by the package that owns the installed shim.

4. The tests mock loadActual() by hand, so they don't verify a real installed tree resolves as assumed.

On scope: the binPaths reset is a valid, separate fix (it makes the linked strategy correct across sequential runs) but doesn't address the hoisted collision on its own. The collision is that npm links only one shared-bin into the shared root .bin (first-wins in bin-links), so no correct per-workspace shim exists. Fixing that cleanly - cross-platform, all bin types - wants a fix at the install layer (arborist creating a per-workspace .bin link on a hoisted name collision), not a special case in libnpmexec.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@arjun-vegeta

arjun-vegeta commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I'll revert the graph walk and scope this PR back to just the binPaths reset for #9640
I'll dig more into the hoisted bin collision on the arborist/bin-links side and open a separate PR for that

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

arjun-vegeta added 2 commits June 27, 2026 14:07
@arjun-vegeta
Revert "test(libnpmexec): fix branch coverage drop on Windows runners"
0b15fd0
This reverts commit d1ed92d.
@arjun-vegeta
Revert "fix(libnpmexec): correctly resolve binaries from workspace de…
6cb84ad
…pendencies"

This reverts commit 94a0d84.
@arjun-vegeta arjun-vegeta changed the title fix(libnpmexec): correctly resolve workspace dependency binaries from graph fix(exec): prevent shared binPaths pollution across workspace runs Jun 27, 2026
owlstronaut
owlstronaut approved these changes Jun 29, 2026
View reviewed changes
@owlstronaut owlstronaut added the backport:v11 label Jun 29, 2026
@owlstronaut
owlstronaut merged commit 18b930f into npm:latest Jun 29, 2026
26 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot mentioned this pull request Jun 29, 2026
fix(exec): prevent shared binPaths pollution across workspace runs #9692
Merged
@github-actions

github-actions Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

🎉 Backport to release/v11 created: #9692

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot mentioned this pull request Jun 29, 2026
chore: release 12.0.0-pre.2 #9594
Merged
owlstronaut pushed a commit that referenced this pull request Jun 29, 2026
@github-actions @arjun-vegeta
fix(exec): prevent shared binPaths pollution across workspace runs (#…
f3f2465
…9692)

Backport of #9650 to `release/v11`.

Co-authored-by: Arjun R <[email protected]>
@manzoorwanijk manzoorwanijk mentioned this pull request Jul 1, 2026
[BUG] npm exec -w a -w b -- <bin> runs the first workspace's bin for every workspace #9640
Closed
3 tasks done
@coderabbitai coderabbitai Bot mentioned this pull request Aug 26, 2026
Publish by OIDC trusted publishing instead of the expired npm token unbraind/pm-jira#78
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@owlstronaut owlstronaut owlstronaut approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

backport:v11

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@arjun-vegeta @manzoorwanijk @owlstronaut
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.