Skip to content

Commit da50c34

Browse files
authored
fix(arborist): reject uninstall args that carry a version (npm#9881)
## What / Why `npm uninstall [email protected]` neither removes the package nor complains about the argument. The spec goes straight through to Arborist as an `rm` entry, nothing in the tree is named `[email protected]`, so the reify finishes with "up to date" and `npm ls` still shows `vite`. `npm update` already validates its own argument list and throws `EUPDATEARGS` for anything that is not a bare package name. This applies the same rule to `rm`, so a version, tag or range now fails with `ERMARGS` and the message names the command to run instead. ## Testing New case in `workspaces/arborist/test/arborist/build-ideal-tree.js`, next to the existing rm test and mirroring the update one. It covers an exact version, a tag, ranges, a scoped name carrying a version, and a filesystem path, where the suggestion falls back to `<pkg>` because there is no name to print. It fails without the change. ## References Fixes npm#9880
1 parent 71915e8 commit da50c34

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

‎test/lib/commands/uninstall.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,34 @@ t.test('remove multiple installed libs', async t => {
143143
t.throws(() => fs.statSync(b), 'should have removed b package from nm')
144144
})
145145

146+
t.test('rejects an arg with a version spec', async t => {
147+
const { uninstall } = await mockNpm(t, {
148+
prefixDir: {
149+
'package.json': JSON.stringify({
150+
name: 'test-rm-version-spec',
151+
version: '1.0.0',
152+
dependencies: {
153+
foo: '*',
154+
},
155+
}),
156+
node_modules: {
157+
foo: {
158+
'package.json': JSON.stringify({
159+
name: 'foo',
160+
version: '1.0.0',
161+
}),
162+
},
163+
},
164+
},
165+
})
166+
167+
await t.rejects(
168+
uninstall(['foo@1']),
169+
{ code: 'ERMARGS', message: /npm rm foo/ },
170+
'should throw ERMARGS instead of silently no-oping'
171+
)
172+
})
173+
146174
t.test('no args local', async t => {
147175
const { uninstall } = await mockNpm(t)
148176

‎workspaces/arborist/lib/arborist/build-ideal-tree.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,22 @@ module.exports = cls => class IdealTreeBuilder extends cls {
397397
this[_updateNames] = update.names
398398

399399
this[_updateAll] = update.all
400+
401+
// validates list of rm names, they must
402+
// be dep names only, no semver ranges are supported
403+
for (const name of options.rm || []) {
404+
const spec = npa(name)
405+
const validationError =
406+
new TypeError(`Remove arguments must only contain package names, eg:
407+
npm rm ${spec.name || '<pkg>'}`)
408+
validationError.code = 'ERMARGS'
409+
410+
// If they gave us anything other than a bare package name
411+
if (spec.raw !== spec.name) {
412+
throw validationError
413+
}
414+
}
415+
400416
// we prune by default unless explicitly set to boolean false
401417
this.#prune = options.prune !== false
402418

‎workspaces/arborist/test/arborist/build-ideal-tree.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,47 @@ t.test('remove deps when initializing tree from actual tree', async t => {
23472347
t.equal(tree.children.get('foo'), undefined, 'removed foo child')
23482348
})
23492349

2350+
t.test('remove deps with a version spec', async t => {
2351+
const path = t.testdir({
2352+
node_modules: {
2353+
foo: {
2354+
'package.json': JSON.stringify({
2355+
name: 'foo',
2356+
version: '1.2.3',
2357+
}),
2358+
},
2359+
},
2360+
})
2361+
2362+
createRegistry(t, false)
2363+
const invalidArgs = [
2364+
2365+
'foo@next',
2366+
'foo@^1.0.0',
2367+
'foo@>=2.0.0',
2368+
'foo@2',
2369+
]
2370+
for (const rmName of invalidArgs) {
2371+
await t.rejects(
2372+
buildIdeal(path, { rm: [rmName] }),
2373+
{ code: 'ERMARGS', message: /npm rm foo/ },
2374+
'should throw an error when the package name has a version'
2375+
)
2376+
}
2377+
2378+
await t.rejects(
2379+
buildIdeal(path, { rm: ['@scope/[email protected]'] }),
2380+
{ code: 'ERMARGS', message: /npm rm @scope\/foo/ },
2381+
'should throw an error when a scoped package name has a version'
2382+
)
2383+
2384+
await t.rejects(
2385+
buildIdeal(path, { rm: ['./foo'] }),
2386+
{ code: 'ERMARGS', message: /npm rm <pkg>/ },
2387+
'should throw an error when the package is a path'
2388+
)
2389+
})
2390+
23502391
t.test('detect conflicts in transitive peerOptional deps', async t => {
23512392
const base = resolve(fixtures, 'test-conflicted-optional-peer-dep')
23522393

0 commit comments

Comments
 (0)