Skip to content

Commit

Permalink
fix: much clearer npx 'canceled' error (#6642)
Browse files Browse the repository at this point in the history
Co-authored-by: AaronHamilton965 <91709196+AaronHamilton965@users.noreply.github.com>
  • Loading branch information
rahulio96 and AaronHamilton965 authored Jul 17, 2023
1 parent b1c3256 commit 02c7ddb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
35 changes: 35 additions & 0 deletions test/lib/commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,38 @@ t.test('workspaces', async t => {
const exists = await fs.stat(path.join(npm.prefix, 'workspace-a', 'npm-exec-test-success'))
t.ok(exists.isFile(), 'bin ran, creating file inside workspace')
})

t.test('npx --no-install @npmcli/npx-test', 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: false,
},
prefixDir: {
'npm-exec-test': {
'package.json': JSON.stringify(manifest),
'index.js': `#!/usr/bin/env node
require('fs').writeFileSync('npm-exec-test-success', '')`,
},
},
})

try {
await npm.exec('exec', ['@npmcli/npx-test'])
t.fail('Expected error was not thrown')
} catch (error) {
t.match(
error.message,
'npx canceled due to missing packages and no YES option: ',
'Expected error message thrown'
)
}
})
8 changes: 5 additions & 3 deletions workspaces/libnpmexec/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,12 @@ const exec = async (opts) => {

if (add.length) {
if (!yes) {
const missingPackages = add.map(a => `${a.replace(/@$/, '')}`)
// set -n to always say no
if (yes === false) {
throw new Error('canceled')
// Error message lists missing package(s) when process is canceled
/* eslint-disable-next-line max-len */
throw new Error(`npx canceled due to missing packages and no YES option: ${JSON.stringify(missingPackages)}`)
}

if (noTTY() || ciInfo.isCI) {
Expand All @@ -257,8 +260,7 @@ const exec = async (opts) => {
add.map((pkg) => pkg.replace(/@$/, '')).join(', ')
}`)
} else {
const addList = add.map(a => ` ${a.replace(/@$/, '')}`)
.join('\n') + '\n'
const addList = missingPackages.join('\n') + '\n'
const prompt = `Need to install the following packages:\n${
addList
}Ok to proceed? `
Expand Down

0 comments on commit 02c7ddb

Please sign in to comment.