2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
use std::collections::hash_map::DefaultHasher;
|
2022-11-08 14:17:24 -05:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::collections::HashMap;
|
2023-02-21 12:03:48 -05:00
|
|
|
use std::collections::HashSet;
|
2022-11-08 14:17:24 -05:00
|
|
|
use std::collections::VecDeque;
|
2023-02-21 12:03:48 -05:00
|
|
|
use std::hash::Hash;
|
|
|
|
use std::hash::Hasher;
|
2022-11-08 14:17:24 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_core::anyhow::bail;
|
|
|
|
use deno_core::anyhow::Context;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::parking_lot::Mutex;
|
2023-02-21 12:03:48 -05:00
|
|
|
use deno_graph::npm::NpmPackageNv;
|
2023-02-15 13:20:40 -05:00
|
|
|
use deno_graph::npm::NpmPackageReq;
|
|
|
|
use deno_graph::semver::VersionReq;
|
2022-11-08 14:17:24 -05:00
|
|
|
use log::debug;
|
|
|
|
|
|
|
|
use crate::npm::registry::NpmDependencyEntry;
|
|
|
|
use crate::npm::registry::NpmDependencyEntryKind;
|
|
|
|
use crate::npm::registry::NpmPackageInfo;
|
|
|
|
use crate::npm::registry::NpmPackageVersionInfo;
|
2023-02-21 12:03:48 -05:00
|
|
|
use crate::npm::resolution::common::resolve_best_package_version_and_info;
|
|
|
|
use crate::npm::resolution::snapshot::SnapshotPackageCopyIndexResolver;
|
2022-11-08 14:17:24 -05:00
|
|
|
use crate::npm::NpmRegistryApi;
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
use super::common::version_req_satisfies;
|
|
|
|
use super::common::LATEST_VERSION_REQ;
|
2022-11-08 14:17:24 -05:00
|
|
|
use super::snapshot::NpmResolutionSnapshot;
|
2023-02-21 12:03:48 -05:00
|
|
|
use super::NpmPackageId;
|
2022-11-08 14:17:24 -05:00
|
|
|
use super::NpmResolutionPackage;
|
2023-01-31 21:27:40 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
/// A unique identifier to a node in the graph.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
|
|
|
struct NodeId(u32);
|
|
|
|
|
|
|
|
/// A resolved package in the resolution graph.
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Node {
|
|
|
|
/// The specifier to child relationship in the graph. The specifier is
|
|
|
|
/// the key in an npm package's dependencies map (ex. "express"). We
|
|
|
|
/// use a BTreeMap for some determinism when creating the snapshot.
|
|
|
|
///
|
|
|
|
/// Note: We don't want to store the children as a `NodeRef` because
|
|
|
|
/// multiple paths might visit through the children and we don't want
|
|
|
|
/// to share those references with those paths.
|
|
|
|
pub children: BTreeMap<String, NodeId>,
|
|
|
|
/// Whether the node has demonstrated to have no peer dependencies in its
|
|
|
|
/// descendants. If this is true then we can skip analyzing this node
|
|
|
|
/// again when we encounter it another time in the dependency tree, which
|
|
|
|
/// is much faster.
|
|
|
|
pub no_peers: bool,
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
#[derive(Clone)]
|
|
|
|
enum ResolvedIdPeerDep {
|
|
|
|
/// This is a reference to the parent instead of the child because we only have a
|
|
|
|
/// node reference to the parent, since we've traversed it, but the child node may
|
|
|
|
/// change from under it.
|
|
|
|
ParentReference {
|
|
|
|
parent: GraphPathNodeOrRoot,
|
|
|
|
child_pkg_nv: NpmPackageNv,
|
|
|
|
},
|
|
|
|
/// A node that was created during snapshotting and is not being used in any path.
|
|
|
|
SnapshotNodeId(NodeId),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResolvedIdPeerDep {
|
|
|
|
pub fn current_state_hash(&self) -> u64 {
|
|
|
|
let mut hasher = DefaultHasher::new();
|
|
|
|
self.current_state_hash_with_hasher(&mut hasher);
|
|
|
|
hasher.finish()
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
pub fn current_state_hash_with_hasher(&self, hasher: &mut DefaultHasher) {
|
|
|
|
match self {
|
|
|
|
ResolvedIdPeerDep::ParentReference {
|
|
|
|
parent,
|
|
|
|
child_pkg_nv,
|
|
|
|
} => {
|
|
|
|
match parent {
|
|
|
|
GraphPathNodeOrRoot::Root(root) => root.hash(hasher),
|
|
|
|
GraphPathNodeOrRoot::Node(node) => node.node_id().hash(hasher),
|
|
|
|
}
|
|
|
|
child_pkg_nv.hash(hasher);
|
|
|
|
}
|
|
|
|
ResolvedIdPeerDep::SnapshotNodeId(node_id) => {
|
|
|
|
node_id.hash(hasher);
|
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
/// A pending resolved identifier used in the graph. At the end of resolution, these
|
|
|
|
/// will become fully resolved to an `NpmPackageId`.
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct ResolvedId {
|
|
|
|
nv: NpmPackageNv,
|
|
|
|
peer_dependencies: Vec<ResolvedIdPeerDep>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResolvedId {
|
|
|
|
/// Gets a hash of the resolved identifier at this current moment in time.
|
|
|
|
///
|
|
|
|
/// WARNING: A resolved identifier references a value that could change in
|
|
|
|
/// the future, so this should be used with that in mind.
|
|
|
|
pub fn current_state_hash(&self) -> u64 {
|
|
|
|
let mut hasher = DefaultHasher::new();
|
|
|
|
self.nv.hash(&mut hasher);
|
|
|
|
for dep in &self.peer_dependencies {
|
|
|
|
dep.current_state_hash_with_hasher(&mut hasher);
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
hasher.finish()
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
pub fn push_peer_dep(&mut self, peer_dep: ResolvedIdPeerDep) {
|
|
|
|
let new_hash = peer_dep.current_state_hash();
|
|
|
|
for dep in &self.peer_dependencies {
|
|
|
|
if new_hash == dep.current_state_hash() {
|
|
|
|
return;
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
self.peer_dependencies.push(peer_dep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mappings of node identifiers to resolved identifiers. Each node has exactly
|
|
|
|
/// one resolved identifier.
|
|
|
|
#[derive(Default)]
|
|
|
|
struct ResolvedNodeIds {
|
|
|
|
node_to_resolved_id: HashMap<NodeId, (ResolvedId, u64)>,
|
|
|
|
resolved_to_node_id: HashMap<u64, NodeId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResolvedNodeIds {
|
|
|
|
pub fn set(&mut self, node_id: NodeId, resolved_id: ResolvedId) {
|
|
|
|
let resolved_id_hash = resolved_id.current_state_hash();
|
|
|
|
if let Some((_, old_resolved_id_key)) = self
|
|
|
|
.node_to_resolved_id
|
|
|
|
.insert(node_id, (resolved_id, resolved_id_hash))
|
|
|
|
{
|
|
|
|
// ensure the old resolved id key is removed as it might be stale
|
|
|
|
self.resolved_to_node_id.remove(&old_resolved_id_key);
|
|
|
|
}
|
|
|
|
self.resolved_to_node_id.insert(resolved_id_hash, node_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, node_id: NodeId) -> Option<&ResolvedId> {
|
|
|
|
self.node_to_resolved_id.get(&node_id).map(|(id, _)| id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_node_id(&self, resolved_id: &ResolvedId) -> Option<NodeId> {
|
|
|
|
self
|
|
|
|
.resolved_to_node_id
|
|
|
|
.get(&resolved_id.current_state_hash())
|
|
|
|
.copied()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
// todo(dsherret): for some reason the lsp errors when using an Rc<RefCell<NodeId>> here
|
|
|
|
// instead of an Arc<Mutex<NodeId>>. We should investigate and fix.
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
/// A pointer to a specific node in a graph path. The underlying node id
|
|
|
|
/// may change as peer dependencies are created.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct NodeIdRef(Arc<Mutex<NodeId>>);
|
|
|
|
|
|
|
|
impl NodeIdRef {
|
|
|
|
pub fn new(node_id: NodeId) -> Self {
|
|
|
|
NodeIdRef(Arc::new(Mutex::new(node_id)))
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
pub fn change(&self, node_id: NodeId) {
|
|
|
|
*self.0.lock() = node_id;
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
|
|
|
|
pub fn get(&self) -> NodeId {
|
|
|
|
*self.0.lock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
enum GraphPathNodeOrRoot {
|
|
|
|
Node(Arc<GraphPath>),
|
|
|
|
Root(NpmPackageNv),
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
/// Path through the graph that represents a traversal through the graph doing
|
|
|
|
/// the dependency resolution. The graph tries to share duplicate package
|
|
|
|
/// information and we try to avoid traversing parts of the graph that we know
|
|
|
|
/// are resolved.
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct GraphPath {
|
|
|
|
previous_node: Option<GraphPathNodeOrRoot>,
|
|
|
|
node_id_ref: NodeIdRef,
|
|
|
|
// todo(dsherret): I think we might be able to get rid of specifier and
|
|
|
|
// node version here, but I added them for extra protection for the time being.
|
2022-11-08 14:17:24 -05:00
|
|
|
specifier: String,
|
2023-02-21 12:03:48 -05:00
|
|
|
nv: NpmPackageNv,
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
impl GraphPath {
|
|
|
|
pub fn for_root(node_id: NodeId, nv: NpmPackageNv) -> Arc<Self> {
|
2022-11-08 14:17:24 -05:00
|
|
|
Arc::new(Self {
|
2023-02-21 12:03:48 -05:00
|
|
|
previous_node: Some(GraphPathNodeOrRoot::Root(nv.clone())),
|
|
|
|
node_id_ref: NodeIdRef::new(node_id),
|
|
|
|
// use an empty specifier
|
|
|
|
specifier: "".to_string(),
|
|
|
|
nv,
|
2022-11-08 14:17:24 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
pub fn node_id(&self) -> NodeId {
|
|
|
|
self.node_id_ref.get()
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
pub fn specifier(&self) -> &str {
|
|
|
|
&self.specifier
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
pub fn change_id(&self, node_id: NodeId) {
|
|
|
|
self.node_id_ref.change(node_id)
|
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
pub fn with_id(
|
|
|
|
self: &Arc<GraphPath>,
|
|
|
|
node_id: NodeId,
|
|
|
|
specifier: &str,
|
|
|
|
nv: NpmPackageNv,
|
|
|
|
) -> Option<Arc<Self>> {
|
|
|
|
if self.has_visited(&nv) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Arc::new(Self {
|
|
|
|
previous_node: Some(GraphPathNodeOrRoot::Node(self.clone())),
|
|
|
|
node_id_ref: NodeIdRef::new(node_id),
|
|
|
|
specifier: specifier.to_string(),
|
|
|
|
nv,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Each time an identifier is added, we do a check to ensure
|
|
|
|
/// that we haven't previously visited this node. I suspect this
|
|
|
|
/// might be a little slow since it has to go up through the ancestors,
|
|
|
|
/// so some optimizations could be made here in the future.
|
|
|
|
pub fn has_visited(self: &Arc<Self>, nv: &NpmPackageNv) -> bool {
|
|
|
|
if self.nv == *nv {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
let mut maybe_next_node = self.previous_node.as_ref();
|
|
|
|
while let Some(GraphPathNodeOrRoot::Node(next_node)) = maybe_next_node {
|
|
|
|
// we've visited this before, so stop
|
|
|
|
if next_node.nv == *nv {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
maybe_next_node = next_node.previous_node.as_ref();
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
pub fn ancestors(&self) -> GraphPathAncestorIterator {
|
|
|
|
GraphPathAncestorIterator {
|
|
|
|
next: self.previous_node.as_ref(),
|
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct GraphPathAncestorIterator<'a> {
|
|
|
|
next: Option<&'a GraphPathNodeOrRoot>,
|
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
impl<'a> Iterator for GraphPathAncestorIterator<'a> {
|
|
|
|
type Item = &'a GraphPathNodeOrRoot;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if let Some(next) = self.next.take() {
|
|
|
|
if let GraphPathNodeOrRoot::Node(node) = next {
|
|
|
|
self.next = node.previous_node.as_ref();
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
Some(next)
|
|
|
|
} else {
|
|
|
|
None
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
#[derive(Default)]
|
2022-11-08 14:17:24 -05:00
|
|
|
pub struct Graph {
|
2023-02-21 12:03:48 -05:00
|
|
|
/// Each requirement is mapped to a specific name and version.
|
|
|
|
package_reqs: HashMap<NpmPackageReq, NpmPackageNv>,
|
|
|
|
/// Then each name and version is mapped to an exact node id.
|
|
|
|
/// Note: Uses a BTreeMap in order to create some determinism
|
|
|
|
/// when creating the snapshot.
|
|
|
|
root_packages: BTreeMap<NpmPackageNv, NodeId>,
|
|
|
|
nodes_by_package_name: HashMap<String, Vec<NodeId>>,
|
|
|
|
nodes: HashMap<NodeId, Node>,
|
|
|
|
resolved_node_ids: ResolvedNodeIds,
|
2022-11-08 14:17:24 -05:00
|
|
|
// This will be set when creating from a snapshot, then
|
|
|
|
// inform the final snapshot creation.
|
2023-02-21 12:03:48 -05:00
|
|
|
packages_to_copy_index: HashMap<NpmPackageId, usize>,
|
2023-02-22 14:15:25 -05:00
|
|
|
/// Packages that the resolver should resolve first.
|
|
|
|
pending_unresolved_packages: Vec<NpmPackageNv>,
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Graph {
|
2023-02-21 12:03:48 -05:00
|
|
|
pub fn from_snapshot(
|
|
|
|
snapshot: NpmResolutionSnapshot,
|
|
|
|
) -> Result<Self, AnyError> {
|
|
|
|
fn get_or_create_graph_node(
|
2022-11-08 14:17:24 -05:00
|
|
|
graph: &mut Graph,
|
2023-02-21 12:03:48 -05:00
|
|
|
resolved_id: &NpmPackageId,
|
|
|
|
packages: &HashMap<NpmPackageId, NpmResolutionPackage>,
|
|
|
|
created_package_ids: &mut HashMap<NpmPackageId, NodeId>,
|
|
|
|
) -> Result<NodeId, AnyError> {
|
|
|
|
if let Some(id) = created_package_ids.get(resolved_id) {
|
|
|
|
return Ok(*id);
|
|
|
|
}
|
|
|
|
|
|
|
|
let node_id = graph.create_node(&resolved_id.nv);
|
|
|
|
created_package_ids.insert(resolved_id.clone(), node_id);
|
|
|
|
|
|
|
|
let peer_dep_ids = resolved_id
|
|
|
|
.peer_dependencies
|
|
|
|
.iter()
|
|
|
|
.map(|peer_dep| {
|
|
|
|
Ok(ResolvedIdPeerDep::SnapshotNodeId(get_or_create_graph_node(
|
|
|
|
graph,
|
|
|
|
peer_dep,
|
|
|
|
packages,
|
|
|
|
created_package_ids,
|
|
|
|
)?))
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, AnyError>>()?;
|
|
|
|
let graph_resolved_id = ResolvedId {
|
|
|
|
nv: resolved_id.nv.clone(),
|
|
|
|
peer_dependencies: peer_dep_ids,
|
|
|
|
};
|
|
|
|
graph.resolved_node_ids.set(node_id, graph_resolved_id);
|
|
|
|
let resolution = match packages.get(resolved_id) {
|
|
|
|
Some(resolved_id) => resolved_id,
|
|
|
|
// maybe the user messed around with the lockfile
|
|
|
|
None => bail!("not found package: {}", resolved_id.as_serialized()),
|
|
|
|
};
|
|
|
|
for (name, child_id) in &resolution.dependencies {
|
|
|
|
let child_node_id = get_or_create_graph_node(
|
|
|
|
graph,
|
|
|
|
child_id,
|
|
|
|
packages,
|
|
|
|
created_package_ids,
|
|
|
|
)?;
|
|
|
|
graph.set_child_parent_node(name, child_node_id, node_id);
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
Ok(node_id)
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut graph = Self {
|
|
|
|
// Note: It might be more correct to store the copy index
|
|
|
|
// from past resolutions with the node somehow, but maybe not.
|
|
|
|
packages_to_copy_index: snapshot
|
|
|
|
.packages
|
|
|
|
.iter()
|
|
|
|
.map(|(id, p)| (id.clone(), p.copy_index))
|
|
|
|
.collect(),
|
2023-02-21 12:03:48 -05:00
|
|
|
package_reqs: snapshot.package_reqs,
|
2023-02-22 14:15:25 -05:00
|
|
|
pending_unresolved_packages: snapshot.pending_unresolved_packages,
|
2022-11-08 14:17:24 -05:00
|
|
|
..Default::default()
|
|
|
|
};
|
2023-02-21 12:03:48 -05:00
|
|
|
let mut created_package_ids =
|
|
|
|
HashMap::with_capacity(snapshot.packages.len());
|
|
|
|
for (id, resolved_id) in snapshot.root_packages {
|
|
|
|
let node_id = get_or_create_graph_node(
|
|
|
|
&mut graph,
|
|
|
|
&resolved_id,
|
|
|
|
&snapshot.packages,
|
|
|
|
&mut created_package_ids,
|
|
|
|
)?;
|
|
|
|
graph.root_packages.insert(id, node_id);
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
Ok(graph)
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
pub fn take_pending_unresolved(&mut self) -> Vec<NpmPackageNv> {
|
|
|
|
std::mem::take(&mut self.pending_unresolved_packages)
|
|
|
|
}
|
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
pub fn has_package_req(&self, req: &NpmPackageReq) -> bool {
|
2023-02-21 12:03:48 -05:00
|
|
|
self.package_reqs.contains_key(req)
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
pub fn has_root_package(&self, id: &NpmPackageNv) -> bool {
|
|
|
|
self.root_packages.contains_key(id)
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
fn get_npm_pkg_id(&self, node_id: NodeId) -> NpmPackageId {
|
|
|
|
let resolved_id = self.resolved_node_ids.get(node_id).unwrap();
|
|
|
|
self.get_npm_pkg_id_from_resolved_id(resolved_id, HashSet::new())
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
fn get_npm_pkg_id_from_resolved_id(
|
|
|
|
&self,
|
|
|
|
resolved_id: &ResolvedId,
|
|
|
|
seen: HashSet<NodeId>,
|
|
|
|
) -> NpmPackageId {
|
|
|
|
if resolved_id.peer_dependencies.is_empty() {
|
|
|
|
NpmPackageId {
|
|
|
|
nv: resolved_id.nv.clone(),
|
|
|
|
peer_dependencies: Vec::new(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut npm_pkg_id = NpmPackageId {
|
|
|
|
nv: resolved_id.nv.clone(),
|
|
|
|
peer_dependencies: Vec::with_capacity(
|
|
|
|
resolved_id.peer_dependencies.len(),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
let mut seen_children_resolved_ids =
|
|
|
|
HashSet::with_capacity(resolved_id.peer_dependencies.len());
|
|
|
|
for peer_dep in &resolved_id.peer_dependencies {
|
|
|
|
let maybe_node_and_resolved_id = match peer_dep {
|
|
|
|
ResolvedIdPeerDep::SnapshotNodeId(node_id) => self
|
|
|
|
.resolved_node_ids
|
|
|
|
.get(*node_id)
|
|
|
|
.map(|resolved_id| (*node_id, resolved_id)),
|
|
|
|
ResolvedIdPeerDep::ParentReference {
|
|
|
|
parent,
|
|
|
|
child_pkg_nv: child_nv,
|
|
|
|
} => match &parent {
|
|
|
|
GraphPathNodeOrRoot::Root(_) => {
|
|
|
|
self.root_packages.get(child_nv).and_then(|node_id| {
|
|
|
|
self
|
|
|
|
.resolved_node_ids
|
|
|
|
.get(*node_id)
|
|
|
|
.map(|resolved_id| (*node_id, resolved_id))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
GraphPathNodeOrRoot::Node(parent_path) => {
|
|
|
|
self.nodes.get(&parent_path.node_id()).and_then(|parent| {
|
|
|
|
parent
|
|
|
|
.children
|
|
|
|
.values()
|
|
|
|
.filter_map(|child_id| {
|
|
|
|
let child_id = *child_id;
|
|
|
|
self
|
|
|
|
.resolved_node_ids
|
|
|
|
.get(child_id)
|
|
|
|
.map(|resolved_id| (child_id, resolved_id))
|
|
|
|
})
|
|
|
|
.find(|(_, resolved_id)| resolved_id.nv == *child_nv)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
// this should always be set
|
|
|
|
debug_assert!(maybe_node_and_resolved_id.is_some());
|
|
|
|
if let Some((child_id, child_resolved_id)) = maybe_node_and_resolved_id
|
|
|
|
{
|
|
|
|
let mut new_seen = seen.clone();
|
|
|
|
if new_seen.insert(child_id) {
|
|
|
|
let child_peer = self.get_npm_pkg_id_from_resolved_id(
|
|
|
|
child_resolved_id,
|
|
|
|
new_seen.clone(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// This condition prevents a name showing up in the peer_dependencies
|
|
|
|
// list that matches the current name. Checking just the name and
|
|
|
|
// version should be sufficient because the rest of the peer dependency
|
|
|
|
// resolutions should be the same
|
|
|
|
let is_pkg_same = child_peer.nv == npm_pkg_id.nv;
|
|
|
|
if !is_pkg_same
|
|
|
|
&& seen_children_resolved_ids.insert(child_peer.clone())
|
|
|
|
{
|
|
|
|
npm_pkg_id.peer_dependencies.push(child_peer);
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
npm_pkg_id
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
fn get_or_create_for_id(
|
2022-11-08 14:17:24 -05:00
|
|
|
&mut self,
|
2023-02-21 12:03:48 -05:00
|
|
|
resolved_id: &ResolvedId,
|
|
|
|
) -> (bool, NodeId) {
|
|
|
|
if let Some(node_id) = self.resolved_node_ids.get_node_id(resolved_id) {
|
|
|
|
return (false, node_id);
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
|
|
|
|
let node_id = self.create_node(&resolved_id.nv);
|
|
|
|
self.resolved_node_ids.set(node_id, resolved_id.clone());
|
|
|
|
(true, node_id)
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
fn create_node(&mut self, pkg_nv: &NpmPackageNv) -> NodeId {
|
|
|
|
let node_id = NodeId(self.nodes.len() as u32);
|
|
|
|
let node = Node {
|
|
|
|
children: Default::default(),
|
|
|
|
no_peers: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
self
|
|
|
|
.nodes_by_package_name
|
|
|
|
.entry(pkg_nv.name.clone())
|
|
|
|
.or_default()
|
|
|
|
.push(node_id);
|
|
|
|
self.nodes.insert(node_id, node);
|
|
|
|
|
|
|
|
node_id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn borrow_node_mut(&mut self, node_id: NodeId) -> &mut Node {
|
|
|
|
self.nodes.get_mut(&node_id).unwrap()
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
fn set_child_parent_node(
|
2022-11-08 14:17:24 -05:00
|
|
|
&mut self,
|
|
|
|
specifier: &str,
|
2023-02-21 12:03:48 -05:00
|
|
|
child_id: NodeId,
|
|
|
|
parent_id: NodeId,
|
2022-11-08 14:17:24 -05:00
|
|
|
) {
|
2023-02-21 12:03:48 -05:00
|
|
|
assert_ne!(child_id, parent_id);
|
|
|
|
let parent = self.borrow_node_mut(parent_id);
|
|
|
|
parent.children.insert(specifier.to_string(), child_id);
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn into_snapshot(
|
|
|
|
self,
|
2023-02-21 12:03:48 -05:00
|
|
|
api: &NpmRegistryApi,
|
2022-11-08 14:17:24 -05:00
|
|
|
) -> Result<NpmResolutionSnapshot, AnyError> {
|
2023-02-21 12:03:48 -05:00
|
|
|
let packages_to_resolved_id = self
|
|
|
|
.nodes
|
|
|
|
.keys()
|
|
|
|
.map(|node_id| (*node_id, self.get_npm_pkg_id(*node_id)))
|
|
|
|
.collect::<HashMap<_, _>>();
|
2022-11-08 14:17:24 -05:00
|
|
|
let mut copy_index_resolver =
|
|
|
|
SnapshotPackageCopyIndexResolver::from_map_with_capacity(
|
|
|
|
self.packages_to_copy_index,
|
2023-02-21 12:03:48 -05:00
|
|
|
self.nodes.len(),
|
2022-11-08 14:17:24 -05:00
|
|
|
);
|
2023-02-21 12:03:48 -05:00
|
|
|
let mut packages = HashMap::with_capacity(self.nodes.len());
|
|
|
|
let mut traversed_node_ids = HashSet::with_capacity(self.nodes.len());
|
|
|
|
let mut pending = VecDeque::new();
|
|
|
|
for root_id in self.root_packages.values() {
|
|
|
|
if traversed_node_ids.insert(*root_id) {
|
|
|
|
pending.push_back(*root_id);
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
while let Some(node_id) = pending.pop_front() {
|
|
|
|
let node = self.nodes.get(&node_id).unwrap();
|
|
|
|
let resolved_id = packages_to_resolved_id.get(&node_id).unwrap();
|
|
|
|
// todo(dsherret): grab this from the dep entry cache, which should have it
|
2022-11-08 14:17:24 -05:00
|
|
|
let dist = api
|
2023-02-21 12:03:48 -05:00
|
|
|
.package_version_info(&resolved_id.nv)
|
2022-11-08 14:17:24 -05:00
|
|
|
.await?
|
2023-02-21 12:03:48 -05:00
|
|
|
.unwrap_or_else(|| panic!("missing: {:?}", resolved_id.nv))
|
2022-11-08 14:17:24 -05:00
|
|
|
.dist;
|
|
|
|
packages.insert(
|
2023-02-21 12:03:48 -05:00
|
|
|
(*resolved_id).clone(),
|
2022-11-08 14:17:24 -05:00
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
copy_index: copy_index_resolver.resolve(resolved_id),
|
|
|
|
pkg_id: (*resolved_id).clone(),
|
2022-11-08 14:17:24 -05:00
|
|
|
dist,
|
|
|
|
dependencies: node
|
|
|
|
.children
|
|
|
|
.iter()
|
2023-02-21 12:03:48 -05:00
|
|
|
.map(|(key, value)| {
|
|
|
|
(
|
|
|
|
key.clone(),
|
|
|
|
packages_to_resolved_id
|
|
|
|
.get(value)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
panic!("{node_id:?} -- missing child: {value:?}")
|
|
|
|
})
|
|
|
|
.clone(),
|
|
|
|
)
|
|
|
|
})
|
2022-11-08 14:17:24 -05:00
|
|
|
.collect(),
|
|
|
|
},
|
|
|
|
);
|
2023-02-21 12:03:48 -05:00
|
|
|
for child_id in node.children.values() {
|
|
|
|
if traversed_node_ids.insert(*child_id) {
|
|
|
|
pending.push_back(*child_id);
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(NpmResolutionSnapshot {
|
2023-02-21 12:03:48 -05:00
|
|
|
root_packages: self
|
|
|
|
.root_packages
|
|
|
|
.into_iter()
|
|
|
|
.map(|(id, node_id)| {
|
|
|
|
(id, packages_to_resolved_id.get(&node_id).unwrap().clone())
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
packages_by_name: self
|
|
|
|
.nodes_by_package_name
|
2022-11-08 14:17:24 -05:00
|
|
|
.into_iter()
|
2023-02-21 12:03:48 -05:00
|
|
|
.map(|(name, ids)| {
|
2023-02-21 15:19:09 -05:00
|
|
|
let mut ids = ids
|
|
|
|
.into_iter()
|
|
|
|
.filter(|id| traversed_node_ids.contains(id))
|
|
|
|
.map(|id| packages_to_resolved_id.get(&id).unwrap().clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
ids.sort();
|
|
|
|
ids.dedup();
|
|
|
|
(name, ids)
|
2022-11-08 14:17:24 -05:00
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
packages,
|
2023-02-21 12:03:48 -05:00
|
|
|
package_reqs: self.package_reqs,
|
2023-02-22 14:15:25 -05:00
|
|
|
pending_unresolved_packages: self.pending_unresolved_packages,
|
2022-11-08 14:17:24 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
// Debugging methods
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[allow(unused)]
|
|
|
|
fn output_path(&self, path: &Arc<GraphPath>) {
|
|
|
|
eprintln!("-----------");
|
|
|
|
self.output_node(path.node_id(), false);
|
|
|
|
for path in path.ancestors() {
|
|
|
|
match path {
|
|
|
|
GraphPathNodeOrRoot::Node(node) => {
|
|
|
|
self.output_node(node.node_id(), false)
|
|
|
|
}
|
|
|
|
GraphPathNodeOrRoot::Root(pkg_id) => {
|
|
|
|
let node_id = self.root_packages.get(pkg_id).unwrap();
|
|
|
|
eprintln!(
|
|
|
|
"Root: {} ({}: {})",
|
|
|
|
pkg_id,
|
|
|
|
node_id.0,
|
|
|
|
self.get_npm_pkg_id(*node_id).as_serialized()
|
2023-01-06 08:48:56 -05:00
|
|
|
)
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
eprintln!("-----------");
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[allow(unused)]
|
|
|
|
fn output_node(&self, node_id: NodeId, show_children: bool) {
|
|
|
|
eprintln!(
|
|
|
|
"{:>4}: {}",
|
|
|
|
node_id.0,
|
|
|
|
self.get_npm_pkg_id(node_id).as_serialized()
|
|
|
|
);
|
|
|
|
|
|
|
|
if show_children {
|
|
|
|
let node = self.nodes.get(&node_id).unwrap();
|
|
|
|
eprintln!(" Children:");
|
|
|
|
for (specifier, child_id) in &node.children {
|
|
|
|
eprintln!(" {}: {}", specifier, child_id.0);
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub fn output_nodes(&self) {
|
|
|
|
eprintln!("~~~");
|
|
|
|
let mut node_ids = self
|
|
|
|
.resolved_node_ids
|
|
|
|
.node_to_resolved_id
|
|
|
|
.keys()
|
|
|
|
.copied()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
node_ids.sort_by(|a, b| a.0.cmp(&b.0));
|
|
|
|
for node_id in node_ids {
|
|
|
|
self.output_node(node_id, true);
|
|
|
|
}
|
|
|
|
eprintln!("~~~");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct DepEntryCache(HashMap<NpmPackageNv, Arc<Vec<NpmDependencyEntry>>>);
|
|
|
|
|
|
|
|
impl DepEntryCache {
|
|
|
|
pub fn store(
|
|
|
|
&mut self,
|
|
|
|
nv: NpmPackageNv,
|
|
|
|
version_info: &NpmPackageVersionInfo,
|
|
|
|
) -> Result<Arc<Vec<NpmDependencyEntry>>, AnyError> {
|
|
|
|
debug_assert!(!self.0.contains_key(&nv)); // we should not be re-inserting
|
|
|
|
let mut deps = version_info
|
|
|
|
.dependencies_as_entries()
|
|
|
|
.with_context(|| format!("npm package: {nv}"))?;
|
|
|
|
// Ensure name alphabetical and then version descending
|
|
|
|
// so these are resolved in that order
|
|
|
|
deps.sort();
|
|
|
|
let deps = Arc::new(deps);
|
|
|
|
self.0.insert(nv, deps.clone());
|
|
|
|
Ok(deps)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(
|
|
|
|
&self,
|
|
|
|
id: &NpmPackageNv,
|
|
|
|
) -> Option<&Arc<Vec<NpmDependencyEntry>>> {
|
|
|
|
self.0.get(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UnresolvedOptionalPeer {
|
|
|
|
specifier: String,
|
|
|
|
graph_path: Arc<GraphPath>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GraphDependencyResolver<'a> {
|
|
|
|
graph: &'a mut Graph,
|
|
|
|
api: &'a NpmRegistryApi,
|
|
|
|
pending_unresolved_nodes: VecDeque<Arc<GraphPath>>,
|
|
|
|
unresolved_optional_peers: HashMap<NpmPackageNv, Vec<UnresolvedOptionalPeer>>,
|
|
|
|
dep_entry_cache: DepEntryCache,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> GraphDependencyResolver<'a> {
|
|
|
|
pub fn new(graph: &'a mut Graph, api: &'a NpmRegistryApi) -> Self {
|
|
|
|
Self {
|
|
|
|
graph,
|
|
|
|
api,
|
|
|
|
pending_unresolved_nodes: Default::default(),
|
|
|
|
unresolved_optional_peers: Default::default(),
|
|
|
|
dep_entry_cache: Default::default(),
|
|
|
|
}
|
2022-11-11 11:33:57 -05:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
pub fn add_root_package(
|
|
|
|
&mut self,
|
|
|
|
package_nv: &NpmPackageNv,
|
|
|
|
package_info: &NpmPackageInfo,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if self.graph.root_packages.contains_key(package_nv) {
|
|
|
|
return Ok(()); // already added
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo(dsherret): using a version requirement here is a temporary hack
|
|
|
|
// to reuse code in a large refactor. We should resolve the node directly
|
|
|
|
// from the package name and version
|
|
|
|
let version_req =
|
|
|
|
VersionReq::parse_from_specifier(&format!("{}", package_nv.version))
|
|
|
|
.unwrap();
|
|
|
|
let (pkg_id, node_id) = self.resolve_node_from_info(
|
|
|
|
&package_nv.name,
|
|
|
|
&version_req,
|
|
|
|
package_info,
|
|
|
|
None,
|
|
|
|
)?;
|
|
|
|
self.graph.root_packages.insert(pkg_id.clone(), node_id);
|
|
|
|
self
|
|
|
|
.pending_unresolved_nodes
|
|
|
|
.push_back(GraphPath::for_root(node_id, pkg_id));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
pub fn add_package_req(
|
|
|
|
&mut self,
|
|
|
|
package_req: &NpmPackageReq,
|
2022-11-11 11:33:57 -05:00
|
|
|
package_info: &NpmPackageInfo,
|
2022-11-08 14:17:24 -05:00
|
|
|
) -> Result<(), AnyError> {
|
2023-02-22 14:15:25 -05:00
|
|
|
if self.graph.package_reqs.contains_key(package_req) {
|
|
|
|
return Ok(()); // already added
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
let (pkg_id, node_id) = self.resolve_node_from_info(
|
2022-11-08 14:17:24 -05:00
|
|
|
&package_req.name,
|
2023-01-31 21:27:40 -05:00
|
|
|
package_req
|
|
|
|
.version_req
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&*LATEST_VERSION_REQ),
|
2022-11-08 14:17:24 -05:00
|
|
|
package_info,
|
2022-11-14 21:22:59 -05:00
|
|
|
None,
|
2022-11-08 14:17:24 -05:00
|
|
|
)?;
|
2023-02-21 12:03:48 -05:00
|
|
|
self
|
|
|
|
.graph
|
|
|
|
.package_reqs
|
|
|
|
.insert(package_req.clone(), pkg_id.clone());
|
|
|
|
self.graph.root_packages.insert(pkg_id.clone(), node_id);
|
|
|
|
self
|
|
|
|
.pending_unresolved_nodes
|
|
|
|
.push_back(GraphPath::for_root(node_id, pkg_id));
|
2022-11-08 14:17:24 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn analyze_dependency(
|
|
|
|
&mut self,
|
|
|
|
entry: &NpmDependencyEntry,
|
2022-11-11 11:33:57 -05:00
|
|
|
package_info: &NpmPackageInfo,
|
2023-02-21 12:03:48 -05:00
|
|
|
graph_path: &Arc<GraphPath>,
|
|
|
|
) -> Result<NodeId, AnyError> {
|
|
|
|
debug_assert_eq!(entry.kind, NpmDependencyEntryKind::Dep);
|
|
|
|
let parent_id = graph_path.node_id();
|
|
|
|
let (_, node_id) = self.resolve_node_from_info(
|
2022-11-08 14:17:24 -05:00
|
|
|
&entry.name,
|
2023-02-21 12:03:48 -05:00
|
|
|
&entry.version_req,
|
2022-11-08 14:17:24 -05:00
|
|
|
package_info,
|
2022-11-14 21:22:59 -05:00
|
|
|
Some(parent_id),
|
2022-11-08 14:17:24 -05:00
|
|
|
)?;
|
2023-01-14 15:10:07 -05:00
|
|
|
// Some packages may resolves to themselves as a dependency. If this occurs,
|
|
|
|
// just ignore adding these as dependencies because this is likely a mistake
|
|
|
|
// in the package.
|
2023-02-21 12:03:48 -05:00
|
|
|
if node_id != parent_id {
|
|
|
|
self.graph.set_child_parent_node(
|
|
|
|
&entry.bare_specifier,
|
|
|
|
node_id,
|
|
|
|
parent_id,
|
|
|
|
);
|
|
|
|
self.try_add_pending_unresolved_node(
|
|
|
|
graph_path,
|
|
|
|
node_id,
|
2023-01-14 15:10:07 -05:00
|
|
|
&entry.bare_specifier,
|
|
|
|
);
|
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
Ok(node_id)
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn try_add_pending_unresolved_node(
|
|
|
|
&mut self,
|
2023-02-21 12:03:48 -05:00
|
|
|
path: &Arc<GraphPath>,
|
|
|
|
node_id: NodeId,
|
|
|
|
specifier: &str,
|
2022-11-08 14:17:24 -05:00
|
|
|
) {
|
2023-02-21 12:03:48 -05:00
|
|
|
if self.graph.nodes.get(&node_id).unwrap().no_peers {
|
|
|
|
return; // skip, no need to analyze this again
|
|
|
|
}
|
|
|
|
let node_nv = self
|
|
|
|
.graph
|
|
|
|
.resolved_node_ids
|
|
|
|
.get(node_id)
|
|
|
|
.unwrap()
|
|
|
|
.nv
|
|
|
|
.clone();
|
|
|
|
let new_path = match path.with_id(node_id, specifier, node_nv) {
|
|
|
|
Some(visited_versions) => visited_versions,
|
|
|
|
None => return, // circular, don't visit this node
|
2022-11-08 14:17:24 -05:00
|
|
|
};
|
2023-02-21 12:03:48 -05:00
|
|
|
self.pending_unresolved_nodes.push_back(new_path);
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_node_from_info(
|
|
|
|
&mut self,
|
2023-01-06 08:48:56 -05:00
|
|
|
pkg_req_name: &str,
|
2023-01-31 21:27:40 -05:00
|
|
|
version_req: &VersionReq,
|
2022-11-11 11:33:57 -05:00
|
|
|
package_info: &NpmPackageInfo,
|
2023-02-21 12:03:48 -05:00
|
|
|
parent_id: Option<NodeId>,
|
|
|
|
) -> Result<(NpmPackageNv, NodeId), AnyError> {
|
|
|
|
let version_and_info = resolve_best_package_version_and_info(
|
|
|
|
version_req,
|
|
|
|
package_info,
|
|
|
|
self
|
|
|
|
.graph
|
|
|
|
.nodes_by_package_name
|
|
|
|
.entry(package_info.name.clone())
|
|
|
|
.or_default()
|
|
|
|
.iter()
|
|
|
|
.map(|node_id| {
|
|
|
|
&self
|
|
|
|
.graph
|
|
|
|
.resolved_node_ids
|
|
|
|
.get(*node_id)
|
|
|
|
.unwrap()
|
|
|
|
.nv
|
|
|
|
.version
|
|
|
|
}),
|
|
|
|
)?;
|
|
|
|
let resolved_id = ResolvedId {
|
|
|
|
nv: NpmPackageNv {
|
|
|
|
name: package_info.name.to_string(),
|
|
|
|
version: version_and_info.version.clone(),
|
|
|
|
},
|
2022-11-08 14:17:24 -05:00
|
|
|
peer_dependencies: Vec::new(),
|
|
|
|
};
|
2023-02-21 12:03:48 -05:00
|
|
|
let (_, node_id) = self.graph.get_or_create_for_id(&resolved_id);
|
|
|
|
let pkg_id = resolved_id.nv;
|
|
|
|
|
|
|
|
let has_deps = if let Some(deps) = self.dep_entry_cache.get(&pkg_id) {
|
|
|
|
!deps.is_empty()
|
|
|
|
} else {
|
|
|
|
let deps = self
|
|
|
|
.dep_entry_cache
|
|
|
|
.store(pkg_id.clone(), version_and_info.info)?;
|
|
|
|
!deps.is_empty()
|
|
|
|
};
|
|
|
|
|
|
|
|
if !has_deps {
|
|
|
|
// ensure this is set if not, as it's an optimization
|
|
|
|
let mut node = self.graph.borrow_node_mut(node_id);
|
|
|
|
node.no_peers = true;
|
|
|
|
}
|
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
debug!(
|
2022-11-14 21:22:59 -05:00
|
|
|
"{} - Resolved {}@{} to {}",
|
|
|
|
match parent_id {
|
2023-02-21 12:03:48 -05:00
|
|
|
Some(parent_id) => self.graph.get_npm_pkg_id(parent_id).as_serialized(),
|
2022-11-14 21:22:59 -05:00
|
|
|
None => "<package-req>".to_string(),
|
|
|
|
},
|
2023-01-06 08:48:56 -05:00
|
|
|
pkg_req_name,
|
2023-01-31 21:27:40 -05:00
|
|
|
version_req.version_text(),
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id.to_string(),
|
|
|
|
);
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
Ok((pkg_id, node_id))
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn resolve_pending(&mut self) -> Result<(), AnyError> {
|
|
|
|
while !self.pending_unresolved_nodes.is_empty() {
|
|
|
|
// now go down through the dependencies by tree depth
|
2023-02-21 12:03:48 -05:00
|
|
|
while let Some(graph_path) = self.pending_unresolved_nodes.pop_front() {
|
|
|
|
let (pkg_id, deps) = {
|
|
|
|
let node_id = graph_path.node_id();
|
|
|
|
if self.graph.nodes.get(&node_id).unwrap().no_peers {
|
|
|
|
// We can skip as there's no reason to analyze this graph segment further
|
|
|
|
// Note that we don't need to count parent references here because that's
|
|
|
|
// only necessary for graph segments that could potentially have peer
|
|
|
|
// dependencies within them.
|
2022-11-08 14:17:24 -05:00
|
|
|
continue;
|
|
|
|
}
|
2022-11-14 21:22:59 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
let pkg_nv = self
|
|
|
|
.graph
|
|
|
|
.resolved_node_ids
|
|
|
|
.get(node_id)
|
|
|
|
.unwrap()
|
|
|
|
.nv
|
|
|
|
.clone();
|
|
|
|
let deps = if let Some(deps) = self.dep_entry_cache.get(&pkg_nv) {
|
|
|
|
deps.clone()
|
|
|
|
} else {
|
|
|
|
// the api should have this in the cache at this point, so no need to parallelize
|
|
|
|
match self.api.package_version_info(&pkg_nv).await? {
|
|
|
|
Some(version_info) => {
|
|
|
|
self.dep_entry_cache.store(pkg_nv.clone(), &version_info)?
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bail!("Could not find version information for {}", pkg_nv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
(pkg_nv, deps)
|
|
|
|
};
|
2022-11-08 14:17:24 -05:00
|
|
|
|
|
|
|
// cache all the dependencies' registry infos in parallel if should
|
2023-02-21 12:03:48 -05:00
|
|
|
self
|
|
|
|
.api
|
|
|
|
.cache_in_parallel({
|
|
|
|
deps.iter().map(|dep| dep.name.clone()).collect()
|
|
|
|
})
|
|
|
|
.await?;
|
2022-11-08 14:17:24 -05:00
|
|
|
|
|
|
|
// resolve the dependencies
|
2022-11-17 21:50:43 -05:00
|
|
|
let mut found_peer = false;
|
2023-02-21 12:03:48 -05:00
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
for dep in deps.iter() {
|
|
|
|
let package_info = self.api.package_info(&dep.name).await?;
|
|
|
|
|
|
|
|
match dep.kind {
|
|
|
|
NpmDependencyEntryKind::Dep => {
|
2023-02-21 12:03:48 -05:00
|
|
|
// todo(dsherret): look into skipping dependency analysis if
|
|
|
|
// it was done previously again
|
|
|
|
let child_id =
|
|
|
|
self.analyze_dependency(dep, &package_info, &graph_path)?;
|
|
|
|
|
2022-11-17 21:50:43 -05:00
|
|
|
if !found_peer {
|
2023-02-21 12:03:48 -05:00
|
|
|
found_peer = !self.graph.borrow_node_mut(child_id).no_peers;
|
2022-11-17 21:50:43 -05:00
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
NpmDependencyEntryKind::Peer
|
|
|
|
| NpmDependencyEntryKind::OptionalPeer => {
|
2022-11-17 21:50:43 -05:00
|
|
|
found_peer = true;
|
2023-02-21 12:03:48 -05:00
|
|
|
// we need to re-evaluate peer dependencies every time and can't
|
|
|
|
// skip over them because they might be evaluated differently based
|
|
|
|
// on the current path
|
|
|
|
let maybe_new_id = self.resolve_peer_dep(
|
2022-11-08 14:17:24 -05:00
|
|
|
&dep.bare_specifier,
|
|
|
|
dep,
|
2022-11-11 11:33:57 -05:00
|
|
|
&package_info,
|
2023-02-21 12:03:48 -05:00
|
|
|
&graph_path,
|
2022-11-08 14:17:24 -05:00
|
|
|
)?;
|
2023-02-21 12:03:48 -05:00
|
|
|
|
|
|
|
// For optional dependencies, we want to resolve them if any future
|
|
|
|
// same parent version resolves them. So when not resolved, store them to be
|
|
|
|
// potentially resolved later.
|
|
|
|
//
|
|
|
|
// Note: This is not a good solution, but will probably work ok in most
|
|
|
|
// scenarios. We can work on improving this in the future. We probably
|
|
|
|
// want to resolve future optional peers to the same dependency for example.
|
|
|
|
if dep.kind == NpmDependencyEntryKind::OptionalPeer {
|
|
|
|
match maybe_new_id {
|
|
|
|
Some(new_id) => {
|
|
|
|
if let Some(unresolved_optional_peers) =
|
|
|
|
self.unresolved_optional_peers.remove(&pkg_id)
|
|
|
|
{
|
|
|
|
for optional_peer in unresolved_optional_peers {
|
|
|
|
let peer_parent = GraphPathNodeOrRoot::Node(
|
|
|
|
optional_peer.graph_path.clone(),
|
|
|
|
);
|
|
|
|
self.set_new_peer_dep(
|
|
|
|
vec![&optional_peer.graph_path],
|
|
|
|
peer_parent,
|
|
|
|
&optional_peer.specifier,
|
|
|
|
new_id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// store this for later if it's resolved for this version
|
|
|
|
self
|
|
|
|
.unresolved_optional_peers
|
|
|
|
.entry(pkg_id.clone())
|
|
|
|
.or_default()
|
|
|
|
.push(UnresolvedOptionalPeer {
|
|
|
|
specifier: dep.bare_specifier.clone(),
|
|
|
|
graph_path: graph_path.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 21:50:43 -05:00
|
|
|
|
|
|
|
if !found_peer {
|
2023-02-21 12:03:48 -05:00
|
|
|
self.graph.borrow_node_mut(graph_path.node_id()).no_peers = true;
|
2022-11-17 21:50:43 -05:00
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_peer_dep(
|
|
|
|
&mut self,
|
|
|
|
specifier: &str,
|
|
|
|
peer_dep: &NpmDependencyEntry,
|
2022-11-11 11:33:57 -05:00
|
|
|
peer_package_info: &NpmPackageInfo,
|
2023-02-21 12:03:48 -05:00
|
|
|
ancestor_path: &Arc<GraphPath>,
|
|
|
|
) -> Result<Option<NodeId>, AnyError> {
|
|
|
|
debug_assert!(matches!(
|
|
|
|
peer_dep.kind,
|
|
|
|
NpmDependencyEntryKind::Peer | NpmDependencyEntryKind::OptionalPeer
|
|
|
|
));
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
let mut path = vec![ancestor_path];
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
// the current dependency might have had the peer dependency
|
|
|
|
// in another bare specifier slot... if so resolve it to that
|
2022-11-08 14:17:24 -05:00
|
|
|
{
|
2023-02-21 12:03:48 -05:00
|
|
|
let maybe_peer_dep = self.find_peer_dep_in_node(
|
|
|
|
ancestor_path,
|
|
|
|
peer_dep,
|
|
|
|
peer_package_info,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
if let Some((peer_parent, peer_dep_id)) = maybe_peer_dep {
|
|
|
|
// this will always have an ancestor because we're not at the root
|
|
|
|
self.set_new_peer_dep(path, peer_parent, specifier, peer_dep_id);
|
|
|
|
return Ok(Some(peer_dep_id));
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
// Peer dependencies are resolved based on its ancestors' siblings.
|
|
|
|
// If not found, then it resolves based on the version requirement if non-optional.
|
|
|
|
for ancestor_node in ancestor_path.ancestors() {
|
|
|
|
match ancestor_node {
|
|
|
|
GraphPathNodeOrRoot::Node(ancestor_graph_path_node) => {
|
|
|
|
path.push(ancestor_graph_path_node);
|
|
|
|
let maybe_peer_dep = self.find_peer_dep_in_node(
|
|
|
|
ancestor_graph_path_node,
|
|
|
|
peer_dep,
|
|
|
|
peer_package_info,
|
|
|
|
)?;
|
|
|
|
if let Some((parent, peer_dep_id)) = maybe_peer_dep {
|
|
|
|
// this will always have an ancestor because we're not at the root
|
|
|
|
self.set_new_peer_dep(path, parent, specifier, peer_dep_id);
|
|
|
|
return Ok(Some(peer_dep_id));
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
GraphPathNodeOrRoot::Root(root_pkg_id) => {
|
2022-11-08 14:17:24 -05:00
|
|
|
// in this case, the parent is the root so the children are all the package requirements
|
2022-11-15 20:52:27 -05:00
|
|
|
if let Some(child_id) = find_matching_child(
|
|
|
|
peer_dep,
|
|
|
|
peer_package_info,
|
2023-02-21 12:03:48 -05:00
|
|
|
self
|
|
|
|
.graph
|
|
|
|
.root_packages
|
|
|
|
.iter()
|
|
|
|
.map(|(pkg_id, id)| (*id, pkg_id)),
|
2022-11-15 20:52:27 -05:00
|
|
|
)? {
|
2023-02-21 12:03:48 -05:00
|
|
|
let peer_parent = GraphPathNodeOrRoot::Root(root_pkg_id.clone());
|
|
|
|
self.set_new_peer_dep(path, peer_parent, specifier, child_id);
|
|
|
|
return Ok(Some(child_id));
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find anything by searching the ancestor siblings, so we need
|
2023-02-21 12:03:48 -05:00
|
|
|
// to resolve based on the package info
|
|
|
|
if !peer_dep.kind.is_optional() {
|
|
|
|
let parent_id = ancestor_path.node_id();
|
|
|
|
let (_, node_id) = self.resolve_node_from_info(
|
|
|
|
&peer_dep.name,
|
|
|
|
peer_dep
|
|
|
|
.peer_dep_version_req
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&peer_dep.version_req),
|
2022-11-08 14:17:24 -05:00
|
|
|
peer_package_info,
|
2023-02-21 12:03:48 -05:00
|
|
|
Some(parent_id),
|
2022-11-08 14:17:24 -05:00
|
|
|
)?;
|
2023-02-21 12:03:48 -05:00
|
|
|
let peer_parent = GraphPathNodeOrRoot::Node(ancestor_path.clone());
|
|
|
|
self.set_new_peer_dep(
|
|
|
|
vec![ancestor_path],
|
|
|
|
peer_parent,
|
|
|
|
specifier,
|
|
|
|
node_id,
|
|
|
|
);
|
|
|
|
Ok(Some(node_id))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
fn find_peer_dep_in_node(
|
|
|
|
&self,
|
|
|
|
path: &Arc<GraphPath>,
|
2023-01-06 09:13:21 -05:00
|
|
|
peer_dep: &NpmDependencyEntry,
|
2023-02-21 12:03:48 -05:00
|
|
|
peer_package_info: &NpmPackageInfo,
|
|
|
|
) -> Result<Option<(GraphPathNodeOrRoot, NodeId)>, AnyError> {
|
|
|
|
let node_id = path.node_id();
|
|
|
|
let resolved_node_id = self.graph.resolved_node_ids.get(node_id).unwrap();
|
|
|
|
// check if this node itself is a match for
|
|
|
|
// the peer dependency and if so use that
|
|
|
|
if resolved_node_id.nv.name == peer_dep.name
|
|
|
|
&& version_req_satisfies(
|
|
|
|
&peer_dep.version_req,
|
|
|
|
&resolved_node_id.nv.version,
|
|
|
|
peer_package_info,
|
|
|
|
None,
|
|
|
|
)?
|
|
|
|
{
|
|
|
|
let parent = path.previous_node.as_ref().unwrap().clone();
|
|
|
|
Ok(Some((parent, node_id)))
|
|
|
|
} else {
|
|
|
|
let node = self.graph.nodes.get(&node_id).unwrap();
|
|
|
|
let children = node.children.values().map(|child_node_id| {
|
|
|
|
let child_node_id = *child_node_id;
|
|
|
|
(
|
|
|
|
child_node_id,
|
|
|
|
&self.graph.resolved_node_ids.get(child_node_id).unwrap().nv,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
find_matching_child(peer_dep, peer_package_info, children).map(
|
|
|
|
|maybe_child_id| {
|
|
|
|
maybe_child_id.map(|child_id| {
|
|
|
|
let parent = GraphPathNodeOrRoot::Node(path.clone());
|
|
|
|
(parent, child_id)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-01-06 09:13:21 -05:00
|
|
|
}
|
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
fn set_new_peer_dep(
|
|
|
|
&mut self,
|
2023-02-21 12:03:48 -05:00
|
|
|
// path from the node above the resolved dep to just above the peer dep
|
|
|
|
path: Vec<&Arc<GraphPath>>,
|
|
|
|
peer_dep_parent: GraphPathNodeOrRoot,
|
|
|
|
peer_dep_specifier: &str,
|
|
|
|
mut peer_dep_id: NodeId,
|
|
|
|
) {
|
|
|
|
debug_assert!(!path.is_empty());
|
|
|
|
let peer_dep_pkg_id = self
|
|
|
|
.graph
|
|
|
|
.resolved_node_ids
|
|
|
|
.get(peer_dep_id)
|
|
|
|
.unwrap()
|
|
|
|
.nv
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
let peer_dep = ResolvedIdPeerDep::ParentReference {
|
|
|
|
parent: peer_dep_parent,
|
|
|
|
child_pkg_nv: peer_dep_pkg_id,
|
|
|
|
};
|
|
|
|
for graph_path_node in path.iter().rev() {
|
|
|
|
let old_node_id = graph_path_node.node_id();
|
|
|
|
let old_resolved_id = self
|
|
|
|
.graph
|
|
|
|
.resolved_node_ids
|
|
|
|
.get(old_node_id)
|
|
|
|
.unwrap()
|
|
|
|
.clone();
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
let mut new_resolved_id = old_resolved_id.clone();
|
|
|
|
new_resolved_id.push_peer_dep(peer_dep.clone());
|
|
|
|
let (created, new_node_id) =
|
|
|
|
self.graph.get_or_create_for_id(&new_resolved_id);
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
// this will occur when the peer dependency is in an ancestor
|
|
|
|
if old_node_id == peer_dep_id {
|
|
|
|
peer_dep_id = new_node_id;
|
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
if created {
|
|
|
|
let old_children =
|
|
|
|
self.graph.borrow_node_mut(old_node_id).children.clone();
|
|
|
|
// copy over the old children to this new one
|
|
|
|
for (specifier, child_id) in &old_children {
|
2022-11-08 14:17:24 -05:00
|
|
|
self
|
|
|
|
.graph
|
2023-02-21 12:03:48 -05:00
|
|
|
.set_child_parent_node(specifier, *child_id, new_node_id);
|
2022-11-14 21:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
debug_assert_eq!(graph_path_node.node_id(), old_node_id);
|
|
|
|
graph_path_node.change_id(new_node_id);
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
// update the previous parent to have this as its child
|
|
|
|
match graph_path_node.previous_node.as_ref().unwrap() {
|
|
|
|
GraphPathNodeOrRoot::Root(pkg_id) => {
|
|
|
|
self.graph.root_packages.insert(pkg_id.clone(), new_node_id);
|
|
|
|
}
|
|
|
|
GraphPathNodeOrRoot::Node(parent_node_path) => {
|
|
|
|
let parent_node_id = parent_node_path.node_id();
|
|
|
|
let parent_node = self.graph.borrow_node_mut(parent_node_id);
|
|
|
|
parent_node
|
|
|
|
.children
|
|
|
|
.insert(graph_path_node.specifier().to_string(), new_node_id);
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
// now set the peer dependency
|
|
|
|
let bottom_node = path.first().unwrap();
|
|
|
|
let parent_node_id = bottom_node.node_id();
|
|
|
|
self.graph.set_child_parent_node(
|
|
|
|
peer_dep_specifier,
|
|
|
|
peer_dep_id,
|
|
|
|
parent_node_id,
|
|
|
|
);
|
2022-11-15 20:52:27 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
// mark the peer dependency to be analyzed
|
|
|
|
self.try_add_pending_unresolved_node(
|
|
|
|
bottom_node,
|
|
|
|
peer_dep_id,
|
|
|
|
peer_dep_specifier,
|
|
|
|
);
|
|
|
|
|
|
|
|
debug!(
|
|
|
|
"Resolved peer dependency for {} in {} to {}",
|
|
|
|
peer_dep_specifier,
|
|
|
|
&self.graph.get_npm_pkg_id(parent_node_id).as_serialized(),
|
|
|
|
&self.graph.get_npm_pkg_id(peer_dep_id).as_serialized(),
|
|
|
|
);
|
2022-11-15 20:52:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
fn find_matching_child<'a>(
|
|
|
|
peer_dep: &NpmDependencyEntry,
|
|
|
|
peer_package_info: &NpmPackageInfo,
|
|
|
|
children: impl Iterator<Item = (NodeId, &'a NpmPackageNv)>,
|
|
|
|
) -> Result<Option<NodeId>, AnyError> {
|
|
|
|
for (child_id, pkg_id) in children {
|
|
|
|
if pkg_id.name == peer_dep.name
|
|
|
|
&& version_req_satisfies(
|
|
|
|
&peer_dep.version_req,
|
|
|
|
&pkg_id.version,
|
|
|
|
peer_package_info,
|
|
|
|
None,
|
|
|
|
)?
|
|
|
|
{
|
|
|
|
return Ok(Some(child_id));
|
2022-11-15 20:52:27 -05:00
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
Ok(None)
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2023-02-21 12:03:48 -05:00
|
|
|
use deno_graph::npm::NpmPackageReqReference;
|
2022-11-08 14:17:24 -05:00
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
use crate::npm::registry::TestNpmRegistryApiInner;
|
2022-11-08 14:17:24 -05:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2023-02-21 12:03:48 -05:00
|
|
|
fn resolved_id_tests() {
|
|
|
|
let mut ids = ResolvedNodeIds::default();
|
|
|
|
let node_id = NodeId(0);
|
|
|
|
let resolved_id = ResolvedId {
|
|
|
|
nv: NpmPackageNv::from_str("package@1.1.1").unwrap(),
|
|
|
|
peer_dependencies: Vec::new(),
|
2022-11-15 20:52:27 -05:00
|
|
|
};
|
2023-02-21 12:03:48 -05:00
|
|
|
ids.set(node_id, resolved_id.clone());
|
|
|
|
assert!(ids.get(node_id).is_some());
|
|
|
|
assert!(ids.get(NodeId(1)).is_none());
|
|
|
|
assert_eq!(ids.get_node_id(&resolved_id), Some(node_id));
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
let resolved_id_new = ResolvedId {
|
|
|
|
nv: NpmPackageNv::from_str("package@1.1.2").unwrap(),
|
|
|
|
peer_dependencies: Vec::new(),
|
2022-11-15 20:52:27 -05:00
|
|
|
};
|
2023-02-21 12:03:48 -05:00
|
|
|
ids.set(node_id, resolved_id_new.clone());
|
|
|
|
assert_eq!(ids.get_node_id(&resolved_id), None); // stale entry should have been removed
|
|
|
|
assert!(ids.get(node_id).is_some());
|
|
|
|
assert_eq!(ids.get_node_id(&resolved_id_new), Some(node_id));
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_deps_no_peer() {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-08 14:17:24 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.ensure_package_version("package-c", "0.1.0");
|
|
|
|
api.ensure_package_version("package-c", "0.0.10");
|
|
|
|
api.ensure_package_version("package-d", "3.2.1");
|
|
|
|
api.ensure_package_version("package-d", "3.2.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "^2"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-c", "^0.1"));
|
|
|
|
api.add_dependency(("package-c", "0.1.0"), ("package-d", "*"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-a@1"]).await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-b@2.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-c".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-c@0.1.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-b@2.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-c@0.1.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-d".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-d@3.2.1").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)])
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-d@3.2.1").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![("package-a@1".to_string(), "package-a@1.0.0".to_string())]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_deps_circular() {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-08 14:17:24 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "*"));
|
|
|
|
api.add_dependency(("package-b", "2.0.0"), ("package-a", "1"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-a@1.0"]).await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-b@2.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-b@2.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-a".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![("package-a@1.0".to_string(), "package-a@1.0.0".to_string())]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-02-21 12:03:48 -05:00
|
|
|
async fn peer_deps_simple_top_tree() {
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-08 14:17:24 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
2023-02-21 12:03:48 -05:00
|
|
|
api.ensure_package_version("package-b", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "1.0.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "1"));
|
|
|
|
api.add_peer_dependency(("package-b", "1.0.0"), ("package-peer", "*"));
|
2022-11-08 14:17:24 -05:00
|
|
|
|
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
2023-02-21 12:03:48 -05:00
|
|
|
vec!["npm:package-a@1.0", "npm:package-peer@1.0"],
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
2023-02-21 12:03:48 -05:00
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-b".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-b@1.0.0_package-peer@1.0.0")
|
2022-11-08 14:17:24 -05:00
|
|
|
.unwrap(),
|
2023-02-21 12:03:48 -05:00
|
|
|
)]),
|
2022-11-08 14:17:24 -05:00
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@1.0.0_package-peer@1.0.0"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
|
|
|
)]),
|
2022-11-08 14:17:24 -05:00
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: Default::default(),
|
2023-02-21 12:03:48 -05:00
|
|
|
dist: Default::default(),
|
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![
|
|
|
|
(
|
2023-02-21 12:03:48 -05:00
|
|
|
"package-a@1.0".to_string(),
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0".to_string()
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
(
|
2023-02-21 12:03:48 -05:00
|
|
|
"package-peer@1.0".to_string(),
|
|
|
|
"package-peer@1.0.0".to_string()
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-02-21 12:03:48 -05:00
|
|
|
async fn peer_deps_simple_root_pkg_children() {
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
|
|
|
api.ensure_package_version("package-0", "1.0.0");
|
2022-11-08 14:17:24 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
2023-02-21 12:03:48 -05:00
|
|
|
api.ensure_package_version("package-b", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "1.0.0");
|
|
|
|
api.add_dependency(("package-0", "1.0.0"), ("package-a", "1"));
|
|
|
|
api.add_dependency(("package-0", "1.0.0"), ("package-peer", "1"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "1"));
|
|
|
|
api.add_peer_dependency(("package-b", "1.0.0"), ("package-peer", "*"));
|
2022-11-08 14:17:24 -05:00
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
2023-02-21 12:03:48 -05:00
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-0@1.0"]).await;
|
2022-11-08 14:17:24 -05:00
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-0@1.0.0_package-peer@1.0.0"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
2023-02-21 12:03:48 -05:00
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
|
|
|
)
|
2022-11-08 14:17:24 -05:00
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
2023-02-21 12:03:48 -05:00
|
|
|
"package-b".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-b@1.0.0_package-peer@1.0.0")
|
|
|
|
.unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
2022-11-08 14:17:24 -05:00
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@1.0.0_package-peer@1.0.0"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
2022-11-08 14:17:24 -05:00
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: Default::default(),
|
2023-02-21 12:03:48 -05:00
|
|
|
dist: Default::default(),
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![(
|
|
|
|
"package-0@1.0".to_string(),
|
|
|
|
"package-0@1.0.0_package-peer@1.0.0".to_string()
|
|
|
|
),]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn peer_deps_simple_deeper() {
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
|
|
|
api.ensure_package_version("package-0", "1.0.0");
|
|
|
|
api.ensure_package_version("package-1", "1.0.0");
|
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "1.0.0");
|
|
|
|
api.add_dependency(("package-0", "1.0.0"), ("package-1", "1"));
|
|
|
|
api.add_dependency(("package-1", "1.0.0"), ("package-a", "1"));
|
|
|
|
api.add_dependency(("package-1", "1.0.0"), ("package-peer", "1"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "1"));
|
|
|
|
api.add_peer_dependency(("package-b", "1.0.0"), ("package-peer", "*"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-0@1.0"]).await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-0@1.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-1".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-1@1.0.0_package-peer@1.0.0")
|
|
|
|
.unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-1@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-b".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-b@1.0.0_package-peer@1.0.0")
|
|
|
|
.unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
2022-11-08 14:17:24 -05:00
|
|
|
},
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: Default::default(),
|
|
|
|
dist: Default::default(),
|
|
|
|
}
|
2022-11-08 14:17:24 -05:00
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
2023-02-21 12:03:48 -05:00
|
|
|
vec![("package-0@1.0".to_string(), "package-0@1.0.0".to_string()),]
|
2022-11-08 14:17:24 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-02-21 12:03:48 -05:00
|
|
|
async fn resolve_with_peer_deps_top_tree() {
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-08 14:17:24 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.ensure_package_version("package-c", "3.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "4.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "4.1.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "^2"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-c", "^3"));
|
|
|
|
api.add_peer_dependency(("package-b", "2.0.0"), ("package-peer", "4"));
|
|
|
|
api.add_peer_dependency(("package-c", "3.0.0"), ("package-peer", "*"));
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
|
|
|
// the peer dependency is specified here at the top of the tree
|
|
|
|
// so it should resolve to 4.0.0 instead of 4.1.0
|
|
|
|
vec!["npm:package-a@1", "npm:package-peer@4.0.0"],
|
|
|
|
)
|
|
|
|
.await;
|
2022-11-08 14:17:24 -05:00
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-b@2.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-c".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-c@3.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@2.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@4.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)])
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-c@3.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@4.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)])
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@4.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
2023-02-21 12:03:48 -05:00
|
|
|
vec![
|
|
|
|
(
|
|
|
|
"package-a@1".to_string(),
|
|
|
|
"package-a@1.0.0_package-peer@4.0.0".to_string()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer@4.0.0".to_string(),
|
|
|
|
"package-peer@4.0.0".to_string()
|
|
|
|
)
|
|
|
|
]
|
2022-11-08 14:17:24 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-02-21 12:03:48 -05:00
|
|
|
async fn resolve_with_peer_deps_ancestor_sibling_not_top_tree() {
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
|
|
|
api.ensure_package_version("package-0", "1.1.1");
|
2022-11-08 14:17:24 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.ensure_package_version("package-c", "3.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "4.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "4.1.0");
|
2023-02-21 12:03:48 -05:00
|
|
|
api.add_dependency(("package-0", "1.1.1"), ("package-a", "1"));
|
2022-11-08 14:17:24 -05:00
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "^2"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-c", "^3"));
|
2023-02-21 12:03:48 -05:00
|
|
|
// the peer dependency is specified here as a sibling of "a" and "b"
|
|
|
|
// so it should resolve to 4.0.0 instead of 4.1.0
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-peer", "4.0.0"));
|
|
|
|
api.add_peer_dependency(("package-b", "2.0.0"), ("package-peer", "4"));
|
|
|
|
api.add_peer_dependency(("package-c", "3.0.0"), ("package-peer", "*"));
|
2022-11-08 14:17:24 -05:00
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
2023-02-21 12:03:48 -05:00
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-0@1.1.1"]).await;
|
2022-11-08 14:17:24 -05:00
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-0@1.1.1").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0_package-peer@4.0.0")
|
|
|
|
.unwrap(),
|
|
|
|
),]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-b@2.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-c".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-c@3.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-peer@4.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@2.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
2023-02-21 12:03:48 -05:00
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-peer@4.0.0").unwrap(),
|
|
|
|
)])
|
2022-11-08 14:17:24 -05:00
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-c@3.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
2023-02-21 12:03:48 -05:00
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-peer@4.0.0").unwrap(),
|
|
|
|
)])
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@4.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: Default::default(),
|
2022-11-08 14:17:24 -05:00
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
2023-02-21 12:03:48 -05:00
|
|
|
vec![("package-0@1.1.1".to_string(), "package-0@1.1.1".to_string())]
|
2022-11-08 14:17:24 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-02-21 12:03:48 -05:00
|
|
|
async fn resolve_with_peer_deps_auto_resolved() {
|
|
|
|
// in this case, the peer dependency is not found in the tree
|
|
|
|
// so it's auto-resolved based on the registry
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-08 14:17:24 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.ensure_package_version("package-c", "3.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "4.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "4.1.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "^2"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-c", "^3"));
|
2023-02-21 12:03:48 -05:00
|
|
|
api.add_peer_dependency(("package-b", "2.0.0"), ("package-peer", "4"));
|
|
|
|
api.add_peer_dependency(("package-c", "3.0.0"), ("package-peer", "*"));
|
2022-11-08 14:17:24 -05:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-a@1"]).await;
|
2022-11-08 14:17:24 -05:00
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-b@2.0.0_package-peer@4.1.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-c".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-c@3.0.0_package-peer@4.1.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@2.0.0_package-peer@4.1.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@4.1.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)])
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-c@3.0.0_package-peer@4.1.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@4.1.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)])
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@4.1.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
2023-02-21 12:03:48 -05:00
|
|
|
vec![("package-a@1".to_string(), "package-a@1.0.0".to_string())]
|
2022-11-08 14:17:24 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-06 09:13:21 -05:00
|
|
|
#[tokio::test]
|
2023-02-21 12:03:48 -05:00
|
|
|
async fn resolve_with_optional_peer_dep_not_resolved() {
|
|
|
|
// in this case, the peer dependency is not found in the tree
|
|
|
|
// so it's auto-resolved based on the registry
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2023-01-06 09:13:21 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
2023-02-21 12:03:48 -05:00
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.ensure_package_version("package-c", "3.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "4.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "4.1.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "^2"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-c", "^3"));
|
2023-01-06 09:13:21 -05:00
|
|
|
api.add_optional_peer_dependency(
|
2023-02-21 12:03:48 -05:00
|
|
|
("package-b", "2.0.0"),
|
|
|
|
("package-peer", "4"),
|
|
|
|
);
|
|
|
|
api.add_optional_peer_dependency(
|
|
|
|
("package-c", "3.0.0"),
|
2023-01-06 09:13:21 -05:00
|
|
|
("package-peer", "*"),
|
|
|
|
);
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-a@1"]).await;
|
2023-01-06 09:13:21 -05:00
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-b@2.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
),
|
|
|
|
(
|
2023-02-21 12:03:48 -05:00
|
|
|
"package-c".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-c@3.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
),
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-b@2.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
2023-02-21 12:03:48 -05:00
|
|
|
dependencies: HashMap::new(),
|
2023-01-06 09:13:21 -05:00
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-c@3.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
2023-02-21 12:03:48 -05:00
|
|
|
vec![("package-a@1".to_string(), "package-a@1.0.0".to_string())]
|
2023-01-06 09:13:21 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-02-21 12:03:48 -05:00
|
|
|
async fn resolve_with_optional_peer_found() {
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2023-01-06 09:13:21 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
2023-02-21 12:03:48 -05:00
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.ensure_package_version("package-c", "3.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "4.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "4.1.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "^2"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-c", "^3"));
|
|
|
|
api.add_optional_peer_dependency(
|
|
|
|
("package-b", "2.0.0"),
|
|
|
|
("package-peer", "4"),
|
|
|
|
);
|
|
|
|
api.add_optional_peer_dependency(
|
|
|
|
("package-c", "3.0.0"),
|
|
|
|
("package-peer", "*"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
|
|
|
vec!["npm:package-a@1", "npm:package-peer@4.0.0"],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-b".to_string(),
|
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-b@2.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-c".to_string(),
|
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-c@3.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@2.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-peer@4.0.0").unwrap(),
|
|
|
|
)])
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-c@3.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-peer@4.0.0").unwrap(),
|
|
|
|
)])
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@4.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
"package-a@1".to_string(),
|
|
|
|
"package-a@1.0.0_package-peer@4.0.0".to_string()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer@4.0.0".to_string(),
|
|
|
|
"package-peer@4.0.0".to_string()
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_optional_peer_first_not_resolved_second_resolved_scenario1()
|
|
|
|
{
|
|
|
|
// When resolving a dependency a second time and it has an optional
|
|
|
|
// peer dependency that wasn't previously resolved, it should resolve all the
|
|
|
|
// previous versions to the new one
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "1.0.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "^1"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-peer", "^1"));
|
|
|
|
api.add_optional_peer_dependency(
|
|
|
|
("package-b", "1.0.0"),
|
|
|
|
("package-peer", "*"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
|
|
|
vec!["npm:package-a@1", "npm:package-b@1"],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-b".to_string(),
|
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-b@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
|
|
|
)]),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
"package-a@1".to_string(),
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0".to_string()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-b@1".to_string(),
|
|
|
|
"package-b@1.0.0_package-peer@1.0.0".to_string()
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_optional_peer_first_not_resolved_second_resolved_scenario2()
|
|
|
|
{
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "2.0.0");
|
|
|
|
api.add_optional_peer_dependency(
|
|
|
|
("package-a", "1.0.0"),
|
|
|
|
("package-peer", "*"),
|
|
|
|
);
|
|
|
|
api.add_dependency(("package-b", "1.0.0"), ("package-a", "1.0.0"));
|
2023-01-06 09:13:21 -05:00
|
|
|
api.add_dependency(("package-b", "1.0.0"), ("package-peer", "2.0.0"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
|
|
|
vec!["npm:package-a@1", "npm:package-b@1"],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@2.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@2.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@1.0.0_package-peer@2.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-a".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@2.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@2.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
)
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@2.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![
|
2023-02-21 12:03:48 -05:00
|
|
|
(
|
|
|
|
"package-a@1".to_string(),
|
|
|
|
"package-a@1.0.0_package-peer@2.0.0".to_string()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-b@1".to_string(),
|
|
|
|
"package-b@1.0.0_package-peer@2.0.0".to_string()
|
|
|
|
)
|
2023-01-06 09:13:21 -05:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_optional_dep_npm_req_top() {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2023-01-06 09:13:21 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "1.0.0");
|
|
|
|
api.add_optional_peer_dependency(
|
|
|
|
("package-a", "1.0.0"),
|
|
|
|
("package-peer", "*"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
|
|
|
vec!["npm:package-a@1", "npm:package-peer@1"],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![
|
2023-02-21 12:03:48 -05:00
|
|
|
(
|
|
|
|
"package-a@1".to_string(),
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0".to_string()
|
|
|
|
),
|
2023-01-06 09:13:21 -05:00
|
|
|
(
|
|
|
|
"package-peer@1".to_string(),
|
|
|
|
"package-peer@1.0.0".to_string()
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_optional_dep_different_resolution_second_time() {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2023-01-06 09:13:21 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "2.0.0");
|
|
|
|
api.add_optional_peer_dependency(
|
|
|
|
("package-a", "1.0.0"),
|
|
|
|
("package-peer", "*"),
|
|
|
|
);
|
|
|
|
api.add_dependency(("package-b", "1.0.0"), ("package-a", "1.0.0"));
|
|
|
|
api.add_dependency(("package-b", "1.0.0"), ("package-peer", "2.0.0"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
|
|
|
vec![
|
|
|
|
"npm:package-a@1",
|
|
|
|
"npm:package-b@1",
|
|
|
|
"npm:package-peer@1.0.0",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
2023-01-06 09:13:21 -05:00
|
|
|
"package-a@1.0.0_package-peer@2.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 1,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@2.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
2023-01-06 09:13:21 -05:00
|
|
|
"package-b@1.0.0_package-peer@2.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@2.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-a".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
2023-01-06 09:13:21 -05:00
|
|
|
"package-a@1.0.0_package-peer@2.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@2.0.0").unwrap(),
|
2023-01-06 09:13:21 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![
|
2023-02-21 12:03:48 -05:00
|
|
|
(
|
|
|
|
"package-a@1".to_string(),
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0".to_string()
|
|
|
|
),
|
2023-01-06 09:13:21 -05:00
|
|
|
(
|
|
|
|
"package-b@1".to_string(),
|
|
|
|
"package-b@1.0.0_package-peer@2.0.0".to_string()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer@1.0.0".to_string(),
|
|
|
|
"package-peer@1.0.0".to_string()
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_peer_dep_other_specifier_slot() {
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "2.0.0");
|
|
|
|
// bit of an edge case... probably nobody has ever done this
|
|
|
|
api.add_dependency(
|
|
|
|
("package-a", "1.0.0"),
|
|
|
|
("package-peer2", "npm:package-peer@2"),
|
|
|
|
);
|
|
|
|
api.add_peer_dependency(("package-a", "1.0.0"), ("package-peer", "2"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-a@1"]).await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@2.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-peer".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-peer@2.0.0").unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer2".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-peer@2.0.0").unwrap(),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@2.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![(
|
|
|
|
"package-a@1".to_string(),
|
|
|
|
"package-a@1.0.0_package-peer@2.0.0".to_string()
|
|
|
|
),]
|
|
|
|
);
|
|
|
|
}
|
2023-01-06 09:13:21 -05:00
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_nested_peer_deps_auto_resolved() {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-08 14:17:24 -05:00
|
|
|
api.ensure_package_version("package-0", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer-a", "2.0.0");
|
|
|
|
api.ensure_package_version("package-peer-b", "3.0.0");
|
|
|
|
api.add_peer_dependency(("package-0", "1.0.0"), ("package-peer-a", "2"));
|
|
|
|
api.add_peer_dependency(
|
|
|
|
("package-peer-a", "2.0.0"),
|
|
|
|
("package-peer-b", "3"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-0@1.0"]).await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-0@1.0.0_package-peer-a@2.0.0__package-peer-b@3.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer-a".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-peer-a@2.0.0_package-peer-b@3.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-peer-a@2.0.0_package-peer-b@3.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer-b@3.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer-b@3.0.0")
|
2023-02-17 09:12:22 -05:00
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
2023-02-21 12:03:48 -05:00
|
|
|
vec![(
|
|
|
|
"package-0@1.0".to_string(),
|
|
|
|
"package-0@1.0.0_package-peer-a@2.0.0__package-peer-b@3.0.0"
|
|
|
|
.to_string()
|
|
|
|
)]
|
2022-11-08 14:17:24 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_nested_peer_deps_ancestor_sibling_deps() {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-08 14:17:24 -05:00
|
|
|
api.ensure_package_version("package-0", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer-a", "2.0.0");
|
|
|
|
api.ensure_package_version("package-peer-b", "3.0.0");
|
|
|
|
api.add_dependency(("package-0", "1.0.0"), ("package-peer-b", "*"));
|
|
|
|
api.add_peer_dependency(("package-0", "1.0.0"), ("package-peer-a", "2"));
|
|
|
|
api.add_peer_dependency(
|
|
|
|
("package-peer-a", "2.0.0"),
|
|
|
|
("package-peer-b", "3"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
|
|
|
vec![
|
|
|
|
"npm:package-0@1.0",
|
|
|
|
"npm:package-peer-a@2",
|
|
|
|
"npm:package-peer-b@3",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-0@1.0.0_package-peer-a@2.0.0__package-peer-b@3.0.0_package-peer-b@3.0.0"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-peer-a".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
2022-11-08 14:17:24 -05:00
|
|
|
"package-peer-a@2.0.0_package-peer-b@3.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer-b@3.0.0")
|
2023-02-17 09:12:22 -05:00
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
2022-11-08 14:17:24 -05:00
|
|
|
"package-peer-a@2.0.0_package-peer-b@3.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer-b@3.0.0")
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer-b@3.0.0")
|
2023-02-17 09:12:22 -05:00
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
"package-0@1.0".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
"package-0@1.0.0_package-peer-a@2.0.0__package-peer-b@3.0.0_package-peer-b@3.0.0"
|
2022-11-08 14:17:24 -05:00
|
|
|
.to_string()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer-a@2".to_string(),
|
|
|
|
"package-peer-a@2.0.0_package-peer-b@3.0.0".to_string()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer-b@3".to_string(),
|
|
|
|
"package-peer-b@3.0.0".to_string()
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_with_peer_deps_multiple() {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-08 14:17:24 -05:00
|
|
|
api.ensure_package_version("package-0", "1.1.1");
|
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.ensure_package_version("package-c", "3.0.0");
|
|
|
|
api.ensure_package_version("package-d", "3.5.0");
|
|
|
|
api.ensure_package_version("package-e", "3.6.0");
|
|
|
|
api.ensure_package_version("package-peer-a", "4.0.0");
|
|
|
|
api.ensure_package_version("package-peer-a", "4.1.0");
|
|
|
|
api.ensure_package_version("package-peer-b", "5.3.0");
|
|
|
|
api.ensure_package_version("package-peer-b", "5.4.1");
|
|
|
|
api.ensure_package_version("package-peer-c", "6.2.0");
|
|
|
|
api.add_dependency(("package-0", "1.1.1"), ("package-a", "1"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "^2"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-c", "^3"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-d", "^3"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-peer-a", "4.0.0"));
|
|
|
|
api.add_peer_dependency(("package-b", "2.0.0"), ("package-peer-a", "4"));
|
|
|
|
api.add_peer_dependency(
|
|
|
|
("package-b", "2.0.0"),
|
2023-02-21 12:03:48 -05:00
|
|
|
("package-peer-c", "=6.2.0"), // will be auto-resolved
|
2022-11-08 14:17:24 -05:00
|
|
|
);
|
|
|
|
api.add_peer_dependency(("package-c", "3.0.0"), ("package-peer-a", "*"));
|
|
|
|
api.add_peer_dependency(
|
|
|
|
("package-peer-a", "4.0.0"),
|
|
|
|
("package-peer-b", "^5.4"), // will be auto-resolved
|
|
|
|
);
|
|
|
|
|
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
|
|
|
vec!["npm:package-0@1.1.1", "npm:package-e@3"],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-0@1.1.1")
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-a".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer-a@4.0.0__package-peer-b@5.4.1"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer-a@4.0.0__package-peer-b@5.4.1"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-b@2.0.0_package-peer-a@4.0.0__package-peer-b@5.4.1_package-peer-c@6.2.0"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-c".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-c@3.0.0_package-peer-a@4.0.0__package-peer-b@5.4.1"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-d".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-d@3.5.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer-a".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-peer-a@4.0.0_package-peer-b@5.4.1"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@2.0.0_package-peer-a@4.0.0__package-peer-b@5.4.1_package-peer-c@6.2.0"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-peer-a".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer-a@4.0.0_package-peer-b@5.4.1")
|
2023-02-17 09:12:22 -05:00
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer-c".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer-c@6.2.0")
|
2023-02-17 09:12:22 -05:00
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
])
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-c@3.0.0_package-peer-a@4.0.0__package-peer-b@5.4.1"
|
2022-11-08 14:17:24 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer-a".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer-a@4.0.0_package-peer-b@5.4.1")
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)])
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-d@3.5.0")
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-e@3.6.0")
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer-a@4.0.0_package-peer-b@5.4.1")
|
2023-02-17 09:12:22 -05:00
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer-b@5.4.1")
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)])
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer-b@5.4.1")
|
2023-02-17 09:12:22 -05:00
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer-c@6.2.0")
|
2023-02-17 09:12:22 -05:00
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dist: Default::default(),
|
|
|
|
dependencies: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![
|
|
|
|
("package-0@1.1.1".to_string(), "package-0@1.1.1".to_string()),
|
|
|
|
("package-e@3".to_string(), "package-e@3.6.0".to_string()),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_peer_deps_circular() {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-08 14:17:24 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "*"));
|
|
|
|
api.add_peer_dependency(("package-b", "2.0.0"), ("package-a", "1"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-a@1.0"]).await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-b@2.0.0_package-a@1.0.0")
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
2023-02-17 09:12:22 -05:00
|
|
|
"package-b@2.0.0_package-a@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-a".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
2022-11-18 09:41:06 -05:00
|
|
|
vec![("package-a@1.0".to_string(), "package-a@1.0.0".to_string())]
|
2022-11-08 14:17:24 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_peer_deps_multiple_copies() {
|
|
|
|
// repeat this a few times to have a higher probability of surfacing indeterminism
|
|
|
|
for _ in 0..3 {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-08 14:17:24 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.ensure_package_version("package-dep", "3.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "4.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "5.0.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-dep", "*"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-peer", "4"));
|
|
|
|
api.add_dependency(("package-b", "2.0.0"), ("package-dep", "*"));
|
|
|
|
api.add_dependency(("package-b", "2.0.0"), ("package-peer", "5"));
|
|
|
|
api.add_peer_dependency(("package-dep", "3.0.0"), ("package-peer", "*"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
|
|
|
vec!["npm:package-a@1", "npm:package-b@2"],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
2022-11-08 14:17:24 -05:00
|
|
|
"package-a@1.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-dep".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
2022-11-08 14:17:24 -05:00
|
|
|
"package-dep@3.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@4.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
2022-11-08 14:17:24 -05:00
|
|
|
"package-b@2.0.0_package-peer@5.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-dep".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
2022-11-08 14:17:24 -05:00
|
|
|
"package-dep@3.0.0_package-peer@5.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@5.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
),
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
2022-11-08 14:17:24 -05:00
|
|
|
"package-dep@3.0.0_package-peer@4.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@4.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
2022-11-08 14:17:24 -05:00
|
|
|
"package-dep@3.0.0_package-peer@5.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 1,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@5.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@4.0.0")
|
2023-02-17 09:12:22 -05:00
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@5.0.0")
|
2023-02-17 09:12:22 -05:00
|
|
|
.unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
"package-a@1".to_string(),
|
|
|
|
"package-a@1.0.0_package-peer@4.0.0".to_string()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-b@2".to_string(),
|
|
|
|
"package-b@2.0.0_package-peer@5.0.0".to_string()
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 21:22:59 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_dep_with_peer_deps_dep_then_peer() {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-14 21:22:59 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "1.0.0");
|
|
|
|
api.ensure_package_version("package-c", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "1.0.0");
|
|
|
|
api.add_peer_dependency(("package-b", "1.0.0"), ("package-peer", "1"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-c", "1"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-peer", "1"));
|
|
|
|
api.add_peer_dependency(("package-c", "1.0.0"), ("package-b", "1"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
|
|
|
vec!["npm:package-a@1.0", "npm:package-b@1.0"],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-b@1.0.0__package-peer@1.0.0"
|
2023-02-17 09:12:22 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-c".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-c@1.0.0_package-b@1.0.0__package-peer@1.0.0"
|
2023-02-17 09:12:22 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
)
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-c@1.0.0_package-b@1.0.0__package-peer@1.0.0"
|
2023-02-17 09:12:22 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-b@1.0.0_package-peer@1.0.0")
|
|
|
|
.unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
"package-a@1.0".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
"package-a@1.0.0_package-b@1.0.0__package-peer@1.0.0".to_string()
|
2022-11-14 21:22:59 -05:00
|
|
|
),
|
2023-02-21 12:03:48 -05:00
|
|
|
(
|
|
|
|
"package-b@1.0".to_string(),
|
|
|
|
"package-b@1.0.0_package-peer@1.0.0".to_string()
|
|
|
|
)
|
|
|
|
]
|
2022-11-14 21:22:59 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-02-21 12:03:48 -05:00
|
|
|
async fn resolve_dep_with_peer_deps_then_other_dep_with_different_peer() {
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-14 21:22:59 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "1.0.0");
|
|
|
|
api.ensure_package_version("package-c", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "1.1.0");
|
|
|
|
api.ensure_package_version("package-peer", "1.2.0");
|
2023-02-21 12:03:48 -05:00
|
|
|
api.add_peer_dependency(("package-a", "1.0.0"), ("package-peer", "*")); // should select 1.2.0, then 1.1.0
|
2022-11-14 21:22:59 -05:00
|
|
|
api.add_dependency(("package-b", "1.0.0"), ("package-c", "1"));
|
|
|
|
api.add_dependency(("package-b", "1.0.0"), ("package-peer", "=1.1.0"));
|
2023-02-21 12:03:48 -05:00
|
|
|
api.add_dependency(("package-c", "1.0.0"), ("package-a", "1"));
|
2022-11-14 21:22:59 -05:00
|
|
|
|
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
|
|
|
vec!["npm:package-a@1.0", "npm:package-b@1.0"],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@1.1.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 1,
|
2022-11-14 21:22:59 -05:00
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@1.1.0").unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@1.2.0"
|
2022-11-14 21:22:59 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2023-02-21 12:03:48 -05:00
|
|
|
copy_index: 0,
|
2022-11-14 21:22:59 -05:00
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@1.2.0").unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@1.0.0_package-peer@1.1.0"
|
2022-11-14 21:22:59 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-c".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-c@1.0.0_package-peer@1.1.0"
|
2022-11-14 21:22:59 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-peer@1.1.0").unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
)
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-c@1.0.0_package-peer@1.1.0"
|
2022-11-14 21:22:59 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-a".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0_package-peer@1.1.0")
|
|
|
|
.unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@1.1.0").unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@1.2.0").unwrap(),
|
2022-11-14 21:22:59 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![
|
2023-02-21 12:03:48 -05:00
|
|
|
(
|
|
|
|
"package-a@1.0".to_string(),
|
|
|
|
"package-a@1.0.0_package-peer@1.2.0".to_string()
|
|
|
|
),
|
2022-11-14 21:22:59 -05:00
|
|
|
(
|
|
|
|
"package-b@1.0".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
"package-b@1.0.0_package-peer@1.1.0".to_string()
|
2022-11-14 21:22:59 -05:00
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-15 20:52:27 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_dep_and_peer_dist_tag() {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2022-11-15 20:52:27 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.ensure_package_version("package-b", "3.0.0");
|
|
|
|
api.ensure_package_version("package-c", "1.0.0");
|
|
|
|
api.ensure_package_version("package-d", "1.0.0");
|
2023-01-06 08:48:56 -05:00
|
|
|
api.ensure_package_version("package-e", "1.0.0");
|
2022-11-15 20:52:27 -05:00
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "some-tag"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-d", "1.0.0"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-c", "1.0.0"));
|
2023-01-06 08:48:56 -05:00
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-e", "1.0.0"));
|
|
|
|
api.add_dependency(("package-e", "1.0.0"), ("package-b", "some-tag"));
|
2022-11-15 20:52:27 -05:00
|
|
|
api.add_peer_dependency(("package-c", "1.0.0"), ("package-d", "other-tag"));
|
|
|
|
api.add_dist_tag("package-b", "some-tag", "2.0.0");
|
|
|
|
api.add_dist_tag("package-d", "other-tag", "1.0.0");
|
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-a@1.0"]).await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
2023-02-17 09:12:22 -05:00
|
|
|
"package-a@1.0.0_package-d@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-15 20:52:27 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-b@2.0.0").unwrap(),
|
2022-11-15 20:52:27 -05:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-c".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-c@1.0.0_package-d@1.0.0")
|
|
|
|
.unwrap(),
|
2022-11-15 20:52:27 -05:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-d".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-d@1.0.0").unwrap(),
|
2022-11-15 20:52:27 -05:00
|
|
|
),
|
2023-01-06 08:48:56 -05:00
|
|
|
(
|
|
|
|
"package-e".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-e@1.0.0").unwrap(),
|
2023-01-06 08:48:56 -05:00
|
|
|
),
|
2022-11-15 20:52:27 -05:00
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-b@2.0.0").unwrap(),
|
2022-11-15 20:52:27 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
2023-02-17 09:12:22 -05:00
|
|
|
"package-c@1.0.0_package-d@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2022-11-15 20:52:27 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-d".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-d@1.0.0").unwrap(),
|
2022-11-15 20:52:27 -05:00
|
|
|
),]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-d@1.0.0").unwrap(),
|
2022-11-15 20:52:27 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
2023-01-06 08:48:56 -05:00
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-e@1.0.0").unwrap(),
|
2023-01-06 08:48:56 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-b".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-b@2.0.0").unwrap(),
|
2023-01-06 08:48:56 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
2022-11-15 20:52:27 -05:00
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![(
|
|
|
|
"package-a@1.0".to_string(),
|
|
|
|
"package-a@1.0.0_package-d@1.0.0".to_string()
|
|
|
|
),]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-14 15:10:07 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn package_has_self_as_dependency() {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2023-01-14 15:10:07 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-a", "1"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-a@1.0"]).await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
2023-01-14 15:10:07 -05:00
|
|
|
copy_index: 0,
|
|
|
|
// in this case, we just ignore that the package did this
|
|
|
|
dependencies: Default::default(),
|
|
|
|
dist: Default::default(),
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![("package-a@1.0".to_string(), "package-a@1.0.0".to_string())]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn package_has_self_but_different_version_as_dependency() {
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = TestNpmRegistryApiInner::default();
|
2023-01-14 15:10:07 -05:00
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-a", "0.5.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-a", "^0.5"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-a@1.0"]).await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-a@0.5.0").unwrap(),
|
2023-01-14 15:10:07 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: Default::default(),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
2023-02-21 12:03:48 -05:00
|
|
|
pkg_id: NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
2023-01-14 15:10:07 -05:00
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-a".to_string(),
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageId::from_serialized("package-a@0.5.0").unwrap(),
|
2023-01-14 15:10:07 -05:00
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![("package-a@1.0".to_string(), "package-a@1.0.0".to_string())]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn grand_child_package_has_self_as_peer_dependency_root() {
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "2"));
|
|
|
|
api.add_peer_dependency(("package-b", "2.0.0"), ("package-a", "*"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-a@1.0"]).await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-b".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-b@2.0.0_package-a@1.0.0")
|
|
|
|
.unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@2.0.0_package-a@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![("package-a@1.0".to_string(), "package-a@1.0.0".to_string())]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn grand_child_package_has_self_as_peer_dependency_under_root() {
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
|
|
|
api.ensure_package_version("package-0", "1.0.0");
|
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "2.0.0");
|
|
|
|
api.add_dependency(("package-0", "1.0.0"), ("package-a", "*"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "2"));
|
|
|
|
api.add_peer_dependency(("package-b", "2.0.0"), ("package-a", "*"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-0@1.0"]).await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-0@1.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-b".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-b@2.0.0_package-a@1.0.0")
|
|
|
|
.unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@2.0.0_package-a@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0").unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![("package-0@1.0".to_string(), "package-0@1.0.0".to_string())]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn nested_deps_same_peer_dep_ancestor() {
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
|
|
|
api.ensure_package_version("package-0", "1.0.0");
|
|
|
|
api.ensure_package_version("package-1", "1.0.0");
|
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "1.0.0");
|
|
|
|
api.ensure_package_version("package-c", "1.0.0");
|
|
|
|
api.ensure_package_version("package-d", "1.0.0");
|
|
|
|
api.add_dependency(("package-0", "1.0.0"), ("package-a", "1"));
|
|
|
|
api.add_dependency(("package-0", "1.0.0"), ("package-1", "1"));
|
|
|
|
api.add_dependency(("package-1", "1.0.0"), ("package-a", "1"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "1"));
|
|
|
|
api.add_dependency(("package-b", "1.0.0"), ("package-c", "1"));
|
|
|
|
api.add_dependency(("package-c", "1.0.0"), ("package-d", "1"));
|
|
|
|
api.add_peer_dependency(("package-b", "1.0.0"), ("package-a", "*"));
|
|
|
|
api.add_peer_dependency(("package-c", "1.0.0"), ("package-a", "*"));
|
|
|
|
api.add_peer_dependency(("package-d", "1.0.0"), ("package-a", "*"));
|
|
|
|
api.add_peer_dependency(("package-b", "1.0.0"), ("package-0", "*"));
|
|
|
|
api.add_peer_dependency(("package-c", "1.0.0"), ("package-0", "*"));
|
|
|
|
api.add_peer_dependency(("package-d", "1.0.0"), ("package-0", "*"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) =
|
|
|
|
run_resolver_and_get_output(api, vec!["npm:package-0@1.0"]).await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-0@1.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0_package-0@1.0.0").unwrap(),
|
|
|
|
), (
|
|
|
|
"package-1".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-1@1.0.0_package-0@1.0.0").unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-1@1.0.0_package-0@1.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0_package-0@1.0.0").unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-a@1.0.0_package-0@1.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-b".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-b@1.0.0_package-0@1.0.0_package-a@1.0.0__package-0@1.0.0")
|
|
|
|
.unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@1.0.0_package-0@1.0.0_package-a@1.0.0__package-0@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-0".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-0@1.0.0").unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0_package-0@1.0.0").unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-c".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-c@1.0.0_package-0@1.0.0_package-a@1.0.0__package-0@1.0.0")
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-c@1.0.0_package-0@1.0.0_package-a@1.0.0__package-0@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-0".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-0@1.0.0").unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0_package-0@1.0.0").unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-d".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-d@1.0.0_package-0@1.0.0_package-a@1.0.0__package-0@1.0.0")
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-d@1.0.0_package-0@1.0.0_package-a@1.0.0__package-0@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-0".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-0@1.0.0").unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0_package-0@1.0.0").unwrap(),
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![("package-0@1.0".to_string(), "package-0@1.0.0".to_string())]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn peer_dep_resolved_then_resolved_deeper() {
|
|
|
|
let api = TestNpmRegistryApiInner::default();
|
|
|
|
api.ensure_package_version("package-0", "1.0.0");
|
|
|
|
api.ensure_package_version("package-1", "1.0.0");
|
|
|
|
api.ensure_package_version("package-a", "1.0.0");
|
|
|
|
api.ensure_package_version("package-b", "1.0.0");
|
|
|
|
api.ensure_package_version("package-peer", "1.0.0");
|
|
|
|
api.add_dependency(("package-0", "1.0.0"), ("package-a", "1"));
|
|
|
|
api.add_dependency(("package-0", "1.0.0"), ("package-1", "1"));
|
|
|
|
api.add_dependency(("package-1", "1.0.0"), ("package-a", "1"));
|
|
|
|
api.add_dependency(("package-a", "1.0.0"), ("package-b", "1"));
|
|
|
|
api.add_peer_dependency(("package-b", "1.0.0"), ("package-peer", "*"));
|
|
|
|
|
|
|
|
let (packages, package_reqs) = run_resolver_and_get_output(
|
|
|
|
api,
|
|
|
|
vec!["npm:package-0@1.0", "npm:package-peer@1.0"],
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(
|
|
|
|
packages,
|
|
|
|
vec![
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-0@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([
|
|
|
|
(
|
|
|
|
"package-1".to_string(),
|
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-1@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-1@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-a".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-a@1.0.0_package-peer@1.0.0")
|
|
|
|
.unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-a@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-b".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-b@1.0.0_package-peer@1.0.0")
|
|
|
|
.unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized(
|
|
|
|
"package-b@1.0.0_package-peer@1.0.0"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: HashMap::from([(
|
|
|
|
"package-peer".to_string(),
|
|
|
|
NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
|
|
|
)]),
|
|
|
|
dist: Default::default(),
|
|
|
|
},
|
|
|
|
NpmResolutionPackage {
|
|
|
|
pkg_id: NpmPackageId::from_serialized("package-peer@1.0.0").unwrap(),
|
|
|
|
copy_index: 0,
|
|
|
|
dependencies: Default::default(),
|
|
|
|
dist: Default::default(),
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
package_reqs,
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
"package-0@1.0".to_string(),
|
|
|
|
"package-0@1.0.0_package-peer@1.0.0".to_string()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"package-peer@1.0".to_string(),
|
|
|
|
"package-peer@1.0.0".to_string()
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
async fn run_resolver_and_get_output(
|
2023-02-21 12:03:48 -05:00
|
|
|
api: TestNpmRegistryApiInner,
|
2022-11-08 14:17:24 -05:00
|
|
|
reqs: Vec<&str>,
|
|
|
|
) -> (Vec<NpmResolutionPackage>, Vec<(String, String)>) {
|
|
|
|
let mut graph = Graph::default();
|
2023-02-21 12:03:48 -05:00
|
|
|
let api = NpmRegistryApi::new_for_test(api);
|
2022-11-08 14:17:24 -05:00
|
|
|
let mut resolver = GraphDependencyResolver::new(&mut graph, &api);
|
|
|
|
|
|
|
|
for req in reqs {
|
2023-02-21 12:03:48 -05:00
|
|
|
let req = NpmPackageReqReference::from_str(req).unwrap().req;
|
2022-11-08 14:17:24 -05:00
|
|
|
resolver
|
2022-11-11 11:33:57 -05:00
|
|
|
.add_package_req(&req, &api.package_info(&req.name).await.unwrap())
|
2022-11-08 14:17:24 -05:00
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
resolver.resolve_pending().await.unwrap();
|
|
|
|
let snapshot = graph.into_snapshot(&api).await.unwrap();
|
2023-02-21 12:03:48 -05:00
|
|
|
|
|
|
|
{
|
2023-02-21 15:19:09 -05:00
|
|
|
let new_snapshot = Graph::from_snapshot(snapshot.clone())
|
|
|
|
.unwrap()
|
|
|
|
.into_snapshot(&api)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
snapshot, new_snapshot,
|
|
|
|
"recreated snapshot should be the same"
|
|
|
|
);
|
|
|
|
// create one again from the new snapshot
|
|
|
|
let new_snapshot2 = Graph::from_snapshot(new_snapshot.clone())
|
|
|
|
.unwrap()
|
|
|
|
.into_snapshot(&api)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
snapshot, new_snapshot2,
|
|
|
|
"second recreated snapshot should be the same"
|
|
|
|
);
|
2023-02-21 12:03:48 -05:00
|
|
|
}
|
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
let mut packages = snapshot.all_packages();
|
2023-02-21 12:03:48 -05:00
|
|
|
packages.sort_by(|a, b| a.pkg_id.cmp(&b.pkg_id));
|
2022-11-08 14:17:24 -05:00
|
|
|
let mut package_reqs = snapshot
|
|
|
|
.package_reqs
|
|
|
|
.into_iter()
|
2023-02-21 12:03:48 -05:00
|
|
|
.map(|(a, b)| {
|
|
|
|
(
|
|
|
|
a.to_string(),
|
|
|
|
snapshot.root_packages.get(&b).unwrap().as_serialized(),
|
|
|
|
)
|
|
|
|
})
|
2022-11-08 14:17:24 -05:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
package_reqs.sort_by(|a, b| a.0.to_string().cmp(&b.0.to_string()));
|
2023-02-21 12:03:48 -05:00
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
(packages, package_reqs)
|
|
|
|
}
|
|
|
|
}
|