fix: time field in pnpm-lock.yaml

This commit is contained in:
Zoltan Kochan
2022-09-03 22:25:19 +03:00
parent c1b968e759
commit 0373af22e2
7 changed files with 100 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/lockfile-file": patch
"@pnpm/resolve-dependencies": patch
---
Always correctly update the "time" field in "pnpm-lock.yaml".

View File

@@ -55,6 +55,7 @@
"@pnpm/types": "workspace:*",
"@zkochan/rimraf": "^2.1.2",
"comver-to-semver": "^1.0.0",
"dependency-path": "workspace:*",
"js-yaml": "npm:@zkochan/js-yaml@^0.0.6",
"normalize-path": "^3.0.0",
"ramda": "npm:@pnpm/ramda@0.28.1",

View File

@@ -4,8 +4,10 @@ import { DEPENDENCIES_FIELDS } from '@pnpm/types'
import { Lockfile, ProjectSnapshot } from '@pnpm/lockfile-types'
import { WANTED_LOCKFILE } from '@pnpm/constants'
import rimraf from '@zkochan/rimraf'
import * as dp from 'dependency-path'
import yaml from 'js-yaml'
import equals from 'ramda/src/equals'
import fromPairs from 'ramda/src/fromPairs'
import isEmpty from 'ramda/src/isEmpty'
import writeFileAtomicCB from 'write-file-atomic'
import logger from './logger'
@@ -141,6 +143,9 @@ export function normalizeLockfile (lockfile: Lockfile, opts: NormalizeLockfileOp
delete lockfileToSave.packages
}
}
if (lockfileToSave.time) {
lockfileToSave.time = pruneTime(lockfileToSave.time, lockfile.importers)
}
if ((lockfileToSave.overrides != null) && isEmpty(lockfileToSave.overrides)) {
delete lockfileToSave.overrides
}
@@ -163,6 +168,25 @@ export function normalizeLockfile (lockfile: Lockfile, opts: NormalizeLockfileOp
return lockfileToSave
}
function pruneTime (time: Record<string, string>, importers: Record<string, ProjectSnapshot>) {
const rootDepPaths = new Set<string>()
for (const importer of Object.values(importers)) {
for (const depType of DEPENDENCIES_FIELDS) {
for (let [depName, ref] of Object.entries(importer[depType] ?? {})) {
if (ref['version']) {
ref = ref['version']
}
const suffixStart = ref.indexOf('_')
const refWithoutPeerSuffix = suffixStart === -1 ? ref : ref.slice(0, suffixStart)
const depPath = dp.refToRelative(refWithoutPeerSuffix, depName)
if (!depPath) continue
rootDepPaths.add(depPath)
}
}
}
return fromPairs(Object.entries(time).filter(([depPath]) => rootDepPaths.has(depPath)))
}
export default async function writeLockfiles (
opts: {
forceSharedFormat?: boolean

View File

@@ -80,3 +80,62 @@ test('empty specifiers field is removed', () => {
},
})
})
test('redundant fields are removed from "time"', () => {
expect(normalizeLockfile({
lockfileVersion: LOCKFILE_VERSION,
packages: {},
importers: {
foo: {
dependencies: {
bar: '1.0.0',
},
devDependencies: {
foo: '1.0.0_react@18.0.0',
},
optionalDependencies: {
qar: '1.0.0',
},
specifiers: {
bar: '1.0.0',
foo: '1.0.0',
qar: '1.0.0',
},
},
},
time: {
'/bar/1.0.0': '2021-02-11T22:54:29.120Z',
'/foo/1.0.0': '2021-02-11T22:54:29.120Z',
'/qar/1.0.0': '2021-02-11T22:54:29.120Z',
'/zoo/1.0.0': '2021-02-11T22:54:29.120Z',
},
}, {
forceSharedFormat: false,
includeEmptySpecifiersField: false,
})).toStrictEqual({
lockfileVersion: LOCKFILE_VERSION,
importers: {
foo: {
dependencies: {
bar: '1.0.0',
},
devDependencies: {
foo: '1.0.0_react@18.0.0',
},
optionalDependencies: {
qar: '1.0.0',
},
specifiers: {
bar: '1.0.0',
foo: '1.0.0',
qar: '1.0.0',
},
},
},
time: {
'/bar/1.0.0': '2021-02-11T22:54:29.120Z',
'/foo/1.0.0': '2021-02-11T22:54:29.120Z',
'/qar/1.0.0': '2021-02-11T22:54:29.120Z',
},
})
})

View File

@@ -12,6 +12,9 @@
{
"path": "../constants"
},
{
"path": "../dependency-path"
},
{
"path": "../error"
},

View File

@@ -223,7 +223,10 @@ export default async function (
lockfileIncludeTarballUrl: opts.lockfileIncludeTarballUrl,
})
if (time) {
newLockfile.time = time
newLockfile.time = {
...opts.wantedLockfile.time,
...time,
}
}
if (opts.forceFullResolution && opts.wantedLockfile != null) {

3
pnpm-lock.yaml generated
View File

@@ -1987,6 +1987,9 @@ importers:
comver-to-semver:
specifier: ^1.0.0
version: 1.0.0
dependency-path:
specifier: workspace:*
version: link:../dependency-path
js-yaml:
specifier: npm:@zkochan/js-yaml@^0.0.6
version: /@zkochan/js-yaml/0.0.6