From daaf4619c85ecf62346770735cfa8e2ddecbef8b Mon Sep 17 00:00:00 2001 From: Gar Date: Wed, 10 Aug 2022 09:34:24 -0700 Subject: fix: ignore global prefix if --prefix is used (#5291) When `--prefix` is used, both the local and global prefix values are set to be identical. This is functionally broken because their directory structures are inherently different (for instance, in posix the tree is in `lib/node_modules` in the global prefix). This commit makes npm exec ignore the global folders if it detects both local and global prefix are identical. --- test/lib/commands/exec.js | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/lib/commands/exec.js b/test/lib/commands/exec.js index 6e4c3e224..f2bccf9e0 100644 --- a/test/lib/commands/exec.js +++ b/test/lib/commands/exec.js @@ -28,6 +28,7 @@ t.test('registry package', async t => { const { npm } = await loadMockNpm(t, { config: { + audit: false, yes: true, }, prefixDir: { @@ -43,7 +44,46 @@ t.test('registry package', async t => { }) await registry.package({ - times: 2, + manifest, + tarballs: { + '1.0.0': path.join(npm.prefix, 'npm-exec-test'), + } }) + + await npm.exec('exec', ['@npmcli/npx-test']) + const exists = await fs.stat(path.join(npm.prefix, 'npm-exec-test-success')) + t.ok(exists.isFile(), 'bin ran, creating file') +}) + +t.test('--prefix', async t => { + const registry = new MockRegistry({ + tap: t, + registry: 'https://registry.npmjs.org/', + }) + + const manifest = registry.manifest({ name: '@npmcli/npx-test' }) + manifest.versions['1.0.0'].bin = { 'npx-test': 'index.js' } + + const { npm } = await loadMockNpm(t, { + config: { + audit: false, + yes: true, + }, + prefixDir: { + 'npm-exec-test': { + 'package.json': JSON.stringify(manifest), + 'index.js': `#!/usr/bin/env node + require('fs').writeFileSync('npm-exec-test-success', '')`, + }, + }, + globals: ({ prefix }) => ({ + 'process.cwd': () => prefix, + }), + }) + + // This is what `--prefix` does + npm.globalPrefix = npm.localPrefix + + await registry.package({ manifest, tarballs: { '1.0.0': path.join(npm.prefix, 'npm-exec-test'), @@ -65,6 +105,7 @@ t.test('workspaces', async t => { const { npm } = await loadMockNpm(t, { config: { + audit: false, yes: true, workspace: ['workspace-a'], }, -- cgit v1.2.3