diff --git a/.changeset/tidy-rivers-marry.md b/.changeset/tidy-rivers-marry.md new file mode 100644 index 0000000000..547ef49ed7 --- /dev/null +++ b/.changeset/tidy-rivers-marry.md @@ -0,0 +1,5 @@ +--- +"@pnpm/core": patch +--- + +fix mergeGitBranchLockfiles when merged lockfile is up-to-date diff --git a/packages/core/src/install/index.ts b/packages/core/src/install/index.ts index 7931da2c88..bca9dbd2fc 100644 --- a/packages/core/src/install/index.ts +++ b/packages/core/src/install/index.ts @@ -330,6 +330,18 @@ export async function mutateModules ( pruneVirtualStore, wantedLockfile: maybeOpts.ignorePackageManifest ? undefined : ctx.wantedLockfile, }) + if (opts.useLockfile && opts.saveLockfile && opts.mergeGitBranchLockfiles) { + await writeLockfiles({ + currentLockfile: ctx.currentLockfile, + currentLockfileDir: ctx.virtualStoreDir, + wantedLockfile: ctx.wantedLockfile, + wantedLockfileDir: ctx.lockfileDir, + forceSharedFormat: opts.forceSharedLockfile, + useInlineSpecifiersFormat: opts.useInlineSpecifiersLockfileFormat, + useGitBranchLockfile: opts.useGitBranchLockfile, + mergeGitBranchLockfiles: opts.mergeGitBranchLockfiles, + }) + } return projects } catch (error: any) { // eslint-disable-line if ( diff --git a/packages/core/test/install/gitBranchLockfile.test.ts b/packages/core/test/install/gitBranchLockfile.test.ts index ba546f29f8..352488e1d5 100644 --- a/packages/core/test/install/gitBranchLockfile.test.ts +++ b/packages/core/test/install/gitBranchLockfile.test.ts @@ -3,7 +3,7 @@ import path from 'path' import { prepareEmpty, preparePackages } from '@pnpm/prepare' import { install, mutateModules } from '@pnpm/core' import { testDefaults } from '../utils' -import { WANTED_LOCKFILE } from '@pnpm/constants' +import { LOCKFILE_VERSION, WANTED_LOCKFILE } from '@pnpm/constants' import { ProjectManifest } from '@pnpm/types' import { getCurrentBranch } from '@pnpm/git-utils' import writeYamlFile from 'write-yaml-file' @@ -144,3 +144,75 @@ test('install with --merge-git-branch-lockfiles', async () => { expect(fs.existsSync(otherLockfilePath)).toBe(false) expect(fs.existsSync(WANTED_LOCKFILE)).toBe(true) }) + +test('install with --merge-git-branch-lockfiles when merged lockfile is up to date', async () => { + const project = prepareEmpty() + + // @types/semver installed in the main branch + await writeYamlFile(WANTED_LOCKFILE, { + dependencies: { + '@types/semver': '5.3.31', + }, + lockfileVersion: LOCKFILE_VERSION, + packages: { + '/@types/semver/5.3.31': { + resolution: { + integrity: 'sha512-WBv5F9HrWTyG800cB9M3veCVkFahqXN7KA7c3VUCYZm/xhNzzIFiXiq+rZmj75j7GvWelN3YNrLX7FjtqBvhMw==', + }, + }, + }, + specifiers: { + '@types/semver': '5.3.31', + }, + }, { lineWidth: 1000 }) + + const branchName: string = 'main-branch' + getCurrentBranch['mockReturnValue'](branchName) + + // is-positive installed in the other branch + const otherLockfilePath: string = path.resolve('pnpm-lock.other.yaml') + const otherLockfileContent = { + dependencies: { + '@types/semver': '5.3.31', + 'is-positive': '3.1.0', + }, + lockfileVersion: LOCKFILE_VERSION, + packages: { + '/@types/semver/5.3.31': { + resolution: { + integrity: 'sha512-WBv5F9HrWTyG800cB9M3veCVkFahqXN7KA7c3VUCYZm/xhNzzIFiXiq+rZmj75j7GvWelN3YNrLX7FjtqBvhMw==', + }, + }, + '/is-positive/3.1.0': { + resolution: { + integrity: 'sha512-8ND1j3y9/HP94TOvGzr69/FgbkX2ruOldhLEsTWwcJVfo4oRjwemJmJxt7RJkKYH8tz7vYBP9JcKQY8CLuJ90Q==', + }, + }, + }, + specifiers: { + '@types/semver': '5.3.31', + 'is-positive': '^3.1.0', + }, + } + await writeYamlFile(otherLockfilePath, otherLockfileContent, { lineWidth: 1000 }) + + // the other branch merged to the main branch + const projectManifest: ProjectManifest = { + dependencies: { + '@types/semver': '5.3.31', + 'is-positive': '^3.1.0', + }, + } + const opts = await testDefaults({ + useGitBranchLockfile: true, + mergeGitBranchLockfiles: true, + frozenLockfile: true, + }) + await install(projectManifest, opts) + + expect(fs.existsSync(otherLockfilePath)).toBe(false) + expect(fs.existsSync(WANTED_LOCKFILE)).toBe(true) + + const wantedLockfileAfterMergeOther = await project.readLockfile() + expect(wantedLockfileAfterMergeOther).toEqual(otherLockfileContent) +})