Skip to content

Commit

Permalink
fix(release): disable workspace changelogs in config when not valid (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fahslaj committed Jan 26, 2024
1 parent c2e0c3d commit 9c81328
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 122 deletions.
6 changes: 3 additions & 3 deletions e2e/release/src/first-release.test.ts
Expand Up @@ -88,7 +88,7 @@ describe('nx release first run', () => {
expect(
releaseOutput1.match(
new RegExp(
`NX Unable to determine the previous git tag. If this is the first release of your workspace, use the --first-release option or set the "changelog.automaticFromRef" config property in nx.json to generate a changelog from the first commit. Otherwise, be sure to configure the "releaseTagPattern" property in nx.json to match the structure of your repository's git tags.`,
`NX Unable to determine the previous git tag. If this is the first release of your workspace, use the --first-release option or set the "release.changelog.automaticFromRef" config property in nx.json to generate a changelog from the first commit. Otherwise, be sure to configure the "release.releaseTagPattern" property in nx.json to match the structure of your repository's git tags.`,
'g'
)
).length
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('nx release first run', () => {
expect(
releaseOutput1.match(
new RegExp(
`NX Unable to determine the previous git tag. If this is the first release of your workspace, use the --first-release option or set the "changelog.automaticFromRef" config property in nx.json to generate a changelog from the first commit. Otherwise, be sure to configure the "releaseTagPattern" property in nx.json to match the structure of your repository's git tags.`,
`NX Unable to determine the previous git tag. If this is the first release of your workspace, use the --first-release option or set the "release.changelog.automaticFromRef" config property in nx.json to generate a changelog from the first commit. Otherwise, be sure to configure the "release.releaseTagPattern" property in nx.json to match the structure of your repository's git tags.`,
'g'
)
).length
Expand Down Expand Up @@ -322,7 +322,7 @@ describe('nx release first run', () => {
expect(
releaseOutput3.match(
new RegExp(
`NX Unable to determine the previous git tag. If this is the first release of your workspace, use the --first-release option or set the "changelog.automaticFromRef" config property in nx.json to generate a changelog from the first commit. Otherwise, be sure to configure the "releaseTagPattern" property in nx.json to match the structure of your repository's git tags.`,
`NX Unable to determine the previous git tag. If this is the first release of your workspace, use the --first-release option or set the "release.changelog.automaticFromRef" config property in nx.json to generate a changelog from the first commit. Otherwise, be sure to configure the "release.releaseTagPattern" property in nx.json to match the structure of your repository's git tags.`,
'g'
)
).length
Expand Down
13 changes: 9 additions & 4 deletions e2e/release/src/independent-projects.test.ts
Expand Up @@ -11,6 +11,7 @@ import {
uniq,
updateJson,
} from '@nx/e2e/utils';
import { execSync } from 'child_process';

expect.addSnapshotSerializer({
serialize(str: string) {
Expand Down Expand Up @@ -60,6 +61,7 @@ describe('nx release - independent projects', () => {
let pkg1: string;
let pkg2: string;
let pkg3: string;
let e2eRegistryUrl: string;

beforeAll(() => {
newProject({
Expand Down Expand Up @@ -92,6 +94,9 @@ describe('nx release - independent projects', () => {
runCommand(`git tag ${pkg1}@0.0.0`);
runCommand(`git tag ${pkg2}@0.0.0`);
runCommand(`git tag ${pkg3}@0.0.0`);

// This is the verdaccio instance that the e2e tests themselves are working from
e2eRegistryUrl = execSync('npm config get registry').toString().trim();
});
afterAll(() => cleanupProject());

Expand Down Expand Up @@ -671,7 +676,7 @@ describe('nx release - independent projects', () => {
integrity: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
total files: 4
Would publish to http://localhost:4873 with tag "latest", but [dry-run] was set
Would publish to ${e2eRegistryUrl} with tag "latest", but [dry-run] was set
Expand Down Expand Up @@ -720,7 +725,7 @@ describe('nx release - independent projects', () => {
integrity: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
total files: 4
Would publish to http://localhost:4873 with tag "latest", but [dry-run] was set
Would publish to ${e2eRegistryUrl} with tag "latest", but [dry-run] was set
Expand Down Expand Up @@ -802,7 +807,7 @@ describe('nx release - independent projects', () => {
integrity: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
total files: 4
Would publish to http://localhost:4873 with tag "latest", but [dry-run] was set
Would publish to ${e2eRegistryUrl} with tag "latest", but [dry-run] was set
> nx run {project-name}:nx-release-publish
Expand All @@ -824,7 +829,7 @@ describe('nx release - independent projects', () => {
integrity: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
total files: 4
Would publish to http://localhost:4873 with tag "latest", but [dry-run] was set
Would publish to ${e2eRegistryUrl} with tag "latest", but [dry-run] was set
Expand Down
6 changes: 3 additions & 3 deletions e2e/release/src/nx-release-git-operations.test.ts
Expand Up @@ -131,7 +131,7 @@ describe('nx release git operations', () => {
expect(
releaseOutput1.match(
new RegExp(
`The 'release' top level command cannot be used with granular git configuration. Instead, configure git options in the 'release.git' property in nx.json, or use the version, changelog, and publish subcommands or programmatic API directly.`,
`The "release" top level command cannot be used with granular git configuration. Instead, configure git options in the "release.git" property in nx.json, or use the version, changelog, and publish subcommands or programmatic API directly.`,
'g'
)
).length
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('nx release git operations', () => {
expect(
releaseVersionOutput.match(
new RegExp(
`The 'release.git' property in nx.json may not be used with the 'nx release version' subcommand or programmatic API. Instead, configure git options for subcommands directly with 'release.version.git' and 'release.changelog.git'.`,
`The "release.git" property in nx.json may not be used with the "nx release version" subcommand or programmatic API. Instead, configure git options for subcommands directly with "release.version.git" and "release.changelog.git".`,
'g'
)
).length
Expand All @@ -177,7 +177,7 @@ describe('nx release git operations', () => {
expect(
releaseChangelogOutput.match(
new RegExp(
`The 'release.git' property in nx.json may not be used with the 'nx release changelog' subcommand or programmatic API. Instead, configure git options for subcommands directly with 'release.version.git' and 'release.changelog.git'.`,
`The "release.git" property in nx.json may not be used with the "nx release changelog" subcommand or programmatic API. Instead, configure git options for subcommands directly with "release.version.git" and "release.changelog.git".`,
'g'
)
).length
Expand Down
4 changes: 2 additions & 2 deletions e2e/release/src/release.test.ts
Expand Up @@ -1089,7 +1089,7 @@ ${JSON.stringify(
{project-name} 🔍 Reading data for package "@proj/{project-name}" from {project-name}/package.json
> NX No git tags matching pattern ">{version}" for project "{project-name}" were found. You will need to create an initial matching tag to use as a base for determining the next version. Alternatively, you can use the --first-release option or set "version.generatorOptions.fallbackCurrentVersionResolver" to "disk" in order to fallback to the version on disk when no matching git tags are found.
> NX No git tags matching pattern ">{version}" for project "{project-name}" were found. You will need to create an initial matching tag to use as a base for determining the next version. Alternatively, you can use the --first-release option or set "release.version.generatorOptions.fallbackCurrentVersionResolver" to "disk" in order to fallback to the version on disk when no matching git tags are found.
`);
Expand Down Expand Up @@ -1151,7 +1151,7 @@ ${JSON.stringify(
expect(
releaseOutput6a.match(
new RegExp(
`NX Unable to resolve the current version from the registry ${e2eRegistryUrl}. Please ensure that the package exists in the registry in order to use the "registry" currentVersionResolver. Alternatively, you can use the --first-release option or set "version.generatorOptions.fallbackCurrentVersionResolver" to "disk" in order to fallback to the version on disk when the registry lookup fails.`,
`NX Unable to resolve the current version from the registry ${e2eRegistryUrl}. Please ensure that the package exists in the registry in order to use the "registry" currentVersionResolver. Alternatively, you can use the --first-release option or set "release.version.generatorOptions.fallbackCurrentVersionResolver" to "disk" in order to fallback to the version on disk when the registry lookup fails.`,
'g'
)
).length
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/generators/release-version/release-version.ts
Expand Up @@ -207,7 +207,7 @@ To fix this you will either need to add a package.json file at that location, or
currentVersionResolvedFromFallback = true;
} else {
throw new Error(
`Unable to resolve the current version from the registry ${registry}. Please ensure that the package exists in the registry in order to use the "registry" currentVersionResolver. Alternatively, you can use the --first-release option or set "version.generatorOptions.fallbackCurrentVersionResolver" to "disk" in order to fallback to the version on disk when the registry lookup fails.`
`Unable to resolve the current version from the registry ${registry}. Please ensure that the package exists in the registry in order to use the "registry" currentVersionResolver. Alternatively, you can use the --first-release option or set "release.version.generatorOptions.fallbackCurrentVersionResolver" to "disk" in order to fallback to the version on disk when the registry lookup fails.`
);
}
}
Expand Down Expand Up @@ -252,7 +252,7 @@ To fix this you will either need to add a package.json file at that location, or
currentVersionResolvedFromFallback = true;
} else {
throw new Error(
`No git tags matching pattern "${releaseTagPattern}" for project "${project.name}" were found. You will need to create an initial matching tag to use as a base for determining the next version. Alternatively, you can use the --first-release option or set "version.generatorOptions.fallbackCurrentVersionResolver" to "disk" in order to fallback to the version on disk when no matching git tags are found.`
`No git tags matching pattern "${releaseTagPattern}" for project "${project.name}" were found. You will need to create an initial matching tag to use as a base for determining the next version. Alternatively, you can use the --first-release option or set "release.version.generatorOptions.fallbackCurrentVersionResolver" to "disk" in order to fallback to the version on disk when no matching git tags are found.`
);
}
} else {
Expand Down
67 changes: 36 additions & 31 deletions packages/nx/src/command-line/release/changelog.ts
Expand Up @@ -104,7 +104,7 @@ export async function releaseChangelog(
'git',
]);
output.error({
title: `The 'release.git' property in nx.json may not be used with the 'nx release changelog' subcommand or programmatic API. Instead, configure git options for subcommands directly with 'release.version.git' and 'release.changelog.git'.`,
title: `The "release.git" property in nx.json may not be used with the "nx release changelog" subcommand or programmatic API. Instead, configure git options for subcommands directly with "release.version.git" and "release.changelog.git".`,
bodyLines: [nxJsonMessage],
});
process.exit(1);
Expand All @@ -125,6 +125,19 @@ export async function releaseChangelog(
process.exit(1);
}

const changelogGenerationEnabled =
!!nxReleaseConfig.changelog.workspaceChangelog ||
Object.values(nxReleaseConfig.groups).some((g) => g.changelog);
if (!changelogGenerationEnabled) {
output.warn({
title: `Changelogs are disabled. No changelog entries will be generated`,
bodyLines: [
`To explicitly enable changelog generation, configure "release.changelog.workspaceChangelog" or "release.changelog.projectChangelogs" in nx.json.`,
],
});
return 0;
}

const useAutomaticFromRef =
nxReleaseConfig.changelog?.automaticFromRef || args.firstRelease;

Expand Down Expand Up @@ -203,7 +216,7 @@ export async function releaseChangelog(
}
} else {
throw new Error(
`Unable to determine the previous git tag. If this is the first release of your workspace, use the --first-release option or set the "changelog.automaticFromRef" config property in nx.json to generate a changelog from the first commit. Otherwise, be sure to configure the "releaseTagPattern" property in nx.json to match the structure of your repository's git tags.`
`Unable to determine the previous git tag. If this is the first release of your workspace, use the --first-release option or set the "release.changelog.automaticFromRef" config property in nx.json to generate a changelog from the first commit. Otherwise, be sure to configure the "release.releaseTagPattern" property in nx.json to match the structure of your repository's git tags.`
);
}
}
Expand All @@ -225,8 +238,7 @@ export async function releaseChangelog(
nxReleaseConfig,
workspaceChangelogVersion,
workspaceChangelogCommits,
postGitTasks,
nxJson.release?.changelog?.workspaceChangelog
postGitTasks
);

for (const releaseGroup of releaseGroups) {
Expand Down Expand Up @@ -273,7 +285,7 @@ export async function releaseChangelog(

if (!fromRef && !commits) {
throw new Error(
`Unable to determine the previous git tag. If this is the first release of your workspace, use the --first-release option or set the "changelog.automaticFromRef" config property in nx.json to generate a changelog from the first commit. Otherwise, be sure to configure the "releaseTagPattern" property in nx.json to match the structure of your repository's git tags.`
`Unable to determine the previous git tag. If this is the first release of your workspace, use the --first-release option or set the "release.changelog.automaticFromRef" config property in nx.json to generate a changelog from the first commit. Otherwise, be sure to configure the "release.releaseTagPattern" property in nx.json to match the structure of your repository's git tags.`
);
}

Expand Down Expand Up @@ -500,13 +512,11 @@ async function generateChangelogForWorkspace(
nxReleaseConfig: NxReleaseConfig,
workspaceChangelogVersion: (string | null) | undefined,
commits: GitCommit[],
postGitTasks: PostGitTask[],
explicitWorkspaceChangelogConfig: unknown
postGitTasks: PostGitTask[]
) {
const config = nxReleaseConfig.changelog.workspaceChangelog;
const isEnabled = args.workspaceChangelog ?? config;
// The entire feature is disabled at the workspace level, exit early
if (isEnabled === false) {
if (config === false) {
return;
}

Expand All @@ -522,33 +532,28 @@ async function generateChangelogForWorkspace(
);
}

if (!workspaceChangelogVersion && args.workspaceChangelog) {
throw new Error(
`Workspace changelog is enabled but no overall version was provided. Please provide an explicit version using --version`
);
if (Object.entries(nxReleaseConfig.groups).length > 1) {
output.warn({
title: `Workspace changelog is enabled, but you have multiple release groups configured. This is not supported, so workspace changelog will be disabled.`,
bodyLines: [
`A single workspace version cannot be determined when defining multiple release groups because versions differ between each group.`,
`Project level changelogs can be enabled with the "release.changelog.projectChangelogs" property.`,
],
});
return;
}

if (
Object.entries(nxReleaseConfig.groups).length > 1 ||
Object.values(nxReleaseConfig.groups)[0].projectsRelationship ===
'independent'
'independent'
) {
if (
explicitWorkspaceChangelogConfig !== undefined &&
explicitWorkspaceChangelogConfig !== false
) {
// only warn the user if they explicitly enabled workspace changelog
// if they didn't, then just disable it quietly, since it was enabled by default
output.warn({
title: `Workspace changelog is enabled, but you have multiple release groups configured or have configured an independent projects relationship. This is not supported, so workspace changelog will be disabled.`,
bodyLines: [
`A single workspace version cannot be determined when defining multiple release groups because versions differ between each group.`,
`Also, a single workspace version also cannot be determined when using independent projects because versions differ between each project.`,
`If you want to generate a workspace changelog, please use a single release group.`,
`Alternatively, project level changelogs can be enabled with the "projectChangelogs" property.`,
],
});
}
output.warn({
title: `Workspace changelog is enabled, but you have configured an independent projects relationship. This is not supported, so workspace changelog will be disabled.`,
bodyLines: [
`A single workspace version cannot be determined when using independent projects because versions differ between each project.`,
`Project level changelogs can be enabled with the "release.changelog.projectChangelogs" property.`,
],
});
return;
}

Expand Down
1 change: 0 additions & 1 deletion packages/nx/src/command-line/release/command-object.ts
Expand Up @@ -44,7 +44,6 @@ export type ChangelogOptions = NxReleaseArgs &
from?: string;
interactive?: string;
gitRemote?: string;
workspaceChangelog?: boolean;
};

export type PublishOptions = NxReleaseArgs &
Expand Down

0 comments on commit 9c81328

Please sign in to comment.