Skip to content

Commit

Permalink
fix: gracefully handle nonexistent global installation directory (#7640)
Browse files Browse the repository at this point in the history
Handle arborist error when loading and checking package in global tree.
  • Loading branch information
milaninfy authored Jul 11, 2024
1 parent dbe7d98 commit 9214be9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
19 changes: 12 additions & 7 deletions workspaces/libnpmexec/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions workspaces/libnpmexec/test/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
})

1 comment on commit 9214be9

@ibrahimkhled

This comment was marked as spam.

Please sign in to comment.