Skip to content

Commit

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

Return a copy of the wantedLockfile (wantedLockfileUnmodified) for checking later on if the lockfile was modified.
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).
5 changes: 5 additions & 0 deletions .changeset/nasty-walls-sell.md
@@ -0,0 +1,5 @@
---
"@pnpm/headless": minor
---

A new option is accepted: wantedLockfileUnmodified. It is used to check if the wanted lockfile was changed and whether it needs to be written.
2 changes: 2 additions & 0 deletions pkg-manager/core/src/install/index.ts
Expand Up @@ -385,6 +385,7 @@ export async function mutateModules (
prunedAt: ctx.modulesFile?.prunedAt,
pruneVirtualStore,
wantedLockfile: maybeOpts.ignorePackageManifest ? undefined : ctx.wantedLockfile,
wantedLockfileUnmodified: ctx.wantedLockfileUnmodified,
})
if (opts.useLockfile && opts.saveLockfile && opts.mergeGitBranchLockfiles) {
await writeLockfiles({
Expand Down Expand Up @@ -1257,6 +1258,7 @@ const installInContext: InstallFunction = async (projects, ctx, opts) => {
allProjects: ctx.projects,
prunedAt: ctx.modulesFile?.prunedAt,
wantedLockfile: result.newLockfile,
wantedLockfileUnmodified: ctx.wantedLockfileUnmodified,
})
return result
}
Expand Down
16 changes: 16 additions & 0 deletions pkg-manager/core/test/lockfile.ts
Expand Up @@ -1580,3 +1580,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
wantedLockfileUnmodified: Lockfile
registries: Registries
}

Expand Down Expand Up @@ -360,6 +361,7 @@ export interface PnpmSingleContext {
skipped: Set<string>
storeDir: string
wantedLockfile: Lockfile
wantedLockfileUnmodified: Lockfile
}

export async function getContextForSingleImporter (
Expand Down
2 changes: 2 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
wantedLockfileUnmodified: Lockfile
lockfileHadConflicts: boolean
}> {
const wantedLockfileVersion = LOCKFILE_VERSION_V6
Expand Down Expand Up @@ -128,6 +129,7 @@ export async function readLockfiles (
existsCurrentLockfile: files[1] != null,
existsWantedLockfile: files[0] != null && !isEmptyLockfile(wantedLockfile),
wantedLockfile,
wantedLockfileUnmodified: JSON.parse(JSON.stringify(wantedLockfile)),
lockfileHadConflicts,
}
}
3 changes: 2 additions & 1 deletion pkg-manager/headless/src/index.ts
Expand Up @@ -146,6 +146,7 @@ export interface HeadlessOptions {
pruneStore: boolean
pruneVirtualStore?: boolean
wantedLockfile?: Lockfile
wantedLockfileUnmodified?: Lockfile
ownLifecycleHooksStdio?: 'inherit' | 'pipe'
pendingBuilds: string[]
resolveSymlinksInInjectedDirs?: boolean
Expand Down Expand Up @@ -545,7 +546,7 @@ export async function headlessInstall (opts: HeadlessOptions) {
storeDir: opts.storeDir,
virtualStoreDir,
})
if (opts.useLockfile) {
if (opts.useLockfile && JSON.stringify(wantedLockfile) !== JSON.stringify(opts.wantedLockfileUnmodified)) {
// We need to write the wanted lockfile as well.
// Even though it will only be changed if the workspace will have new projects with no dependencies.
await writeLockfiles({
Expand Down

0 comments on commit 8243836

Please sign in to comment.