From 3474d7c607803409ddf5812cbf5f050efe4bc0fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Correia?= Date: Mon, 9 Dec 2024 13:25:15 +0000 Subject: [PATCH] fix(release): use prepatch version for pre-release dependent package updates (#29123) --- .../release-version/release-version.spec.ts | 64 +++++++++++++++++++ .../release-version/release-version.ts | 25 ++++++-- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/packages/js/src/generators/release-version/release-version.spec.ts b/packages/js/src/generators/release-version/release-version.spec.ts index 7df5c586e98c7..32bb988760402 100644 --- a/packages/js/src/generators/release-version/release-version.spec.ts +++ b/packages/js/src/generators/release-version/release-version.spec.ts @@ -696,6 +696,70 @@ To fix this you will either need to add a package.json file at that location, or } `); }); + + it('should update dependents with a prepatch when creating a pre-release version', async () => { + expect(readJson(tree, 'libs/my-lib/package.json').version).toEqual( + '0.0.1' + ); + expect( + readJson( + tree, + 'libs/project-with-dependency-on-my-pkg/package.json' + ).version + ).toEqual('0.0.1'); + expect( + readJson( + tree, + 'libs/project-with-devDependency-on-my-pkg/package.json' + ).version + ).toEqual('0.0.1'); + + await releaseVersionGenerator(tree, { + projects: Object.values(projectGraph.nodes), // version all projects + projectGraph, + currentVersionResolver: 'disk', + specifier: 'prepatch', + preid: 'alpha', + releaseGroup: createReleaseGroup('independent'), + }); + + expect(readJson(tree, 'libs/my-lib/package.json')) + .toMatchInlineSnapshot(` + { + "name": "my-lib", + "version": "0.0.2-alpha.0", + } + `); + + expect( + readJson( + tree, + 'libs/project-with-dependency-on-my-pkg/package.json' + ) + ).toMatchInlineSnapshot(` + { + "dependencies": { + "my-lib": "0.0.2-alpha.0", + }, + "name": "project-with-dependency-on-my-pkg", + "version": "0.0.2-alpha.0", + } + `); + expect( + readJson( + tree, + 'libs/project-with-devDependency-on-my-pkg/package.json' + ) + ).toMatchInlineSnapshot(` + { + "devDependencies": { + "my-lib": "0.0.2-alpha.0", + }, + "name": "project-with-devDependency-on-my-pkg", + "version": "0.0.2-alpha.0", + } + `); + }); }); }); }); diff --git a/packages/js/src/generators/release-version/release-version.ts b/packages/js/src/generators/release-version/release-version.ts index 82343018a8b90..ce30a59da2fb5 100644 --- a/packages/js/src/generators/release-version/release-version.ts +++ b/packages/js/src/generators/release-version/release-version.ts @@ -47,6 +47,14 @@ import { import { sortProjectsTopologically } from './utils/sort-projects-topologically'; import { updateLockFile } from './utils/update-lock-file'; +function resolvePreIdSpecifier(currentSpecifier: string, preid?: string) { + if (!currentSpecifier.startsWith('pre') && preid) { + return `pre${currentSpecifier}`; + } + + return currentSpecifier; +} + export async function releaseVersionGenerator( tree: Tree, options: ReleaseVersionGeneratorSchema @@ -89,7 +97,10 @@ Valid values are: ${validReleaseVersionPrefixes // Set default for updateDependents const updateDependents = options.updateDependents ?? 'auto'; - const updateDependentsBump = 'patch'; + const updateDependentsBump = resolvePreIdSpecifier( + 'patch', + options.preid + ) as ReleaseType; // Sort the projects topologically if update dependents is enabled // TODO: maybe move this sorting to the command level? @@ -445,8 +456,13 @@ To fix this you will either need to add a package.json file at that location, or ); } else { let extraText = ''; - if (options.preid && !specifier.startsWith('pre')) { - specifier = `pre${specifier}`; + const prereleaseSpecifier = resolvePreIdSpecifier( + specifier, + options.preid + ); + + if (prereleaseSpecifier !== specifier) { + specifier = prereleaseSpecifier; extraText = `, combined with your given preid "${options.preid}"`; } logger.buffer( @@ -598,6 +614,7 @@ To fix this you will either need to add a package.json file at that location, or options.releaseGroup.projectsRelationship === 'independent' ); + // list of projects that depend on the current package const allDependentProjects = Object.values(localPackageDependencies) .flat() .filter((localPackageDependency) => { @@ -765,7 +782,7 @@ To fix this you will either need to add a package.json file at that location, or dependentProject: LocalPackageDependency; dependencyPackageName: string; newDependencyVersion: string; - forceVersionBump: 'major' | 'minor' | 'patch' | false; + forceVersionBump: ReleaseType | false; }) => { const updatedFilePath = joinPathFragments( projectNameToPackageRootMap.get(dependentProject.source),