-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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: Add peer dependency on react #20538
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
@IanVS Done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to change this code:
storybook/code/lib/cli/src/ensure-react-peer-deps.ts
Lines 5 to 8 in 34d287e
try { | |
require.resolve('react'); | |
require.resolve('react-dom'); | |
} catch (e) { |
To instead check the contents of package.json
.. As-is defeats the purpose AFAICS, because having a peerDependency, ensures it's installed, then we check if it's installed?
We should read package.json
, and check if react
is defined in either devDependencies
, dependencies
or peerDependencies
.
Please correct me if I'm wrong, but those 2 lines are the reason we've added the peerDependencies in lib/cli
?
@valentinpalkovic I wanted to bring this review to your attention, because git tells me you wrote the code I highlighted. It's totally possible I'm missing some context. Let me know if you'd like to discuss tomorrow? |
@ndelangen This is not the case for all package managers, is it? Yarn, pnpm and npm sometimes (configuration dependent) does not automatically install peer dependencies for you. So this logic is still necessary to ensure a valuable user experience, because peer deps warnings are often ignored. I don‘t understand, though, why we should read the package.jsons content rather than using require.resolve. Can you explain it to me, maybe I miss something. |
In order to do I'm aware some package managers will not install the peer-dependency, but some do. And it seems with modern package managers this is the way. I think we'd be wise to simply assume that's how things are. Looking into package.json instead has 2 major differences:
And it will check what we actually want to check: Does the user have a direct dependency on Let's say we run Let's say we run |
Only if you assume that peer-deps are automatically installed. But the error that React cannot be resolved is thrown for customers who are using package managers, which do not install peer-deps. Namely for the package managers' yarn and yarn berry. You're right that this check will never error in an environment where pnpm and npm (version 3 through 6 though does not install peer-deps automatically) are used because they take care of throwing an error if peer-deps are not installed correctly.
This argument is valid. I would like to add the argument, that
I don't get this point TBH. Running
Same here. Storybook cannot be started if dependencies are not installed.
Is hoisting an issue? Why should it be? We want to ensure, that
Is this important, though? Of course, the correct way would be to install I honestly do not like the approach of scanning the package.json, because project structures might be complex. Is it always easy to say, that it is enough to scan the root's package.json? Can we be 100% sure, that the execution context is on the same level as the package.json is placed? I am not sure about that. Either way, as @IanVS has mentioned in #20454 (comment) we should move the check where it is really needed (@storybook/addon-essentials, @storybook/addon-interactions, @storybook/addon-links, @storybook/angular, @storybook/testing-library). But this would mean that we could revert #20459 (because technically the peer-dependency declaration is not necessary here) and this PR would be obsolete. The react/react-dom check was placed into the cli package though, because it is easier to have it here. |
I chatted with @valentinpalkovic and we concluded to move the core checking for react-dep to addon-docs. I'll have to close this PR, and also revert some work from you @IanVS, but it will make a lot more sense, in the end, I think. |
@bryanjtc thank you for contributing, it's truly appreciated. I'm sorry this PR wasn't merged, certainly not because anything you could have foreseen. I hope to see more contributions from you 🙏 |
Thanks for the discussion @valentinpalkovic and @ndelangen. FWIW I agree with your decision to move this check to addon-docs. It has the side-benefit of allowing projects without docs to avoid installing react, if that is their wish. |
Issue: #20537
What I did
Added react peer dependencies
How to test
If your answer is yes to any of these, please make sure to include it in your PR.