Skip to content

Commit 9214be9

Browse files
authored
fix: gracefully handle nonexistent global installation directory (#7640)
Handle arborist error when loading and checking package in global tree.
1 parent dbe7d98 commit 9214be9

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

workspaces/libnpmexec/lib/index.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,19 @@ const exec = async (opts) => {
202202
args[0] = getBinFromManifest(commandManifest)
203203

204204
if (needInstall.length > 0 && globalPath) {
205-
// See if the package is installed globally, and run the translated bin
205+
// See if the package is installed globally. If it is, run the translated bin
206206
const globalArb = new Arborist({ ...flatOptions, path: globalPath, global: true })
207-
const globalTree = await globalArb.loadActual()
208-
const { manifest: globalManifest } =
209-
await missingFromTree({ spec, tree: globalTree, flatOptions, shallow: true })
210-
if (!globalManifest && await fileExists(`${globalBin}/${args[0]}`)) {
211-
binPaths.push(globalBin)
212-
return await run()
207+
const globalTree = await globalArb.loadActual().catch(() => {
208+
log.verbose(`Could not read global path ${globalPath}, ignoring`)
209+
return null
210+
})
211+
if (globalTree) {
212+
const { manifest: globalManifest } =
213+
await missingFromTree({ spec, tree: globalTree, flatOptions, shallow: true })
214+
if (!globalManifest && await fileExists(`${globalBin}/${args[0]}`)) {
215+
binPaths.push(globalBin)
216+
return await run()
217+
}
213218
}
214219
}
215220
}

workspaces/libnpmexec/test/registry.js

+19
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,22 @@ t.test('packages with different versions in the global tree', async t => {
226226
created: 'global/node_modules/@npmcli/A/bin-file.js',
227227
})
228228
})
229+
230+
t.test('run from registry - non existant global path', async t => {
231+
const { fixtures, package } = createPkg({ versions: ['2.0.0'] })
232+
233+
const { exec, path, registry, readOutput } = setup(t, {
234+
testdir: fixtures,
235+
})
236+
237+
await package({ registry, path })
238+
239+
await exec({
240+
args: ['@npmcli/create-index'],
241+
globalPath: resolve(path, 'non-existant'),
242+
})
243+
244+
t.match(await readOutput('@npmcli-create-index'), {
245+
value: 'packages-2.0.0',
246+
})
247+
})

0 commit comments

Comments
 (0)