From 888957a010304c38d41823f3f517ae337a100d7b Mon Sep 17 00:00:00 2001 From: James Henry Date: Fri, 14 Feb 2025 20:08:34 +0400 Subject: [PATCH] fix(release): ensure tags for version match stable variant before prerelease (#30047) --- e2e/release/src/release-tag-pattern.test.ts | 30 +++++++++++++++++++ .../nx/src/command-line/release/utils/git.ts | 9 +++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/e2e/release/src/release-tag-pattern.test.ts b/e2e/release/src/release-tag-pattern.test.ts index dd65e3a639..c40d9560cd 100644 --- a/e2e/release/src/release-tag-pattern.test.ts +++ b/e2e/release/src/release-tag-pattern.test.ts @@ -57,6 +57,36 @@ describe('nx release releaseTagPattern', () => { afterEach(() => cleanupProject()); + it('should prefer stable versions over prereleases', async () => { + updateJson('nx.json', (nxJson) => { + nxJson.release = { + releaseTagPattern: 'v{version}', + version: { + conventionalCommits: true, + }, + }; + return nxJson; + }); + + // Tag the existing commit as a prerelease + await runCommandAsync(`git tag -a v1.0.0-beta.1 -m "v1.0.0-beta.1"`); + + // Resolve that prerelease as the current version + expect(runCLI(`release version -d`)).toContain( + `Resolved the current version as 1.0.0-beta.1 from git tag "v1.0.0-beta.1"` + ); + + // Make a new commit and tag it as a stable version + await runCommandAsync(`echo "Hello" > README.md`); + await runCommandAsync(`git add README.md`); + await runCommandAsync(`git commit -m "chore: update README.md"`); + await runCommandAsync(`git tag -a v1.0.0 -m "v1.0.0"`); + + expect(runCLI(`release version -d`)).toContain( + `Resolved the current version as 1.0.0 from git tag "v1.0.0"` + ); + }); + describe('releaseTagPatternCheckAllBranchesWhen', () => { it('should check the current branch first, and then fall back to all branches by default/when not specified', async () => { updateJson('nx.json', (nxJson) => { diff --git a/packages/nx/src/command-line/release/utils/git.ts b/packages/nx/src/command-line/release/utils/git.ts index 48e9bed733..749de08e6a 100644 --- a/packages/nx/src/command-line/release/utils/git.ts +++ b/packages/nx/src/command-line/release/utils/git.ts @@ -85,7 +85,14 @@ export async function getLatestGitTagForPattern( } } - const defaultGitArgs = ['tag', '--sort', '-v:refname']; + const defaultGitArgs = [ + // Apply git config to take version suffixes into account when sorting, e.g. 1.0.0 is newer than 1.0.0-beta.1 + '-c', + 'versionsort.suffix=-', + 'tag', + '--sort', + '-v:refname', + ]; try { let tags: string[];