mirror of
https://github.com/pnpm/pnpm.git
synced 2026-03-24 01:51:41 -04:00
fix: don't include specifiers field in new experimental lockfile format (#5110)
* fix: don't include specifiers field in new experimental lockfile format * test(lockfile-file): update arguments in normalizeLockfile test * test: lockfile-file Co-authored-by: Brandon Cheng <gluxon@users.noreply.github.com>
This commit is contained in:
@@ -72,13 +72,16 @@ async function writeLockfile (
|
||||
? convertToInlineSpecifiersFormat(wantedLockfile) as unknown as Lockfile
|
||||
: wantedLockfile
|
||||
|
||||
const yamlDoc = yamlStringify(lockfileToStringify, opts?.forceSharedFormat === true)
|
||||
const yamlDoc = yamlStringify(lockfileToStringify, {
|
||||
forceSharedFormat: opts?.forceSharedFormat === true,
|
||||
includeEmptySpecifiersField: !opts?.useInlineSpecifiersFormat,
|
||||
})
|
||||
|
||||
return writeFileAtomic(lockfilePath, yamlDoc)
|
||||
}
|
||||
|
||||
function yamlStringify (lockfile: Lockfile, forceSharedFormat: boolean) {
|
||||
let normalizedLockfile = normalizeLockfile(lockfile, forceSharedFormat)
|
||||
function yamlStringify (lockfile: Lockfile, opts: NormalizeLockfileOpts) {
|
||||
let normalizedLockfile = normalizeLockfile(lockfile, opts)
|
||||
normalizedLockfile = sortLockfileKeys(normalizedLockfile)
|
||||
return yaml.dump(normalizedLockfile, LOCKFILE_YAML_FORMAT)
|
||||
}
|
||||
@@ -89,9 +92,14 @@ function isEmptyLockfile (lockfile: Lockfile) {
|
||||
|
||||
export type LockfileFile = Omit<Lockfile, 'importers'> & Partial<ProjectSnapshot> & Partial<Pick<Lockfile, 'importers'>>
|
||||
|
||||
export function normalizeLockfile (lockfile: Lockfile, forceSharedFormat: boolean) {
|
||||
export interface NormalizeLockfileOpts {
|
||||
forceSharedFormat: boolean
|
||||
includeEmptySpecifiersField: boolean
|
||||
}
|
||||
|
||||
export function normalizeLockfile (lockfile: Lockfile, opts: NormalizeLockfileOpts) {
|
||||
let lockfileToSave!: LockfileFile
|
||||
if (!forceSharedFormat && equals(Object.keys(lockfile.importers), ['.'])) {
|
||||
if (!opts.forceSharedFormat && equals(Object.keys(lockfile.importers), ['.'])) {
|
||||
lockfileToSave = {
|
||||
...lockfile,
|
||||
...lockfile.importers['.'],
|
||||
@@ -110,8 +118,9 @@ export function normalizeLockfile (lockfile: Lockfile, forceSharedFormat: boolea
|
||||
...lockfile,
|
||||
importers: Object.keys(lockfile.importers).reduce((acc, alias) => {
|
||||
const importer = lockfile.importers[alias]
|
||||
const normalizedImporter: ProjectSnapshot = {
|
||||
specifiers: importer.specifiers ?? {},
|
||||
const normalizedImporter: Partial<ProjectSnapshot> = {}
|
||||
if (!isEmpty(importer.specifiers ?? {}) || opts.includeEmptySpecifiersField) {
|
||||
normalizedImporter['specifiers'] = importer.specifiers ?? {}
|
||||
}
|
||||
if (importer.dependenciesMeta != null && !isEmpty(importer.dependenciesMeta)) {
|
||||
normalizedImporter['dependenciesMeta'] = importer.dependenciesMeta
|
||||
@@ -183,7 +192,11 @@ export default async function writeLockfiles (
|
||||
const wantedLockfileToStringify = (opts.useInlineSpecifiersFormat ?? false)
|
||||
? convertToInlineSpecifiersFormat(opts.wantedLockfile) as unknown as Lockfile
|
||||
: opts.wantedLockfile
|
||||
const yamlDoc = yamlStringify(wantedLockfileToStringify, forceSharedFormat)
|
||||
const normalizeOpts = {
|
||||
forceSharedFormat,
|
||||
includeEmptySpecifiersField: !opts.useInlineSpecifiersFormat,
|
||||
}
|
||||
const yamlDoc = yamlStringify(wantedLockfileToStringify, normalizeOpts)
|
||||
|
||||
// in most cases the `pnpm-lock.yaml` and `node_modules/.pnpm-lock.yaml` are equal
|
||||
// in those cases the YAML document can be stringified only once for both files
|
||||
@@ -204,7 +217,7 @@ export default async function writeLockfiles (
|
||||
prefix: opts.wantedLockfileDir,
|
||||
})
|
||||
|
||||
const currentYamlDoc = yamlStringify(opts.currentLockfile, forceSharedFormat)
|
||||
const currentYamlDoc = yamlStringify(opts.currentLockfile, normalizeOpts)
|
||||
|
||||
await Promise.all([
|
||||
writeFileAtomic(wantedLockfilePath, yamlDoc),
|
||||
|
||||
@@ -20,7 +20,10 @@ test('empty overrides and neverBuiltDependencies are removed during lockfile nor
|
||||
},
|
||||
},
|
||||
},
|
||||
}, false)).toStrictEqual({
|
||||
}, {
|
||||
forceSharedFormat: false,
|
||||
includeEmptySpecifiersField: false,
|
||||
})).toStrictEqual({
|
||||
lockfileVersion: LOCKFILE_VERSION,
|
||||
onlyBuiltDependencies: [],
|
||||
importers: {
|
||||
@@ -35,3 +38,45 @@ test('empty overrides and neverBuiltDependencies are removed during lockfile nor
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
test('empty specifiers field is preserved', () => {
|
||||
expect(normalizeLockfile({
|
||||
lockfileVersion: LOCKFILE_VERSION,
|
||||
packages: {},
|
||||
importers: {
|
||||
foo: {
|
||||
specifiers: {},
|
||||
},
|
||||
},
|
||||
}, {
|
||||
forceSharedFormat: false,
|
||||
includeEmptySpecifiersField: true,
|
||||
})).toStrictEqual({
|
||||
lockfileVersion: LOCKFILE_VERSION,
|
||||
importers: {
|
||||
foo: {
|
||||
specifiers: {},
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
test('empty specifiers field is removed', () => {
|
||||
expect(normalizeLockfile({
|
||||
lockfileVersion: LOCKFILE_VERSION,
|
||||
packages: {},
|
||||
importers: {
|
||||
foo: {
|
||||
specifiers: {},
|
||||
},
|
||||
},
|
||||
}, {
|
||||
forceSharedFormat: false,
|
||||
includeEmptySpecifiersField: false,
|
||||
})).toStrictEqual({
|
||||
lockfileVersion: LOCKFILE_VERSION,
|
||||
importers: {
|
||||
foo: {},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
@@ -57,7 +57,7 @@ export default (lockfile: Lockfile, pkg: ProjectManifest, importerId: string, op
|
||||
return false
|
||||
}
|
||||
for (const depName of pkgDepNames) {
|
||||
if (!importerDeps[depName] || importer.specifiers[depName] !== pkgDeps[depName]) return false
|
||||
if (!importerDeps[depName] || importer.specifiers?.[depName] !== pkgDeps[depName]) return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user