Skip to content

Commit

Permalink
fix: don't create an empty node_modules directory (#6482)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Apr 28, 2023
1 parent 893936b commit e6b83c8
Show file tree
Hide file tree
Showing 21 changed files with 144 additions and 49 deletions.
6 changes: 6 additions & 0 deletions .changeset/twenty-snakes-deny.md
@@ -0,0 +1,6 @@
---
"@pnpm/modules-yaml": minor
"pnpm": patch
---

Do not create a `node_modules` folder with a `.modules.yaml` file if there are now dependencies inside `node_modules`.
2 changes: 1 addition & 1 deletion __utils__/prepare/package.json
Expand Up @@ -9,7 +9,7 @@
"unique-string": "^2.0.0",
"write-json5-file": "^3.1.0",
"write-pkg": "4.0.0",
"write-yaml-file": "^4.2.0"
"write-yaml-file": "^5.0.0"
},
"devDependencies": {
"@pnpm/prepare": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion exec/plugin-commands-rebuild/package.json
Expand Up @@ -46,7 +46,7 @@
"execa": "npm:safe-execa@0.1.2",
"path-exists": "^4.0.0",
"sinon": "^15.0.4",
"write-yaml-file": "^4.2.0"
"write-yaml-file": "^5.0.0"
},
"dependencies": {
"@pnpm/cli-utils": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion exec/plugin-commands-script-runners/package.json
Expand Up @@ -41,7 +41,7 @@
"@types/is-windows": "^1.0.0",
"@types/ramda": "0.28.20",
"is-windows": "^1.0.2",
"write-yaml-file": "^4.2.0"
"write-yaml-file": "^5.0.0"
},
"dependencies": {
"@pnpm/cli-utils": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion lockfile/filter-lockfile/package.json
Expand Up @@ -38,7 +38,7 @@
"@types/ramda": "0.28.20",
"detect-libc": "^2.0.1",
"tempy": "^1.0.1",
"write-yaml-file": "^4.2.0",
"write-yaml-file": "^5.0.0",
"yaml-tag": "1.1.0"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion lockfile/lockfile-file/package.json
Expand Up @@ -42,7 +42,7 @@
"@types/semver": "7.3.13",
"@types/write-file-atomic": "^4.0.0",
"tempy": "^1.0.1",
"write-yaml-file": "^4.2.0",
"write-yaml-file": "^5.0.0",
"yaml-tag": "1.1.0"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion lockfile/lockfile-utils/package.json
Expand Up @@ -35,7 +35,7 @@
"@types/js-yaml": "^4.0.5",
"@types/ramda": "0.28.20",
"tempy": "^1.0.1",
"write-yaml-file": "^4.2.0",
"write-yaml-file": "^5.0.0",
"yaml-tag": "1.1.0"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion patching/plugin-commands-patching/package.json
Expand Up @@ -41,7 +41,7 @@
"@types/normalize-path": "^3.0.0",
"@types/ramda": "0.28.20",
"@types/semver": "7.3.13",
"write-yaml-file": "^4.2.0"
"write-yaml-file": "^5.0.0"
},
"dependencies": {
"@pnpm/cli-utils": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion pkg-manager/core/package.json
Expand Up @@ -101,7 +101,7 @@
"sinon": "^15.0.4",
"symlink-dir": "^5.1.1",
"write-json-file": "^4.3.0",
"write-yaml-file": "^4.2.0"
"write-yaml-file": "^5.0.0"
},
"directories": {
"test": "test"
Expand Down
2 changes: 2 additions & 0 deletions pkg-manager/core/src/install/index.ts
Expand Up @@ -1195,6 +1195,8 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => {
skipped: Array.from(ctx.skipped),
storeDir: ctx.storeDir,
virtualStoreDir: ctx.virtualStoreDir,
}, {
makeModulesDir: Object.keys(result.currentLockfile.packages ?? {}).length > 0,
})
})(),
])
Expand Down
2 changes: 2 additions & 0 deletions pkg-manager/headless/src/index.ts
Expand Up @@ -566,6 +566,8 @@ export async function headlessInstall (opts: HeadlessOptions) {
skipped: Array.from(skipped),
storeDir: opts.storeDir,
virtualStoreDir,
}, {
makeModulesDir: Object.keys(filteredLockfile.packages ?? {}).length > 0,
})
if (opts.useLockfile) {
// We need to write the wanted lockfile as well.
Expand Down
2 changes: 1 addition & 1 deletion pkg-manager/modules-yaml/package.json
Expand Up @@ -35,7 +35,7 @@
"is-windows": "^1.0.2",
"ramda": "npm:@pnpm/ramda@0.28.1",
"read-yaml-file": "^2.1.0",
"write-yaml-file": "^4.2.0"
"write-yaml-file": "^5.0.0"
},
"devDependencies": {
"@pnpm/modules-yaml": "workspace:*",
Expand Down
14 changes: 12 additions & 2 deletions pkg-manager/modules-yaml/src/index.ts
Expand Up @@ -91,7 +91,10 @@ const YAML_OPTS = {

export async function writeModulesManifest (
modulesDir: string,
modules: Modules & { registries: Registries }
modules: Modules & { registries: Registries },
opts?: {
makeModulesDir?: boolean
}
) {
const modulesYamlPath = path.join(modulesDir, MODULES_FILENAME)
const saveModules = { ...modules }
Expand All @@ -114,5 +117,12 @@ export async function writeModulesManifest (
if (!isWindows()) {
saveModules.virtualStoreDir = path.relative(modulesDir, saveModules.virtualStoreDir)
}
return writeYamlFile(modulesYamlPath, saveModules, YAML_OPTS)
try {
await writeYamlFile(modulesYamlPath, saveModules, {
...YAML_OPTS,
makeDir: opts?.makeModulesDir ?? false,
})
} catch (err: any) { // eslint-disable-line
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err
}
}
53 changes: 53 additions & 0 deletions pkg-manager/modules-yaml/test/index.ts
@@ -1,4 +1,5 @@
/// <reference path="../../../__typings__/index.d.ts"/>
import fs from 'fs'
import path from 'path'
import { readModulesManifest, writeModulesManifest } from '@pnpm/modules-yaml'
import readYamlFile from 'read-yaml-file'
Expand Down Expand Up @@ -60,3 +61,55 @@ test('backward compatible read of .modules.yaml created with shamefully-hoist=fa
'/body-parser/1.19.0': { 'body-parser': 'private' },
})
})

test('readModulesManifest() should not create a node_modules directory if it does not exist', async () => {
const modulesDir = path.join(tempy.directory(), 'node_modules')
const modulesYaml = {
hoistedDependencies: {},
included: {
dependencies: true,
devDependencies: true,
optionalDependencies: true,
},
layoutVersion: 1,
packageManager: 'pnpm@2',
pendingBuilds: [],
publicHoistPattern: [],
prunedAt: new Date().toUTCString(),
registries: {
default: 'https://registry.npmjs.org/',
},
shamefullyHoist: false,
skipped: [],
storeDir: '/.pnpm-store',
virtualStoreDir: path.join(modulesDir, '.pnpm'),
}
await writeModulesManifest(modulesDir, modulesYaml)
expect(fs.existsSync(modulesDir)).toBeFalsy()
})

test('readModulesManifest() should create a node_modules directory if makeModuleDir is set to true', async () => {
const modulesDir = path.join(tempy.directory(), 'node_modules')
const modulesYaml = {
hoistedDependencies: {},
included: {
dependencies: true,
devDependencies: true,
optionalDependencies: true,
},
layoutVersion: 1,
packageManager: 'pnpm@2',
pendingBuilds: [],
publicHoistPattern: [],
prunedAt: new Date().toUTCString(),
registries: {
default: 'https://registry.npmjs.org/',
},
shamefullyHoist: false,
skipped: [],
storeDir: '/.pnpm-store',
virtualStoreDir: path.join(modulesDir, '.pnpm'),
}
await writeModulesManifest(modulesDir, modulesYaml, { makeModulesDir: true })
expect(await readModulesManifest(modulesDir)).toEqual(modulesYaml)
})
2 changes: 1 addition & 1 deletion pkg-manager/plugin-commands-installation/package.json
Expand Up @@ -55,7 +55,7 @@
"tempy": "^1.0.1",
"write-json-file": "^4.3.0",
"write-pkg": "4.0.0",
"write-yaml-file": "^4.2.0"
"write-yaml-file": "^5.0.0"
},
"dependencies": {
"@pnpm/cli-utils": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion pkg-manifest/exportable-manifest/package.json
Expand Up @@ -34,7 +34,7 @@
"@types/cross-spawn": "^6.0.2",
"@types/ramda": "0.28.20",
"cross-spawn": "^7.0.3",
"write-yaml-file": "^4.2.0"
"write-yaml-file": "^5.0.0"
},
"dependencies": {
"@pnpm/error": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion pkg-manifest/write-project-manifest/package.json
Expand Up @@ -33,7 +33,7 @@
"@pnpm/types": "workspace:*",
"json5": "^2.2.3",
"write-file-atomic": "^5.0.0",
"write-yaml-file": "^4.2.0"
"write-yaml-file": "^5.0.0"
},
"devDependencies": {
"@pnpm/write-project-manifest": "workspace:*",
Expand Down

0 comments on commit e6b83c8

Please sign in to comment.