From 25b425ca2571a4e54f5d69d6d77ef3b91c98dd1a Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Wed, 29 Jul 2020 01:46:34 +0300 Subject: [PATCH] fix: don't remove the modules directory, just the contents of it close #2720 PR #2722 --- .changeset/dirty-comics-doubt.md | 5 +++++ packages/get-context/src/index.ts | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .changeset/dirty-comics-doubt.md diff --git a/.changeset/dirty-comics-doubt.md b/.changeset/dirty-comics-doubt.md new file mode 100644 index 0000000000..6988ed45d1 --- /dev/null +++ b/.changeset/dirty-comics-doubt.md @@ -0,0 +1,5 @@ +--- +"@pnpm/get-context": patch +--- + +When purging an incompatible modules directory, don't remove the actual directory, just the contents of it. diff --git a/packages/get-context/src/index.ts b/packages/get-context/src/index.ts index 835107f45b..5dbe559132 100644 --- a/packages/get-context/src/index.ts +++ b/packages/get-context/src/index.ts @@ -276,12 +276,22 @@ async function purgeModulesDirsOfImporter ( prefix: importer.rootDir, }) try { - await rimraf(importer.modulesDir) + // We don't remove the actual modules directory, just the contents of it. + // 1. we will need the directory anyway. + // 2. in some setups, pnpm won't even have permission to remove the modules directory. + await removeContentsOfDir(importer.modulesDir) } catch (err) { if (err.code !== 'ENOENT') throw err } } +async function removeContentsOfDir (dir: string) { + const items = await fs.readdir(dir) + for (const item of items) { + await rimraf(path.join(dir, item)) + } +} + function stringifyIncludedDeps (included: IncludedDependencies) { return DEPENDENCIES_FIELDS.filter((depsField) => included[depsField]).join(', ') }