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: Peer dependencies should only be looked up from top level dependencies #3803

Merged
merged 7 commits into from
Jul 7, 2017

Conversation

kaylie-alexa
Copy link
Member

Summary
This is a bug fix for #3710

The previous implementation of satisfying peer dependencies of an added dependency looked through all the existing patterns of a dependency (including nested dependencies of other packages) and returned the first match, which ended up copying the nested modules of the other dependency and swallowing warnings about unmet/incorrect peer dependencies. This PR checks for whether the package is at root level or not and otherwise throws a warning if it doesn't meet the semver qualifications.

Test plan
Added tests, and tested on CLI that the peer dependencies don't get installed.

BEFORE
screen shot 2017-07-02 at 5 38 28 pm

AFTER
screen shot 2017-07-02 at 5 39 36 pm

@voxsim
Copy link
Contributor

voxsim commented Jul 3, 2017

I deleted my comment because I was wrong ;)

@BYK BYK self-assigned this Jul 5, 2017
@BYK BYK self-requested a review July 5, 2017 13:39
@BYK BYK added this to In flight (review/code) in Yarn 1.0 Jul 5, 2017
Copy link
Member

@BYK BYK left a comment

Choose a reason for hiding this comment

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

Quite an elegant solution and great test case :)

Just one comment about that callback function and then I think we're good to go.

return resolvedPattern ? this._satisfiesPeerDependency(range, resolvedPattern.version) : false;
const pkgs = this.resolver.getAllInfoForPackageName(name);
let foundPattern;
pkgs.find(pkg => {
Copy link
Member

Choose a reason for hiding this comment

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

I think what you intend to do is this:

const found = pkgs.find(pkg => {
    const {root, version, patterns} = pkg._reference || {};
    return root && this._satisfiesPeerDependency(range, version) && patterns;
});

if (found) {
    ref.addDependencies(found._reference.patterns);
}

Modifying the outside scope in iteration callbacks are a bit dangerous and quite confusing to the reader :(

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah! i didn't like the side effect either but i got a warning about Property cannot be assigned on possibly undefined value then it got a bit messier as I had to check for found && found._reference, etc. I'll try with your suggestion again.

Copy link
Member

Choose a reason for hiding this comment

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

You can probably use invariant() if nothing helps with that warning. That said, with the way I suggested, it shouldn't be a problem.

Copy link
Member

@BYK BYK left a comment

Choose a reason for hiding this comment

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

Looks good to me and builds are also passing so feel free to merge whenever!

});
const foundPattern = found && found._reference && found._reference.patterns;
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if this is you or flow yelling at you but you don't need to check found._reference since it is required for .find() to return something for found. Actually you can even move patterns up there and just check found as I suggested earlier. I'm guessing you did it this way because of flow complaining tho :D

Copy link
Member Author

Choose a reason for hiding this comment

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

yup exactly, made flow unhappy. thanks!

@BYK BYK changed the title Fix peer dependency to only look from top level dependencies Fix: Peer dependencies should only be looked up from top level dependencies Jul 7, 2017
@kaylie-alexa kaylie-alexa merged commit 337b73f into yarnpkg:master Jul 7, 2017
@cpojer cpojer moved this from Awaiting Review to Done in Yarn 1.0 Jul 7, 2017
BYK added a commit that referenced this pull request Sep 16, 2017
…4478)

**Summary**

Fixes #4446, fixes #4433, fixes #2688, fixes #2387. Follow up to #3803. The fix in #3893 was
too aggressive, allowing only top-level dependencies to be used in
peer dependency resolution which was incorrect. This patch allows
resolving peer dependencies from the same or higher levels in the
dependency tree.

**Test plan**

Additional unit and integration tests.
joaolucasl pushed a commit to joaolucasl/yarn that referenced this pull request Oct 27, 2017
…arnpkg#4478)

**Summary**

Fixes yarnpkg#4446, fixes yarnpkg#4433, fixes yarnpkg#2688, fixes yarnpkg#2387. Follow up to yarnpkg#3803. The fix in yarnpkg#3893 was
too aggressive, allowing only top-level dependencies to be used in
peer dependency resolution which was incorrect. This patch allows
resolving peer dependencies from the same or higher levels in the
dependency tree.

**Test plan**

Additional unit and integration tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants