Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Respect link deps when calculating peerDep sets
Browse files Browse the repository at this point in the history
Previously, we were not including link targets in the virtual trees
where peer dependency sets are calculated.  Additionally, we were still
using the path `/virtual-root` for the virtual node, even though this is
no longer load-bearing.  (And, as of the recent change to the Node
printable output, no longer necessary or particularly helpful for
debugging.)

As a result, a link dependency from the root node like `file:../../foo`
would get resolved against `/virtual-root`, resulting in `/foo`, which
of course does not match any Node in the virtual tree.  The outcome was
an ERESOVLVE error where the `current` Node is shown as having no name
or version (because it is an unsatisfied Link).

The solution is two-part.

First, the path of the virtual tree root now matches the path of the
Node that it is sourced from.

Second, any Link children of the source node have their targets mirrored
in the virtual tree, resulting in them being matched appropriately.

The result is that a Link dependency can now properly satisfy a
peerDependency.  Test shows an example of using a Link to a local Node
as a workaround for a peerSet that otherwise would not be resolveable.

This can of course be abused to get around valid peerDep contracts, but
if they user takes it on themselves to use a local fork of a dependency,
we should respect that in buildIdealTree as we do elsewhere.

Fix: npm/cli#2199
  • Loading branch information
isaacs committed Mar 9, 2021
1 parent 8ef1988 commit 962214b
Show file tree
Hide file tree
Showing 7 changed files with 612 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/arborist/build-ideal-tree.js
Expand Up @@ -883,6 +883,8 @@ This is a one-time fix-up, please be patient...
// create a virtual root node with the same deps as the node that
// is requesting this one, so that we can get all the peer deps in
// a context where they're likely to be resolvable.
// Note that the virtual root will also have virtual copies of the
// targets of any child Links, so that they resolve appropriately.
const parent = parent_ || this[_virtualRoot](edge.from)
const realParent = edge.peer ? edge.from.resolveParent : edge.from

Expand Down Expand Up @@ -936,11 +938,23 @@ This is a one-time fix-up, please be patient...
return this[_virtualRoots].get(node)

const vr = new Node({
path: '/virtual-root',
path: node.realpath,
sourceReference: node,
legacyPeerDeps: this.legacyPeerDeps,
})

// also need to set up any targets from any link deps, so that
// they are properly reflected in the virtual environment
for (const child of node.children.values()) {
if (child.isLink) {
new Node({
path: child.realpath,
sourceReference: child.target,
root: vr,
})
}
}

this[_virtualRoots].set(node, vr)
return vr
}
Expand Down

0 comments on commit 962214b

Please sign in to comment.