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(resolver): Fix incorrect peer dependency res. from different trees #4687

Merged
merged 4 commits into from
Oct 11, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/package-linker.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,24 @@ export default class PackageLinker {
}
const ref = pkg._reference;
invariant(ref, 'Package reference is missing');
// TODO: We are taking the "shortest" ref tree but there may be multiple ref trees with the same length
Copy link
Member

Choose a reason for hiding this comment

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

Is it a problem if we choose one of those at random?

Copy link
Member Author

Choose a reason for hiding this comment

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

I honestly don't know, hence the TODO.

const refTree = ref.requests.map(req => req.parentNames).sort((arr1, arr2) => arr1.length - arr2.length)[0];
const refTreeLen = refTree.length;
Copy link
Member

Choose a reason for hiding this comment

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

I'd just use refTree.length, it looks like a premature optim (same for pkgParentsLen)

Copy link
Member Author

Choose a reason for hiding this comment

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

It was mostly for ease of reading for myself but sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh well, these were easier on the eyes in a previous version of the code. Now they are useless as you pointed out.


const getLevelDistance = pkgRef => {
let minDistance = Infinity;
for (const req of pkgRef.requests) {
const pkgParents = req.parentNames;
const pkgParentsLen = pkgParents.length;
const distance = refTreeLen - pkgParentsLen;

if (distance >= 0 && distance < minDistance && pkgParents.every((name, idx) => name === refTree[idx])) {
Copy link
Member

Choose a reason for hiding this comment

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

Might be more less readable, but more efficient to use !pakgParents.some instead?

minDistance = distance;
}
}

return minDistance;
};

for (const peerDepName in peerDeps) {
const range = peerDeps[peerDepName];
Expand All @@ -459,16 +477,16 @@ export default class PackageLinker {
if (!(peerPkgRef && peerPkgRef.patterns)) {
continue;
}
const levelDistance = ref.level - peerPkgRef.level;
if (levelDistance >= 0 && levelDistance < resolvedLevelDistance) {
const levelDistance = getLevelDistance(peerPkgRef);
if (isFinite(levelDistance) && levelDistance < resolvedLevelDistance) {
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to not set minDistance to Infinity and just check for existence now?

Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted that too but then I'd need to add special casing into the getLevelDistance code above due to the distance >= 0 && distance < minDistance checks. Is it too confusing right now? :(

Copy link
Member

Choose a reason for hiding this comment

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

Ah I see, hmm can't think of an alternative atm. Thanks for explaining. :)

if (this._satisfiesPeerDependency(range, peerPkgRef.version)) {
resolvedLevelDistance = levelDistance;
resolvedPeerPkgPattern = peerPkgRef.patterns;
this.reporter.verbose(
this.reporter.lang(
'selectedPeer',
`${pkg.name}@${pkg.version}`,
`${peerDepName}@${range}`,
`${peerDepName}@${peerPkgRef.version}`,
peerPkgRef.level,
),
);
Expand Down