Skip to content

Commit d6fbb55

Browse files
fix(exec): resolve workspace-local bin under the linked install strategy (npm#9639)
In continuation of our exploration of using `install-strategy=linked` in the [Gutenberg monorepo](WordPress/gutenberg#75814), which powers the WordPress Block Editor. Under `install-strategy=linked`, `npm exec -w <ws> -- <bin>` ignored a workspace-local bin (provided by a sibling workspace dependency) and fell through to the registry, producing a spurious `E404`. The hoisted strategy ran the local bin correctly. ## Why For a workspace exec, the command computed the local bin directory as `resolve(this.npm.localDir, name, 'node_modules', '.bin')`, i.e. `<root>/node_modules/<name>/node_modules/.bin`. That path only resolves when the workspace is symlinked into the root `node_modules` as `<name>`, which is how the hoisted strategy lays workspaces out. The linked strategy does not hoist workspaces into the root `node_modules`; the workspace's real bin lives at `<workspace>/node_modules/.bin`. libnpmexec walks up from the given bin directory looking for `node_modules/.bin/<bin>`, so starting from the nonexistent hoisted path never reached the workspace's actual bin and the lookup fell back to the registry. ## How Base the local bin directory on the workspace's own path (`runPath`) instead of the hoisted `localDir/<name>` location. This is correct under both strategies: linked finds the bin in the workspace's `node_modules/.bin`, and hoisted still finds the root-hoisted bin because the walk-up continues from the workspace directory to the root `node_modules/.bin`. ## References Fixes npm#9616
1 parent 7e29a19 commit d6fbb55

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎lib/commands/exec.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ class Exec extends BaseCommand {
4242
}
4343
}
4444

45-
async callExec (args, { name, locationMsg, runPath } = {}) {
45+
async callExec (args, { locationMsg, runPath } = {}) {
4646
let localBin = this.npm.localBin
4747
let pkgPath = this.npm.localPrefix
4848

4949
// This is where libnpmexec will actually run the scripts from
5050
if (!runPath) {
5151
runPath = process.cwd()
5252
} else {
53-
// We have to consider if the workspace has its own separate versions libnpmexec will walk up to localDir after looking here
54-
localBin = resolve(this.npm.localDir, name, 'node_modules', '.bin')
53+
// Use the workspace's own node_modules/.bin, not localDir/<name>, since the linked strategy does not symlink workspaces into the root node_modules.
54+
localBin = resolve(runPath, 'node_modules', '.bin')
5555
// We also need to look for `bin` entries in the workspace package.json
5656
// libnpmexec will NOT look in the project root for the bin entry
5757
pkgPath = runPath

‎test/lib/commands/exec.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,41 @@ t.test('finds workspace dep first', async t => {
220220
t.ok(exists.isFile(), 'bin ran, creating file')
221221
})
222222

223+
t.test('finds workspace dep bin under linked install strategy', async t => {
224+
const { npm } = await loadMockNpm(t, {
225+
config: {
226+
'install-strategy': 'linked',
227+
},
228+
prefixDir: {
229+
'package.json': JSON.stringify({
230+
name: '@npmcli/npx-workspace-root-test',
231+
workspaces: ['workspace-a', 'tool'],
232+
}),
233+
'workspace-a': {
234+
'package.json': JSON.stringify({
235+
name: 'workspace-a',
236+
dependencies: { tool: '*' },
237+
}),
238+
},
239+
tool: {
240+
'package.json': JSON.stringify({
241+
name: 'tool',
242+
version: '1.0.0',
243+
bin: { 'npx-test': 'index.js' },
244+
}),
245+
'index.js': `#!/usr/bin/env node
246+
require('fs').writeFileSync('npm-exec-test-success', '')`,
247+
},
248+
},
249+
})
250+
251+
await npm.exec('install', [])
252+
npm.config.set('workspace', ['workspace-a'])
253+
await npm.exec('exec', ['npx-test'])
254+
const exists = await fs.stat(path.join(npm.prefix, 'workspace-a', 'npm-exec-test-success'))
255+
t.ok(exists.isFile(), 'workspace-local bin ran instead of falling back to the registry')
256+
})
257+
223258
t.test('npx --no-install @npmcli/npx-test', async t => {
224259
const registry = new MockRegistry({
225260
tap: t,

0 commit comments

Comments
 (0)