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

CLI: Fix get versions utility for NPM #29577

Open
wants to merge 4 commits into
base: next
Choose a base branch
from

Conversation

johnrcui
Copy link

@johnrcui johnrcui commented Nov 8, 2024

Closes #26553

What I did

This PR patches a core utility function used to retrieve package versions with npm or pnpm and affects everyone who tries to run npx storybook@latest init using npm or pnpm as a package manager.

The command used to retrieve the latest package version npm info <package> version --json on recent versions of npm no longer return a valid JSON response (eg. "8.4.2" the semantic version wrapped in double quotes), but instead just returns an unquoted string. The same is the case with pnpm and also affects those using bun since the underlying call to get versions uses an npm command.

Without having to figure out what p/npm version's this behavior changed and needing to keep track of it, the following PR resorts to just using npm info <package> version (without the --json flag) as this behavior is consistent across all versions of p/npm.

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 77.7 MB 77.7 MB 0 B -4.34 0%
initSize 143 MB 143 MB 102 B -4.17 0%
diffSize 65.2 MB 65.2 MB 102 B 0.8 0%
buildSize 6.83 MB 6.83 MB 0 B -0.28 0%
buildSbAddonsSize 1.51 MB 1.51 MB 0 B 0.15 0%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 1.86 MB 1.86 MB 0 B -0.28 0%
buildSbPreviewSize 271 kB 271 kB 0 B 0.18 0%
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 3.83 MB 3.83 MB 0 B -0.28 0%
buildPreviewSize 3 MB 3 MB 0 B 0.45 0%
testBuildSize 0 B 0 B 0 B - -
testBuildSbAddonsSize 0 B 0 B 0 B - -
testBuildSbCommonSize 0 B 0 B 0 B - -
testBuildSbManagerSize 0 B 0 B 0 B - -
testBuildSbPreviewSize 0 B 0 B 0 B - -
testBuildStaticSize 0 B 0 B 0 B - -
testBuildPrebuildSize 0 B 0 B 0 B - -
testBuildPreviewSize 0 B 0 B 0 B - -
name before after diff z %
createTime 6.6s 6.8s 217ms -1.51 3.2%
generateTime 19.8s 19.8s 2ms -0.32 0%
initTime 13.1s 13.7s 622ms -0.45 4.5%
buildTime 8.2s 8.1s -53ms -0.43 -0.6%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 6.4s 5.1s -1s -291ms -1.2 -24.9%
devManagerResponsive 4s 3.1s -979ms -1.61 🔰-31.4%
devManagerHeaderVisible 732ms 506ms -226ms -1.09 -44.7%
devManagerIndexVisible 856ms 538ms -318ms -1.5 🔰-59.1%
devStoryVisibleUncached 824ms 851ms 27ms -0.75 3.2%
devStoryVisible 848ms 530ms -318ms -1.44 🔰-60%
devAutodocsVisible 647ms 444ms -203ms -0.79 -45.7%
devMDXVisible 712ms 465ms -247ms -0.8 -53.1%
buildManagerHeaderVisible 730ms 521ms -209ms -0.68 -40.1%
buildManagerIndexVisible 764ms 542ms -222ms -0.62 -41%
buildStoryVisible 726ms 508ms -218ms -0.77 -42.9%
buildAutodocsVisible 586ms 431ms -155ms -0.75 -36%
buildMDXVisible 628ms 455ms -173ms -0.38 -38%

Greptile Summary

This PR fixes version retrieval functionality in Storybook's package manager proxies to handle changes in npm/pnpm version output format.

  • Modified runGetVersions in NPM/PNPM/BUN proxies to handle unquoted version strings from newer package managers
  • Removed --json flag when fetching single version information since newer versions return plain strings
  • Updated test files to expect unquoted version strings instead of JSON-formatted responses
  • Fixed issue preventing npx storybook@latest init from working with npm/pnpm package managers
  • Maintained JSON parsing only when fetching all versions with constraints

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

5 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +235 to 239
it('with constraint it throws an error if command output is not a valid JSON', async () => {
vi.spyOn(npmProxy, 'executeCommand').mockResolvedValueOnce('NOT A JSON');

await expect(npmProxy.latestVersion('@storybook/core')).rejects.toThrow();
await expect(npmProxy.latestVersion('@storybook/core', '5.X')).rejects.toThrow();
});
Copy link
Contributor

Choose a reason for hiding this comment

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

style: test description should clarify that this only applies when using version constraints, as unconstrained version queries now expect plain strings

Copy link
Author

Choose a reason for hiding this comment

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

The updated test description explicitly focuses the scope to just when using version constraints to satisfy the fact that this test previously existed. I'm not sure why this test is even here when it's making an assumption on how an external system will fail and forcing it to fail by intentionally mocking a failing response from the external system. An integration test that actually confirms that the expected result and format from calling the external system is true would be a better test.

Comment on lines +157 to 161
it('with constraint it throws an error if command output is not a valid JSON', async () => {
vi.spyOn(pnpmProxy, 'executeCommand').mockResolvedValueOnce('NOT A JSON');

await expect(pnpmProxy.latestVersion('@storybook/core')).rejects.toThrow();
await expect(pnpmProxy.latestVersion('@storybook/core', '5.X')).rejects.toThrow();
});
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Test case should also verify behavior without constraint since both paths can throw JSON parse errors

Copy link
Author

Choose a reason for hiding this comment

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

The behavior without constraint cannot be tested because it does not throw an error nor does it attempt to validate the correctness of the command output.

@johnrcui johnrcui changed the title Fix get versions utility fix(core): get versions utility Nov 8, 2024
@johnrcui johnrcui force-pushed the fix-get-versions-utility branch from b5d9e82 to 170c43a Compare November 8, 2024 21:52
Copy link

nx-cloud bot commented Nov 11, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 170c43a. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

@storybook-pr-benchmarking
Copy link

storybook-pr-benchmarking bot commented Nov 11, 2024

Package Benchmarks

Commit: 76e97c3, ran on 26 November 2024 at 13:23:02 UTC

No significant changes detected, all good. 👏

@yannbf yannbf changed the title fix(core): get versions utility CLI: Fix get versions utility for NPM Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot init in a new project
3 participants