From 9214be9ed8779493e00d193e36a930918a30be64 Mon Sep 17 00:00:00 2001 From: milaninfy <111582375+milaninfy@users.noreply.github.com> Date: Thu, 11 Jul 2024 15:58:45 -0400 Subject: [PATCH] fix: gracefully handle nonexistent global installation directory (#7640) Handle arborist error when loading and checking package in global tree. --- workspaces/libnpmexec/lib/index.js | 19 ++++++++++++------- workspaces/libnpmexec/test/registry.js | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/workspaces/libnpmexec/lib/index.js b/workspaces/libnpmexec/lib/index.js index 3d9d24469ac59..8344471696e25 100644 --- a/workspaces/libnpmexec/lib/index.js +++ b/workspaces/libnpmexec/lib/index.js @@ -202,14 +202,19 @@ const exec = async (opts) => { args[0] = getBinFromManifest(commandManifest) if (needInstall.length > 0 && globalPath) { - // See if the package is installed globally, and run the translated bin + // See if the package is installed globally. If it is, run the translated bin const globalArb = new Arborist({ ...flatOptions, path: globalPath, global: true }) - const globalTree = await globalArb.loadActual() - const { manifest: globalManifest } = - await missingFromTree({ spec, tree: globalTree, flatOptions, shallow: true }) - if (!globalManifest && await fileExists(`${globalBin}/${args[0]}`)) { - binPaths.push(globalBin) - return await run() + const globalTree = await globalArb.loadActual().catch(() => { + log.verbose(`Could not read global path ${globalPath}, ignoring`) + return null + }) + if (globalTree) { + const { manifest: globalManifest } = + await missingFromTree({ spec, tree: globalTree, flatOptions, shallow: true }) + if (!globalManifest && await fileExists(`${globalBin}/${args[0]}`)) { + binPaths.push(globalBin) + return await run() + } } } } diff --git a/workspaces/libnpmexec/test/registry.js b/workspaces/libnpmexec/test/registry.js index 81c5f90bb2b4e..a9ae2cb8a597e 100644 --- a/workspaces/libnpmexec/test/registry.js +++ b/workspaces/libnpmexec/test/registry.js @@ -226,3 +226,22 @@ t.test('packages with different versions in the global tree', async t => { created: 'global/node_modules/@npmcli/A/bin-file.js', }) }) + +t.test('run from registry - non existant global path', async t => { + const { fixtures, package } = createPkg({ versions: ['2.0.0'] }) + + const { exec, path, registry, readOutput } = setup(t, { + testdir: fixtures, + }) + + await package({ registry, path }) + + await exec({ + args: ['@npmcli/create-index'], + globalPath: resolve(path, 'non-existant'), + }) + + t.match(await readOutput('@npmcli-create-index'), { + value: 'packages-2.0.0', + }) +})