diff --git a/.changeset/early-dodos-wash.md b/.changeset/early-dodos-wash.md new file mode 100644 index 00000000000..f37fbfff6f1 --- /dev/null +++ b/.changeset/early-dodos-wash.md @@ -0,0 +1,5 @@ +--- +"@pnpm/get-context": minor +--- + +Return `wantedLockfileIsModified`. diff --git a/.changeset/itchy-dancers-collect.md b/.changeset/itchy-dancers-collect.md new file mode 100644 index 00000000000..2074f812016 --- /dev/null +++ b/.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). diff --git a/pkg-manager/core/src/install/index.ts b/pkg-manager/core/src/install/index.ts index 2f72c11da1e..ed4162cb82f 100644 --- a/pkg-manager/core/src/install/index.ts +++ b/pkg-manager/core/src/install/index.ts @@ -385,6 +385,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({ @@ -1257,6 +1258,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 } diff --git a/pkg-manager/core/test/lockfile.ts b/pkg-manager/core/test/lockfile.ts index d8ce27a903b..025aab42cb4 100644 --- a/pkg-manager/core/test/lockfile.ts +++ b/pkg-manager/core/test/lockfile.ts @@ -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) +}) diff --git a/pkg-manager/get-context/src/index.ts b/pkg-manager/get-context/src/index.ts index 1ee36a9fd7a..1f8bb7ea68b 100644 --- a/pkg-manager/get-context/src/index.ts +++ b/pkg-manager/get-context/src/index.ts @@ -52,6 +52,7 @@ export interface PnpmContext { skipped: Set storeDir: string wantedLockfile: Lockfile + wantedLockfileIsModified: boolean registries: Registries } @@ -360,6 +361,7 @@ export interface PnpmSingleContext { skipped: Set storeDir: string wantedLockfile: Lockfile + wantedLockfileIsModified: boolean } export async function getContextForSingleImporter ( diff --git a/pkg-manager/get-context/src/readLockfiles.ts b/pkg-manager/get-context/src/readLockfiles.ts index f2696674448..d6ebe98de1f 100644 --- a/pkg-manager/get-context/src/readLockfiles.ts +++ b/pkg-manager/get-context/src/readLockfiles.ts @@ -46,6 +46,7 @@ export async function readLockfiles ( existsCurrentLockfile: boolean existsWantedLockfile: boolean wantedLockfile: Lockfile + wantedLockfileIsModified: boolean lockfileHadConflicts: boolean }> { const wantedLockfileVersion = LOCKFILE_VERSION_V6 @@ -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: {}, } @@ -128,6 +131,7 @@ export async function readLockfiles ( existsCurrentLockfile: files[1] != null, existsWantedLockfile: files[0] != null && !isEmptyLockfile(wantedLockfile), wantedLockfile, + wantedLockfileIsModified, lockfileHadConflicts, } }