Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(release): only add nx-release-publish to public packages #21338

Merged
merged 8 commits into from
Jan 26, 2024
53 changes: 41 additions & 12 deletions e2e/release/src/independent-projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ describe('nx release - independent projects', () => {

pkg3 = uniq('my-pkg-3');
runCLI(`generate @nx/workspace:npm-package ${pkg3}`);
updateJson(`${pkg3}/package.json`, (json) => {
json.private = true;
return json;
});

/**
* Update pkg2 to depend on pkg3.
Expand Down Expand Up @@ -205,9 +201,6 @@ describe('nx release - independent projects', () => {
+ "version": "999.9.9-package.3",
"scripts": {

}
+


"dependencies": {
- "@proj/{project-name}": "0.0.0"
Expand Down Expand Up @@ -424,7 +417,7 @@ describe('nx release - independent projects', () => {
release: {
projectsRelationship: 'independent',
changelog: {
projectChangelogs: {}, // enable project changelogs with default options
projectChangelogs: true, // enable project changelogs with default options
workspaceChangelog: false, // disable workspace changelog
},
},
Expand Down Expand Up @@ -746,7 +739,25 @@ describe('nx release - independent projects', () => {

> nx run {project-name}:nx-release-publish

Skipped package "@proj/{project-name}" from project "{project-name}", because it has \`"private": true\` in {project-name}/package.json

📦 @proj/{project-name}@999.9.9-version-git-operations-test.3
=== Tarball Contents ===

XXXB CHANGELOG.md
XXB index.js
XXXB package.json
XXB project.json
=== Tarball Details ===
name: @proj/{project-name}
version: 999.9.9-version-git-operations-test.3
filename: proj-{project-name}-999.9.9-version-git-operations-test.3.tgz
package size: XXXB
unpacked size: XXXB
shasum: {SHASUM}
integrity: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
total files: 4

Would publish to http://localhost:4873 with tag "latest", but [dry-run] was set



Expand Down Expand Up @@ -837,7 +848,25 @@ describe('nx release - independent projects', () => {

> nx run {project-name}:nx-release-publish

Skipped package "@proj/{project-name}" from project "{project-name}", because it has \`"private": true\` in {project-name}/package.json

📦 @proj/{project-name}@999.9.9-version-git-operations-test.3
=== Tarball Contents ===

XXXB CHANGELOG.md
XXB index.js
XXXB package.json
XXB project.json
=== Tarball Details ===
name: @proj/{project-name}
version: 999.9.9-version-git-operations-test.3
filename: proj-{project-name}-999.9.9-version-git-operations-test.3.tgz
package size: XXXB
unpacked size: XXXB
shasum: {SHASUM}
integrity: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
total files: 4

Would publish to http://localhost:4873 with tag "latest", but [dry-run] was set



Expand All @@ -862,7 +891,7 @@ describe('nx release - independent projects', () => {
},
},
changelog: {
projectChangelogs: {},
projectChangelogs: true,
},
},
};
Expand Down Expand Up @@ -920,7 +949,7 @@ describe('nx release - independent projects', () => {
},
},
changelog: {
projectChangelogs: {},
projectChangelogs: true,
},
},
};
Expand Down
17 changes: 5 additions & 12 deletions e2e/release/src/private-js-packages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,28 +188,21 @@ describe('nx release - private JS packages', () => {

`);

const privatePkgPublishOutput = runCLI(`release publish -p ${privatePkg}`);
const privatePkgPublishOutput = runCLI(`release publish -p ${privatePkg}`, {
silenceError: true,
});
expect(privatePkgPublishOutput).toMatchInlineSnapshot(`

> NX Your filter "{private-project-name}" matched the following projects:

- {private-project-name}


> NX Running target nx-release-publish for project {private-project-name}:
> NX Based on your config, the following projects were matched for publishing but do not have the "nx-release-publish" target specified:

- {private-project-name}



> nx run {private-project-name}:nx-release-publish

Skipped package "@proj/{private-project-name}" from project "{private-project-name}", because it has \`"private": true\` in {private-project-name}/package.json



> NX Successfully ran target nx-release-publish for project {private-project-name}

There are a few possible reasons for this: (1) The projects may be private (2) You may not have an appropriate plugin (such as \`@nx/js\`) installed which adds the target automatically to public projects (3) You intended to configure the target manually, or exclude those projects via config in nx.json


`);
Expand Down
148 changes: 0 additions & 148 deletions packages/nx/src/command-line/release/config/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1967,80 +1967,6 @@ describe('createNxReleaseConfig()', () => {
}
`);
});

it('should return an error if any matched projects do not have the required target specified', async () => {
const res = await createNxReleaseConfig(
{
...projectGraph,
nodes: {
...projectGraph.nodes,
'project-without-target': {
name: 'project-without-target',
type: 'lib',
data: {
root: 'libs/project-without-target',
targets: {},
} as any,
},
},
},
{
groups: {
'group-1': {
projects: '*', // using string form to ensure that is supported in addition to array form
},
},
},
'nx-release-publish'
);
expect(res).toMatchInlineSnapshot(`
{
"error": {
"code": "PROJECTS_MISSING_TARGET",
"data": {
"projects": [
"project-without-target",
],
"targetName": "nx-release-publish",
},
},
"nxReleaseConfig": null,
}
`);

const res2 = await createNxReleaseConfig(
{
...projectGraph,
nodes: {
...projectGraph.nodes,
'another-project-without-target': {
name: 'another-project-without-target',
type: 'lib',
data: {
root: 'libs/another-project-without-target',
targets: {},
} as any,
},
},
},
{},
'nx-release-publish'
);
expect(res2).toMatchInlineSnapshot(`
{
"error": {
"code": "PROJECTS_MISSING_TARGET",
"data": {
"projects": [
"another-project-without-target",
],
"targetName": "nx-release-publish",
},
},
"nxReleaseConfig": null,
}
`);
});
});

describe('user config -> mixed top level and granular git', () => {
Expand Down Expand Up @@ -2172,80 +2098,6 @@ describe('createNxReleaseConfig()', () => {
`);
});

it('should return an error if any matched projects do not have the required target specified', async () => {
const res = await createNxReleaseConfig(
{
...projectGraph,
nodes: {
...projectGraph.nodes,
'project-without-target': {
name: 'project-without-target',
type: 'lib',
data: {
root: 'libs/project-without-target',
targets: {},
} as any,
},
},
},
{
groups: {
'group-1': {
projects: '*', // using string form to ensure that is supported in addition to array form
},
},
},
'nx-release-publish'
);
expect(res).toMatchInlineSnapshot(`
{
"error": {
"code": "PROJECTS_MISSING_TARGET",
"data": {
"projects": [
"project-without-target",
],
"targetName": "nx-release-publish",
},
},
"nxReleaseConfig": null,
}
`);

const res2 = await createNxReleaseConfig(
{
...projectGraph,
nodes: {
...projectGraph.nodes,
'another-project-without-target': {
name: 'another-project-without-target',
type: 'lib',
data: {
root: 'libs/another-project-without-target',
targets: {},
} as any,
},
},
},
{},
'nx-release-publish'
);
expect(res2).toMatchInlineSnapshot(`
{
"error": {
"code": "PROJECTS_MISSING_TARGET",
"data": {
"projects": [
"another-project-without-target",
],
"targetName": "nx-release-publish",
},
},
"nxReleaseConfig": null,
}
`);
});

it("should return an error if a group's releaseTagPattern has no {version} placeholder", async () => {
const res = await createNxReleaseConfig(projectGraph, {
groups: {
Expand Down
50 changes: 1 addition & 49 deletions packages/nx/src/command-line/release/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import { NxJsonConfiguration } from '../../../config/nx-json';
import { output, type ProjectGraph } from '../../../devkit-exports';
import { findMatchingProjects } from '../../../utils/find-matching-projects';
import { projectHasTarget } from '../../../utils/project-graph-utils';
import { resolveNxJsonConfigErrorMessage } from '../utils/resolve-nx-json-error-message';

type DeepRequired<T> = Required<{
Expand Down Expand Up @@ -73,7 +72,6 @@ export interface CreateNxReleaseConfigError {
| 'RELEASE_GROUP_MATCHES_NO_PROJECTS'
| 'RELEASE_GROUP_RELEASE_TAG_PATTERN_VERSION_PLACEHOLDER_MISSING_OR_EXCESSIVE'
| 'PROJECT_MATCHES_MULTIPLE_GROUPS'
| 'PROJECTS_MISSING_TARGET'
| 'CONVENTIONAL_COMMITS_SHORTHAND_MIXED_WITH_OVERLAPPING_GENERATOR_OPTIONS'
| 'GLOBAL_GIT_CONFIG_MIXED_WITH_GRANULAR_GIT_CONFIG';
data: Record<string, string | string[]>;
Expand All @@ -82,9 +80,7 @@ export interface CreateNxReleaseConfigError {
// Apply default configuration to any optional user configuration and handle known errors
export async function createNxReleaseConfig(
projectGraph: ProjectGraph,
userConfig: NxJsonConfiguration['release'] = {},
// Optionally ensure that all configured projects have implemented a certain target
requiredTargetName?: 'nx-release-publish'
userConfig: NxJsonConfiguration['release'] = {}
): Promise<{
error: null | CreateNxReleaseConfigError;
nxReleaseConfig: NxReleaseConfig | null;
Expand Down Expand Up @@ -353,21 +349,6 @@ export async function createNxReleaseConfig(
};
}

// Ensure all matching projects have the relevant target available, if applicable
if (requiredTargetName) {
const error = ensureProjectsHaveTarget(
matchingProjects,
projectGraph,
requiredTargetName
);
if (error) {
return {
error,
nxReleaseConfig: null,
};
}
}

// If provided, ensure release tag pattern is valid
if (releaseGroup.releaseTagPattern) {
const error = ensureReleaseGroupReleaseTagPatternIsValid(
Expand Down Expand Up @@ -526,14 +507,6 @@ export async function handleNxReleaseConfigError(
});
}
break;
case 'PROJECTS_MISSING_TARGET':
{
output.error({
title: `Based on your config, the following projects were matched for release but do not have a "${error.data.targetName}" target specified. Please ensure you have an appropriate plugin such as @nx/js installed, or have configured the target manually, or exclude the projects using release groups config in nx.json:`,
bodyLines: Array.from(error.data.projects).map((name) => `- ${name}`),
});
}
break;
case 'RELEASE_GROUP_RELEASE_TAG_PATTERN_VERSION_PLACEHOLDER_MISSING_OR_EXCESSIVE':
{
const nxJsonMessage = await resolveNxJsonConfigErrorMessage([
Expand Down Expand Up @@ -610,27 +583,6 @@ function ensureArray(value: string | string[]): string[] {
return Array.isArray(value) ? value : [value];
}

function ensureProjectsHaveTarget(
projects: string[],
projectGraph: ProjectGraph,
requiredTargetName: string
): null | CreateNxReleaseConfigError {
const missingTargetProjects = projects.filter(
(project) =>
!projectHasTarget(projectGraph.nodes[project], requiredTargetName)
);
if (missingTargetProjects.length) {
return {
code: 'PROJECTS_MISSING_TARGET',
data: {
targetName: requiredTargetName,
projects: missingTargetProjects,
},
};
}
return null;
}

function isObject(value: any): value is Record<string, any> {
return value && typeof value === 'object' && !Array.isArray(value);
}
Expand Down
Loading