Skip to content

Commit

Permalink
fix: don't write pnpm-lock.yaml if it has no changes (#6260)
Browse files Browse the repository at this point in the history
close #6158
  • Loading branch information
zkochan committed Mar 23, 2023
1 parent b8bafe8 commit e6fb46e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-dodos-wash.md
@@ -0,0 +1,5 @@
---
"@pnpm/get-context": minor
---

Return `wantedLockfileIsModified`.
6 changes: 6 additions & 0 deletions .changeset/itchy-dancers-collect.md
@@ -0,0 +1,6 @@
---
"@pnpm/core": patch
"pnpm": patch
---

Don't write the `pnpm-lock.yaml` file if it has no changes and `pnpm install --frozen-lockfile` was executed [#6158](https://github.com/pnpm/pnpm/issues/6158).
2 changes: 2 additions & 0 deletions pkg-manager/core/src/install/index.ts
Expand Up @@ -388,6 +388,7 @@ export async function mutateModules (
prunedAt: ctx.modulesFile?.prunedAt,
pruneVirtualStore,
wantedLockfile: maybeOpts.ignorePackageManifest ? undefined : ctx.wantedLockfile,
useLockfile: opts.useLockfile && ctx.wantedLockfileIsModified,
})
if (opts.useLockfile && opts.saveLockfile && opts.mergeGitBranchLockfiles) {
await writeLockfiles({
Expand Down Expand Up @@ -1266,6 +1267,7 @@ const installInContext: InstallFunction = async (projects, ctx, opts) => {
allProjects: ctx.projects,
prunedAt: ctx.modulesFile?.prunedAt,
wantedLockfile: result.newLockfile,
useLockfile: opts.useLockfile && ctx.wantedLockfileIsModified,
})
return result
}
Expand Down
16 changes: 16 additions & 0 deletions pkg-manager/core/test/lockfile.ts
Expand Up @@ -1559,3 +1559,19 @@ test('update the lockfile when a new project is added to the workspace and lockf
const lockfile: Lockfile = await readYamlFile(WANTED_LOCKFILE)
expect(Object.keys(lockfile.importers)).toStrictEqual(['project-1', 'project-2'])
})

test('lockfile is not written when it has no changes', async () => {
prepareEmpty()

const manifest = await install({
dependencies: {
'@types/semver': '^5.3.31',
},
}, await testDefaults())

const stat = await fs.stat(WANTED_LOCKFILE)
const initialMtime = stat.mtimeMs

await install(manifest, await testDefaults())
expect(await fs.stat(WANTED_LOCKFILE)).toHaveProperty('mtimeMs', initialMtime)
})
2 changes: 2 additions & 0 deletions pkg-manager/get-context/src/index.ts
Expand Up @@ -52,6 +52,7 @@ export interface PnpmContext {
skipped: Set<string>
storeDir: string
wantedLockfile: Lockfile
wantedLockfileIsModified: boolean
registries: Registries
}

Expand Down Expand Up @@ -363,6 +364,7 @@ export interface PnpmSingleContext {
skipped: Set<string>
storeDir: string
wantedLockfile: Lockfile
wantedLockfileIsModified: boolean
}

export async function getContextForSingleImporter (
Expand Down
4 changes: 4 additions & 0 deletions pkg-manager/get-context/src/readLockfiles.ts
Expand Up @@ -46,6 +46,7 @@ export async function readLockfiles (
existsCurrentLockfile: boolean
existsWantedLockfile: boolean
wantedLockfile: Lockfile
wantedLockfileIsModified: boolean
lockfileHadConflicts: boolean
}> {
const wantedLockfileVersion = opts.useLockfileV6 ? LOCKFILE_VERSION_V6 : LOCKFILE_VERSION
Expand Down Expand Up @@ -115,8 +116,10 @@ export async function readLockfiles (
const wantedLockfile = files[0] ??
(currentLockfile && clone(currentLockfile)) ??
createLockfileObject(importerIds, sopts)
let wantedLockfileIsModified = false
for (const importerId of importerIds) {
if (!wantedLockfile.importers[importerId]) {
wantedLockfileIsModified = true
wantedLockfile.importers[importerId] = {
specifiers: {},
}
Expand All @@ -128,6 +131,7 @@ export async function readLockfiles (
existsCurrentLockfile: files[1] != null,
existsWantedLockfile: files[0] != null,
wantedLockfile,
wantedLockfileIsModified,
lockfileHadConflicts,
}
}

0 comments on commit e6fb46e

Please sign in to comment.