Skip to content

Commit 6d6cec1

Browse files
committed
fix(install): do not install invalid package name
Throws an usage error if finding an invalid argument in global install. Fixes: #3029
1 parent f37f7d2 commit 6d6cec1

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/commands/install.js

+6
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ class Install extends ArboristWorkspaceCmd {
140140
args = ['.']
141141
}
142142

143+
// throw usage error if trying to install empty package
144+
// name to global space, e.g: `npm i -g ""`
145+
if (where === globalTop && !args.every(Boolean)) {
146+
throw this.usageError()
147+
}
148+
143149
// TODO: Add warnings for other deprecated flags? or remove this one?
144150
if (isDev) {
145151
log.warn(

test/lib/commands/install.js

+17
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ t.test('should install globally using Arborist', async t => {
142142
t.strictSame(SCRIPTS, [], 'no scripts when installing globally')
143143
})
144144

145+
t.test('should not install invalid global package name', async t => {
146+
const { npm } = await loadMockNpm(t, {
147+
'@npmcli/run-script': () => {},
148+
'../../lib/utils/reify-finish.js': async () => {},
149+
'@npmcli/arborist': function (args) {
150+
throw new Error('should not reify')
151+
},
152+
})
153+
npm.config.set('global', true)
154+
npm.globalPrefix = path.resolve(t.testdir({}))
155+
await t.rejects(
156+
npm.exec('install', ['']),
157+
/Usage:/,
158+
'should not install invalid package name'
159+
)
160+
})
161+
145162
t.test('npm i -g npm engines check success', async t => {
146163
const { npm } = await loadMockNpm(t, {
147164
'../../lib/utils/reify-finish.js': async () => {},

0 commit comments

Comments
 (0)