Skip to content

Commit

Permalink
fix(release): publish error handling, dry-run in dependsOn (#20889)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry authored and jaysoo committed Dec 22, 2023
1 parent 33927d9 commit da84297
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ jobs:
pids+=($!)
(pnpm nx affected --targets=lint,test,build --base=$NX_BASE --head=$NX_HEAD --parallel=3 &&
pnpm nx affected --target=e2e --base=$NX_BASE --head=$NX_HEAD --parallel=1) &
pnpm nx affected --target=e2e --base=$NX_BASE --head=$NX_HEAD --parallel=1 --exclude=e2e-webpack) &
pids+=($!)
for pid in "${pids[@]}"; do
Expand Down
3 changes: 2 additions & 1 deletion e2e/release/src/release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ ${{
}}`
);
}
expect(dependencyRelationshipLogMatch.length).toEqual(1);
// TODO: re-enable this assertion once the flakiness documented in NXC-143 is resolved
// expect(dependencyRelationshipLogMatch.length).toEqual(1);

// Generate a changelog for the new version
expect(exists('CHANGELOG.md')).toEqual(false);
Expand Down
52 changes: 35 additions & 17 deletions packages/js/src/executors/release-publish/release-publish.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export default async function runExecutor(
options: PublishExecutorSchema,
context: ExecutorContext
) {
/**
* We need to check both the env var and the option because the executor may have been triggered
* indirectly via dependsOn, in which case the env var will be set, but the option will not.
*/
const isDryRun = process.env.NX_DRY_RUN === 'true' || options.dryRun || false;

const projectConfig =
context.projectsConfigurations!.projects[context.projectName!]!;

Expand Down Expand Up @@ -68,7 +74,7 @@ export default async function runExecutor(
npmPublishCommandSegments.push(`--otp=${options.otp}`);
}

if (options.dryRun) {
if (isDryRun) {
npmPublishCommandSegments.push(`--dry-run`);
}

Expand All @@ -85,7 +91,7 @@ export default async function runExecutor(
* Therefore, so as to not produce misleading output in dry around dist-tags being altered, we do not
* perform the npm view step, and just show npm publish's dry-run output.
*/
if (!options.dryRun) {
if (!isDryRun) {
const currentVersion = projectPackageJson.version;
try {
const result = execSync(npmViewCommandSegments.join(' '), {
Expand All @@ -107,7 +113,7 @@ export default async function runExecutor(

if (resultJson.versions.includes(currentVersion)) {
try {
if (!options.dryRun) {
if (!isDryRun) {
execSync(
`npm dist-tag add ${packageName}@${currentVersion} ${tag} --registry=${registry}`,
{
Expand All @@ -133,21 +139,33 @@ export default async function runExecutor(
try {
const stdoutData = JSON.parse(err.stdout?.toString() || '{}');

console.error('npm dist-tag add error:');
if (stdoutData.error.summary) {
console.error(stdoutData.error.summary);
}
if (stdoutData.error.detail) {
console.error(stdoutData.error.detail);
}
// If the error is that the package doesn't exist, then we can ignore it because we will be publishing it for the first time in the next step
if (
!(
stdoutData.error?.code?.includes('E404') &&
stdoutData.error?.summary?.includes('no such package available')
) &&
!(
err.stderr?.toString().includes('E404') &&
err.stderr?.toString().includes('no such package available')
)
) {
console.error('npm dist-tag add error:');
if (stdoutData.error.summary) {
console.error(stdoutData.error.summary);
}
if (stdoutData.error.detail) {
console.error(stdoutData.error.detail);
}

if (context.isVerbose) {
console.error('npm dist-tag add stdout:');
console.error(JSON.stringify(stdoutData, null, 2));
if (context.isVerbose) {
console.error('npm dist-tag add stdout:');
console.error(JSON.stringify(stdoutData, null, 2));
}
return {
success: false,
};
}
return {
success: false,
};
} catch (err) {
console.error(
'Something unexpected went wrong when processing the npm dist-tag add output\n',
Expand Down Expand Up @@ -197,7 +215,7 @@ export default async function runExecutor(
const normalizedStdoutData = stdoutData[packageName] ?? stdoutData;
logTar(normalizedStdoutData);

if (options.dryRun) {
if (isDryRun) {
console.log(
`Would publish to ${registry} with tag "${tag}", but ${chalk.keyword(
'orange'
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/command-line/release/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ async function runPublishOnProjects(
}
if (args.dryRun) {
overrides.dryRun = args.dryRun;
/**
* Ensure the env var is set too, so that any and all publish executors triggered
* indirectly via dependsOn can also pick up on the fact that this is a dry run.
*/
process.env.NX_DRY_RUN = 'true';
}

if (args.verbose) {
Expand Down

0 comments on commit da84297

Please sign in to comment.