Skip to content

Commit 02c7ddb

Browse files
fix: much clearer npx 'canceled' error (#6642)
Co-authored-by: AaronHamilton965 <[email protected]>
1 parent b1c3256 commit 02c7ddb

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

test/lib/commands/exec.js

+35
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,38 @@ t.test('workspaces', async t => {
129129
const exists = await fs.stat(path.join(npm.prefix, 'workspace-a', 'npm-exec-test-success'))
130130
t.ok(exists.isFile(), 'bin ran, creating file inside workspace')
131131
})
132+
133+
t.test('npx --no-install @npmcli/npx-test', async t => {
134+
const registry = new MockRegistry({
135+
tap: t,
136+
registry: 'https://registry.npmjs.org/',
137+
})
138+
139+
const manifest = registry.manifest({ name: '@npmcli/npx-test' })
140+
manifest.versions['1.0.0'].bin = { 'npx-test': 'index.js' }
141+
142+
const { npm } = await loadMockNpm(t, {
143+
config: {
144+
audit: false,
145+
yes: false,
146+
},
147+
prefixDir: {
148+
'npm-exec-test': {
149+
'package.json': JSON.stringify(manifest),
150+
'index.js': `#!/usr/bin/env node
151+
require('fs').writeFileSync('npm-exec-test-success', '')`,
152+
},
153+
},
154+
})
155+
156+
try {
157+
await npm.exec('exec', ['@npmcli/npx-test'])
158+
t.fail('Expected error was not thrown')
159+
} catch (error) {
160+
t.match(
161+
error.message,
162+
'npx canceled due to missing packages and no YES option: ',
163+
'Expected error message thrown'
164+
)
165+
}
166+
})

workspaces/libnpmexec/lib/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,12 @@ const exec = async (opts) => {
245245

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

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

0 commit comments

Comments
 (0)