From bee942eb631db120a1f4b5e62f646c092060b89d Mon Sep 17 00:00:00 2001 From: Milan Meva Date: Thu, 11 Jul 2024 14:48:38 -0400 Subject: [PATCH] fix(exec): check if globalPath exists before passing to arborist --- workspaces/libnpmexec/lib/file-exists.js | 10 ++++++++++ workspaces/libnpmexec/lib/index.js | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/workspaces/libnpmexec/lib/file-exists.js b/workspaces/libnpmexec/lib/file-exists.js index 8a1a88adee993..cecc602489284 100644 --- a/workspaces/libnpmexec/lib/file-exists.js +++ b/workspaces/libnpmexec/lib/file-exists.js @@ -11,6 +11,15 @@ const fileExists = async (file) => { } } +const directoryExists = async (path) => { + try { + const res = await stat(path) + return res.isDirectory() + } catch { + return false + } +} + const localFileExists = async (dir, binName, root) => { for (const path of walkUp(dir)) { const binDir = resolve(path, 'node_modules', '.bin') @@ -30,4 +39,5 @@ const localFileExists = async (dir, binName, root) => { module.exports = { fileExists, localFileExists, + directoryExists, } diff --git a/workspaces/libnpmexec/lib/index.js b/workspaces/libnpmexec/lib/index.js index 3d9d24469ac59..ccfa1eae8af6d 100644 --- a/workspaces/libnpmexec/lib/index.js +++ b/workspaces/libnpmexec/lib/index.js @@ -9,7 +9,7 @@ const npa = require('npm-package-arg') const pacote = require('pacote') const { read } = require('read') const semver = require('semver') -const { fileExists, localFileExists } = require('./file-exists.js') +const { fileExists, localFileExists, directoryExists } = require('./file-exists.js') const getBinFromManifest = require('./get-bin-from-manifest.js') const noTTY = require('./no-tty.js') const runScript = require('./run-script.js') @@ -201,7 +201,7 @@ const exec = async (opts) => { args[0] = getBinFromManifest(commandManifest) - if (needInstall.length > 0 && globalPath) { + if (needInstall.length > 0 && await directoryExists(globalPath)) { // See if the package is installed globally, and run the translated bin const globalArb = new Arborist({ ...flatOptions, path: globalPath, global: true }) const globalTree = await globalArb.loadActual()