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(js): show lifecycle script contents in publish executor, scrub version in dry-run #23850

Merged
merged 5 commits into from
May 27, 2024

Conversation

JamesHenry
Copy link
Collaborator

@JamesHenry JamesHenry commented May 21, 2024

Current Behavior

Lifecycle scripts related to publishing change the output of npm publish, which mixes JSON with non-JSON content despite the --json flag being set. Currently, we will error when attempting to parse the whole thing as JSON.

The lifecycle scripts contents themselves are not shown to the user.

Additionally, during dry-run we have no choice but to print the version that currently exists on disk, which can be confusing.

Expected Behavior

We extract and parse the JSON from the npm publish output, even when it is mixed with other output. We also show the lifecycle script outputs to the user, where applicable.

During dry-run, we replace the version in the publish output with a placeholder to avoid confusion around what would be published.

Related Issue(s)

Fixes #22925

Copy link

vercel bot commented May 21, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated (UTC)
nx-dev ⬜️ Ignored (Inspect) Visit Preview May 24, 2024 9:13am

@JamesHenry JamesHenry self-assigned this May 21, 2024
const extractJsonData = extractNpmPublishJsonData(output.toString());
if (!extractJsonData.jsonData) {
console.error(
'The npm publish output data could not be extracted. Please report this issue on https://github.com/nrwl/nx'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not occur under any known circumstances, that's why I added this request for a report

Copy link
Collaborator

@FrozenPandaz FrozenPandaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution is much better and more robust 👍

Some cleanup and I think it's good to go.

Comment on lines +57 to +61
return {
beforeJsonData: str,
jsonData: null,
afterJsonData: '',
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should throw an error here instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think I agree, because we would then need dedicated handling within the existing try catch that this code executes within to get it to ignore this specific error only, it would currently blow up and end up in the "something went wrong" ultimate fallback. That catch is already relatively involved for handling errors from npm and that would suddenly bloat its purpose to handle known errors from us as well. It’s either that or wrap it in another nested try catch which seems equally undesirable…

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately, it's subjective, with zero consequences to the user experience either way (assuming you agree with me that it shouldn't end up in the "something unexpected went wrong" section), so hopefully we don't need to block the PR over it

* Additionally, we want to capture and show the lifecycle script outputs as beforeJsonData and afterJsonData and print them accordingly below.
*/
const extractJsonData = extractNpmPublishJsonData(output.toString());
if (!extractJsonData.jsonData) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we throw an error from the function, we won't need this block.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see comment, we would need more code overall than this block in the error case.


// If npm workspaces are in use, the publish output will nest the data under the package name, so we normalize it first
const normalizedStdoutData = stdoutData[packageName] ?? stdoutData;
const normalizedStdoutData =
extractJsonData.jsonData[packageName] ?? extractJsonData.jsonData;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't easily tell. Is the nested packageName specific form handled by the function above?

Can you ensure unit tests exist for both cases please?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As part of my testing of npm to ensure that the required keys are present in all supported versions (and beyond), I (re)discovered the fundamental change that we made to this executor a little while ago.

In order to improve the npm config resolution (and in taking advice from Luke from NPM) we stopped running npm publish from the package root and began running it from the workspace root, pointing it to the package root instead.

Well it turns out the nesting behaviour that I handled on this line a long time ago (and the comment I wrote above it) are only ever applicable when running with workspaces AND when executing the command from the package root. So this nesting never occurs in our executor any more and I therefore removed it entirely. This also lets us keep the new JSON extraction logic as simple as possible.

So that this knowledge is not lost I have added a comment above the npm publish command construction: 21b243d#diff-1c2ebfe42e321fc6f4a8c99988807e45610b304f4685123906741b0260fc5472R204-R211

@FrozenPandaz FrozenPandaz merged commit aa2519f into master May 27, 2024
6 checks passed
@FrozenPandaz FrozenPandaz deleted the publish-executor-fixes branch May 27, 2024 14:20
FrozenPandaz pushed a commit that referenced this pull request May 28, 2024
…rsion in dry-run (#23850)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

Lifecycle scripts related to publishing change the output of `npm
publish`, which mixes JSON with non-JSON content despite the `--json`
flag being set. Currently, we will error when attempting to parse the
whole thing as JSON.

The lifecycle scripts contents themselves are not shown to the user.

Additionally, during dry-run we have no choice but to print the version
that currently exists on disk, which can be confusing.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

We extract and parse the JSON from the `npm publish` output, even when
it is mixed with other output. We also show the lifecycle script outputs
to the user, where applicable.

During dry-run, we replace the version in the publish output with a
placeholder to avoid confusion around what would be published.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #22925

(cherry picked from commit aa2519f)
Copy link

github-actions bot commented Jun 7, 2024

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nx release fails when publishing packages that have a prepublishOnly step that produces console output
3 participants