diff --git a/workspaces/arborist/lib/arborist/reify.js b/workspaces/arborist/lib/arborist/reify.js index be920272d48f0..666342cf444b3 100644 --- a/workspaces/arborist/lib/arborist/reify.js +++ b/workspaces/arborist/lib/arborist/reify.js @@ -18,6 +18,7 @@ const { mkdir, rm, symlink, + access, } = require('node:fs/promises') const { moveFile } = require('@npmcli/fs') const PackageJson = require('@npmcli/package-json') @@ -123,10 +124,19 @@ module.exports = cls => class Reifier extends cls { // we do NOT want to set ownership on this folder, especially // recursively, because it can have other side effects to do that // in a project directory. We just want to make it if it's missing. - await mkdir(resolve(this.path), { recursive: true }) + const resolvedPath = resolve(this.path) + try { + await access(resolvedPath) + } catch (accessError) { + if (accessError.code === 'ENOENT') { + await mkdir(resolvedPath, { recursive: true }) + } else { + throw accessError + } + } // do not allow the top-level node_modules to be a symlink - await this.#validateNodeModules(resolve(this.path, 'node_modules')) + await this.#validateNodeModules(resolve(resolvedPath, 'node_modules')) } await this[_loadTrees](options)