perf: save node_modules/.modules.yaml in JSON format (#10406)

This commit is contained in:
Zoltan Kochan
2026-02-06 15:59:22 +01:00
parent 4471eb801f
commit 9821d2d0c8
10 changed files with 24 additions and 59 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/modules-yaml": major
---
Remove options from writeModulesManifest.

View File

@@ -0,0 +1,5 @@
---
"@pnpm/modules-yaml": minor
---
Save node_modules/.modules.yaml in JSON format.

View File

@@ -7,6 +7,7 @@ export default { // eslint-disable-line
createReadStream: gfs.createReadStream,
link: promisify(gfs.link),
linkSync: withEagainRetry(gfs.linkSync),
mkdir: promisify(gfs.mkdir),
mkdirSync: withEagainRetry(gfs.mkdirSync),
renameSync: withEagainRetry(gfs.renameSync),
readFile: promisify(gfs.readFile),

View File

@@ -1513,8 +1513,6 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => {
storeDir: ctx.storeDir,
virtualStoreDir: ctx.virtualStoreDir,
virtualStoreDirMaxLength: ctx.virtualStoreDirMaxLength,
}, {
makeModulesDir: Object.keys(result.currentLockfile.packages ?? {}).length > 0,
})
})(),
])

View File

@@ -644,8 +644,6 @@ export async function headlessInstall (opts: HeadlessOptions): Promise<Installat
storeDir: opts.storeDir,
virtualStoreDir,
virtualStoreDirMaxLength: opts.virtualStoreDirMaxLength,
}, {
makeModulesDir: Object.keys(filteredLockfile.packages ?? {}).length > 0,
})
const currentLockfileDir = path.join(rootModulesDir, '.pnpm')
if (opts.useLockfile) {

View File

@@ -33,11 +33,11 @@
"compile": "tsc --build && pnpm run lint --fix"
},
"dependencies": {
"@pnpm/graceful-fs": "workspace:*",
"@pnpm/types": "workspace:*",
"is-windows": "catalog:",
"ramda": "catalog:",
"read-yaml-file": "catalog:",
"write-yaml-file": "catalog:"
"read-yaml-file": "catalog:"
},
"devDependencies": {
"@pnpm/modules-yaml": "workspace:*",

View File

@@ -1,4 +1,5 @@
import path from 'path'
import fs from '@pnpm/graceful-fs'
import {
type DepPath,
type DependenciesField,
@@ -9,7 +10,6 @@ import {
import readYamlFile from 'read-yaml-file'
import mapValues from 'ramda/src/map'
import isWindows from 'is-windows'
import writeYamlFile from 'write-yaml-file'
// The dot prefix is needed because otherwise `npm shrinkwrap`
// thinks that it is an extraneous package.
@@ -102,23 +102,13 @@ export async function readModulesManifest (modulesDir: string): Promise<Modules
return modules
}
const YAML_OPTS = {
lineWidth: 1000,
noCompatMode: true,
noRefs: true,
sortKeys: true,
}
export interface StrictModules extends Modules {
registries: Registries
}
export async function writeModulesManifest (
modulesDir: string,
modules: StrictModules,
opts?: {
makeModulesDir?: boolean
}
modules: StrictModules
): Promise<void> {
const modulesYamlPath = path.join(modulesDir, MODULES_FILENAME)
const saveModules = { ...modules, ignoredBuilds: modules.ignoredBuilds ? Array.from(modules.ignoredBuilds) : undefined }
@@ -141,12 +131,6 @@ export async function writeModulesManifest (
if (!isWindows()) {
saveModules.virtualStoreDir = path.relative(modulesDir, saveModules.virtualStoreDir)
}
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
}
await fs.mkdir(modulesDir, { recursive: true })
await fs.writeFile(modulesYamlPath, JSON.stringify(saveModules, null, 2))
}

View File

@@ -1,5 +1,4 @@
/// <reference path="../../../__typings__/index.d.ts"/>
import fs from 'fs'
import path from 'path'
import { readModulesManifest, writeModulesManifest, type StrictModules } from '@pnpm/modules-yaml'
import { sync as readYamlFile } from 'read-yaml-file'
@@ -64,7 +63,7 @@ test('backward compatible read of .modules.yaml created with shamefully-hoist=fa
})
})
test('readModulesManifest() should not create a node_modules directory if it does not exist', async () => {
test('readModulesManifest() should create a node_modules directory', async () => {
const modulesDir = path.join(tempy.directory(), 'node_modules')
const modulesYaml: StrictModules = {
hoistedDependencies: {},
@@ -89,34 +88,6 @@ test('readModulesManifest() should not create a node_modules directory if it doe
virtualStoreDirMaxLength: 120,
}
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: StrictModules = {
hoistedDependencies: {},
included: {
dependencies: true,
devDependencies: true,
optionalDependencies: true,
},
ignoredBuilds: new Set(),
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'),
virtualStoreDirMaxLength: 120,
}
await writeModulesManifest(modulesDir, modulesYaml, { makeModulesDir: true })
expect(await readModulesManifest(modulesDir)).toEqual(modulesYaml)
})

View File

@@ -9,6 +9,9 @@
"../../__typings__/**/*.d.ts"
],
"references": [
{
"path": "../../fs/graceful-fs"
},
{
"path": "../../packages/types"
}

6
pnpm-lock.yaml generated
View File

@@ -5573,6 +5573,9 @@ importers:
pkg-manager/modules-yaml:
dependencies:
'@pnpm/graceful-fs':
specifier: workspace:*
version: link:../../fs/graceful-fs
'@pnpm/types':
specifier: workspace:*
version: link:../../packages/types
@@ -5585,9 +5588,6 @@ importers:
read-yaml-file:
specifier: 'catalog:'
version: 2.1.0
write-yaml-file:
specifier: 'catalog:'
version: 5.0.0
devDependencies:
'@pnpm/modules-yaml':
specifier: workspace:*