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

Docgen: Only add react-docgen info when a component is defined in the file #26967

Merged
merged 3 commits into from
May 14, 2024

Conversation

glenjamin
Copy link
Contributor

This avoids errors when a component is being imported and re-exported under a different name via a barrel file or similar.

Closes #26074

What I did

The issue occurs when the react-docgen plugin is attempting to add the additional metadata into a file that exports the component, but doesn't contain the definition or a local with the same name.

I couldn't see enough information in the react-docgen handlers to distinguish between a re-export and a definition, so I've made the extra docgen handler already defined in this loader make a note of the filename from the component definition.

When modifying the AST to add the docgen info, we only assign the info if the definition file is the same as the file we're transforming. This could potentially lead to other scenarios no longer adding the docgen info, but I believe that as long as the file containing the definition is still being processed by the loader, it should only be removing redundant re-assignments.

I'm not especially precious about taking this approach to resolve the issue, so please let me know if an alternative approach is preferred and I can give that a go instead.

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

While I was able to to add a story to the sandbox which reproduced the issue, I struggled to see how this is executed as part of the testing/CI process, so I'd appreciate any pointers if there's something I've missed.

Manual testing

  1. Run a sandbox for react-webpack-18-ts
  2. Open Storybook in your browser
  3. Access any renderers/react/js-argtypes story
  4. Without the fix, we see the following error:
    PropsWriter is not defined
    
    ./src/stories/renderers/react/docgen-components/js-re-exported-component/input.jsx@http://localhost:6006/stories-renderers-react-js-argtypes-stories.iframe.bundle.js:4238:1
    
  5. With this change, there is no longer an error

Documentation

I don't believe any documentation is required.

  • 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 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>

This avoids errors when a component is being imported and re-exported under
a different name via a barrel file or similar.
@yannbf yannbf added bug ci:merged Run the CI jobs that normally run when merged. docgen labels Apr 29, 2024
@yannbf yannbf changed the title Only add react-docgen info in the file a component is defined Docgen: Only add react-docgen info when a component is defined in the file Apr 29, 2024
Copy link

nx-cloud bot commented Apr 29, 2024

☁️ Nx Cloud Report

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

@hanshank
Copy link

hanshank commented May 10, 2024

@glenjamin @valentinpalkovic

  • Any idea if this will be merged anytime soon?

@valentinpalkovic
Copy link
Contributor

Currently, we have a feature freeze. As soon as Storybook 8.1 is published next week, we will unfreeze next and we will be able to merge this PR.

Copy link
Contributor

@valentinpalkovic valentinpalkovic left a comment

Choose a reason for hiding this comment

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

LGTM!

@glenjamin Thank you so much for your contribution. Amazing work. I've added another commit to actually also apply your changes to the react-vite framework.

@valentinpalkovic valentinpalkovic merged commit 691069c into storybookjs:next May 14, 2024
61 of 63 checks passed
@github-actions github-actions bot mentioned this pull request May 14, 2024
17 tasks
@glenjamin
Copy link
Contributor Author

Thanks! will this be backported to v7, or is it v8 only?

@soren121
Copy link

Separately, I would also like to see this included in the next patch release (8.1.x) if possible! This bug has been a blocker for my team upgrading to Storybook 8.

And many thanks for the fix @glenjamin! This bug has been on my list to look into, but you got to it first :)

@valentinpalkovic valentinpalkovic added the patch:yes Bugfix & documentation PR that need to be picked to main branch label May 15, 2024
@valentinpalkovic
Copy link
Contributor

valentinpalkovic commented May 15, 2024

It will not be backported to 7. it will be part of the next patch release 8.1.1 :)

storybook-bot pushed a commit that referenced this pull request May 15, 2024
Docgen: Only add react-docgen info when a component is defined in the file
(cherry picked from commit 691069c)
@github-actions github-actions bot mentioned this pull request May 15, 2024
9 tasks
storybook-bot pushed a commit that referenced this pull request May 15, 2024
Docgen: Only add react-docgen info when a component is defined in the file
(cherry picked from commit 691069c)
storybook-bot pushed a commit that referenced this pull request May 15, 2024
Docgen: Only add react-docgen info when a component is defined in the file
(cherry picked from commit 691069c)
@github-actions github-actions bot mentioned this pull request May 15, 2024
9 tasks
@github-actions github-actions bot added the patch:done Patch/release PRs already cherry-picked to main/release branch label May 15, 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. docgen 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
5 participants