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: Invariant failed: Expected package.json#version to be defined in the "undefined" package #28752

Merged
merged 1 commit into from
Aug 2, 2024

Conversation

abcdmku
Copy link
Contributor

@abcdmku abcdmku commented Jul 29, 2024

Closes #28606

What I did

It looks like the packageJson const was getting set as the path rather than returning the json. I renamed that const and used it to read the file contents and parse the json.

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!

  1. I tested a monorepo that has the undefined version issue:
 NX   Invariant failed: Expected package.json#version to be defined in the "undefined" package}
  1. I built the code then copied the built core-server code to the monorepo that was having an issue.
  2. I ran storybook and saw it successfully spin up:
╭───────────────────────────────────────────────────╮
│                                                   │
│   Storybook 8.2.6 for react-webpack5 started      │
│   345 ms for manager and 7.73 s for preview       │
│                                                   │
│    Local:            http://localhost:4402/       │
│    On your network:  http://192.168.4.41:4402/    │
│                                                   │
╰───────────────────────────────────────────────────╯

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/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 pull request has been released as version 0.0.0-pr-28752-sha-a65743e5. Try it out in a new sandbox by running npx storybook@0.0.0-pr-28752-sha-a65743e5 sandbox or in an existing project with npx storybook@0.0.0-pr-28752-sha-a65743e5 upgrade.

More information
Published version 0.0.0-pr-28752-sha-a65743e5
Triggered by @ndelangen
Repository abcdmku/storybook
Branch issue-28606-fix
Commit a65743e5
Datetime Tue Jul 30 14:36:35 UTC 2024 (1722350195)
Workflow run 10164438729

To request a new release of this pull request, mention the @storybookjs/core team.

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

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 76.3 MB 76.3 MB 0 B -2.93 0%
initSize 198 MB 198 MB -597 B -0.08 0%
diffSize 122 MB 122 MB -597 B 0.51 0%
buildSize 7.6 MB 7.6 MB -303 B 0.07 0%
buildSbAddonsSize 1.63 MB 1.63 MB -192 B -0.43 0%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 2.3 MB 2.3 MB 13 B 0.33 0%
buildSbPreviewSize 349 kB 349 kB -124 B -0.58 0%
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 4.47 MB 4.47 MB -303 B -0.49 0%
buildPreviewSize 3.12 MB 3.12 MB 0 B 0.73 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 7.5s 25.6s 18s 1.75 🔺70.4%
generateTime 24.8s 21.1s -3s -672ms -0.8 -17.3%
initTime 25.6s 21s -4s -570ms -1.24 🔰-21.7%
buildTime 16.2s 13.8s -2s -421ms -0.42 -17.5%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 8.3s 9.1s 797ms -0.15 8.7%
devManagerResponsive 5.4s 5.9s 500ms -0.12 8.5%
devManagerHeaderVisible 845ms 915ms 70ms 0.19 7.7%
devManagerIndexVisible 871ms 926ms 55ms 0.07 5.9%
devStoryVisibleUncached 1.4s 1.4s 49ms 0.08 3.3%
devStoryVisible 893ms 971ms 78ms 0.17 8%
devAutodocsVisible 784ms 967ms 183ms 1.19 18.9%
devMDXVisible 740ms 854ms 114ms 1.24 🔺13.3%
buildManagerHeaderVisible 876ms 944ms 68ms 0.65 7.2%
buildManagerIndexVisible 877ms 946ms 69ms 0.6 7.3%
buildStoryVisible 918ms 1s 88ms 0.63 8.7%
buildAutodocsVisible 794ms 753ms -41ms -0.31 -5.4%
buildMDXVisible 689ms 814ms 125ms 0.63 15.4%

Greptile Summary

The pull request fixes an issue with the packageJson constant in standalone.ts, ensuring the correct JSON content is read and parsed.

  • Modified code/core/src/core-server/standalone.ts to correctly read and parse package.json.
  • Renamed the constant to packageJsonDir to hold the directory path.
  • Ensured packageJson contains the actual JSON content, resolving the undefined package version error.

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.

LGTM

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

@abcdmku
Copy link
Contributor Author

abcdmku commented Jul 30, 2024

So I updated another monorepo we recently built to Nx 19.5.3 and Storybook 8.2.6. Storybook starts up just fine without the fix:

╭────────────────────────────────────────────────────╮
│                                                    │
│   Storybook 8.2.6 for react-webpack5 started       │
│   85 ms for manager and 3.15 s for preview         │
│                                                    │
│    Local:            http://localhost:59829/       │
│    On your network:  http://192.168.4.41:59829/    │
│                                                    │
╰────────────────────────────────────────────────────╯

After comparing both monorepos with each other, I finally found what was different between them! The older monorepo (that has the storybook issue) defines the storybook script in the project.json as seen here:

    "storybook": {
      "executor": "@nx/storybook:storybook",
      "options": {
        "port": 4402,
        "configDir": "libs/component-lib/.storybook"
      },
      "configurations": {
        "ci": {
          "quiet": true
        }
      }
    },

The newer monorepo defines the storybook script as part of the plugins in nx.json as seen here:

"plugins": [
  {
    "plugin": "@nx/storybook/plugin",
    "options": {
      "serveStorybookTargetName": "storybook",
      "buildStorybookTargetName": "build-storybook",
      "testStorybookTargetName": "test-storybook",
      "staticStorybookTargetName": "static-storybook"
    }
  },
]

I removed the storybook definition from the older monorepo project.json and added the plugin on the nx.json and stroybook started right up :D

When using the storybook script from the plugin, a node_modules folder is created in the project with storybook defined. I'm assuming thats why storybook doesn't throw an error.

ps:
I then took the built code from this PR and replaced the code in node_modules/@storybook/core/dist/core-server for the newer monorepo and everything still worked. So this change seems to be backwards compatible.

@wadamek65
Copy link

@abcdmku Just tried this out myself and it does indeed work! 🎉 Thanks for the info!

@ndelangen ndelangen self-assigned this Jul 30, 2024
@ndelangen ndelangen added bug patch:yes Bugfix & documentation PR that need to be picked to main branch core nx Prioritize Nx compatibility for angular ci:merged Run the CI jobs that normally run when merged. labels Jul 30, 2024
Copy link

nx-cloud bot commented Jul 30, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit a65743e. 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.

@ndelangen
Copy link
Member

I'm releasing a canary release of this PR now.

@abcdmku the PR description will automatically get updated.
Would you be able to test this canary?

@abcdmku
Copy link
Contributor Author

abcdmku commented Aug 1, 2024

Just got around to testing this, sorry for the delay this ticket isn't in my teams sprint 😅

To make sure everything was still failing I ran storybook on that same monorepo:

> nx run component-lib:storybook

NX   Invariant failed: Expected package.json#version to be defined in the "undefined" package}

Installed the canary release

npx storybook@0.0.0-pr-28752-sha-a65743e5 upgrade --force --config-dir libs/component-lib/.storybook

It took a second to get NX happy with a 0.0.0 version of storybook because of the less than v7 check*. After solving that I ran storybook and:

╭──────────────────────────────────────────────────────────────────────╮
│                                                                      │
│   Storybook 0.0.0-pr-28752-sha-a65743e5 for react-webpack5 started   │
│   307 ms for manager and 7.73 s for preview                          │
│                                                                      │
│    Local:            http://localhost:4402/                          │
│    On your network:  http://192.168.4.41:4402/                       │
│                                                                      │
╰──────────────────────────────────────────────────────────────────────╯

🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉

*edit: I created an issue here for the canary version error here: nrwl/nx#27277

@abcdmku abcdmku closed this Aug 1, 2024
@abcdmku abcdmku reopened this Aug 1, 2024
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.

1 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings

code/core/src/core-server/standalone.ts Show resolved Hide resolved
@abcdmku
Copy link
Contributor Author

abcdmku commented Aug 1, 2024

whoops the ui shifted and i clicked the wrong button 😂 didnt mean to close the pr

@abcdmku
Copy link
Contributor Author

abcdmku commented Aug 1, 2024

@ndelangen lmk if you want me to do anything else for this PR

@ndelangen ndelangen merged commit be429a7 into storybookjs:next Aug 2, 2024
73 of 76 checks passed
storybook-bot pushed a commit that referenced this pull request Aug 2, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 2, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 5, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 6, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 6, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 6, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 6, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 6, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 6, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 6, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 6, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 7, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 7, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
storybook-bot pushed a commit that referenced this pull request Aug 7, 2024
Fix: Invariant failed: Expected package.json#version to be defined in the "undefined" package
(cherry picked from commit be429a7)
@github-actions github-actions bot added the patch:done Patch/release PRs already cherry-picked to main/release branch label Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug ci:merged Run the CI jobs that normally run when merged. core nx Prioritize Nx compatibility for angular patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Invariant failed: Expected package.json#version to be defined in the "undefined" package}
3 participants