yarn icon indicating copy to clipboard operation
yarn copied to clipboard

Version constraint in dependencies resolved incorrectly

Open steplerbox opened this issue 9 years ago • 10 comments

Version constraint in dependencies resolved incorrectly.

project dependencies

  • react: 0.14.8
  • react-date-range: 0.2.4

react-date-range has dependency

  • react: >=0.13.0

0.14.8 satisfies constraint >=0.13.0

BUT

yarn adds node_modules/react-date-range/node_modules/react - v16.0.0. And with new react version we are getting breaking changes.

Expected behavior: don't install react v 16.0.0 to node_modules/react-date-range/node_modules npm v4.6.1 behaves as expected.

KDE 16.10 node v8.0.0 npm v4.6.1 yarn v1.1.0

steplerbox avatar Sep 27 '17 10:09 steplerbox

File a bug on react-date-range, they are using a dependency instead of a peer dependency.

Dependencies have no guarantee to be deduped, we only do this as an optimization and we could imagine removing it or changing the algorithm at any time (for example through a --no-hoisting option). Peer dependencies are what they should use here, because they really want to always use the exact same version and instance than their parent.

Closing as not-a-bug, but feel free to continue the discussion.

arcanis avatar Sep 27 '17 10:09 arcanis

Ok, they should use peerDependency. But it doesn't answer to my question - why is new version of react installed if i already have 0.14.8 and it satisfies constraint >=0.13.0?

steplerbox avatar Sep 27 '17 11:09 steplerbox

I agree that this is not ideal and we should improve the hoister to handle this case (I actually have an idea about what's causing this), but that doesn't change the final verdict: as long as your application is relying on this behavior, things might break at any time.

To summarize: in the end, this is an optimization bug, not a resolution bug.

arcanis avatar Sep 27 '17 11:09 arcanis

With npm everything is ok. I think we should use it. Thanks.

steplerbox avatar Sep 27 '17 11:09 steplerbox

Afaik NPM doesn't make any different guarantee on this side. It happens to work now, but is just as likely to break in the future if they change their hoisting algorithm. The resolution bug is in the dependency, that's where you should look for a fix.

arcanis avatar Sep 27 '17 11:09 arcanis

@steplerbox if you are happy with npm, of course go ahead and use it. Yarn doesn't exist because npm is bad or terrible or anything like that. Yarn exists because it focuses on different areas than npm focuses. That said I'm wondering what triggered your 👎 on @arcanis' answer since I think he both acknowledged the issue about optimization and guided you on how to achieve the desired and a safer result. What was missing there, would you mind elaborating a bit?

BYK avatar Sep 27 '17 11:09 BYK

@BYK and how to achieve the desired result?

steplerbox avatar Sep 27 '17 11:09 steplerbox

@arcanis I need to make a branch, change package.json and use it? Currently I have a lot of fixed versions and a big part of them have react in dependencies, not a peerDependencies. How to manage with it?

steplerbox avatar Sep 27 '17 12:09 steplerbox

@BYK and how to achieve the desired result?

File an issue or even better, submit a PR to the projects you use? You can then use the PR "branch" with a GitHub require in your project until it gets merged and published.

You can also give https://yarnpkg.com/en/docs/selective-version-resolutions/ a shot in the meantime.

BYK avatar Sep 27 '17 12:09 BYK

@BYK thanks, but it doesn't look like adequate solution. Because i need to make a dozens of pull requests. Here is more elegant solution. Add alias for react to webpack configuration.

Thanks for your help and explanations. But for me it's so strange when something work good, but after some updates everything breaks. I refuse to understand why it's necessary to add dependency if I already have it.

steplerbox avatar Sep 27 '17 13:09 steplerbox