summaryrefslogtreecommitdiff
path: root/cli/npm/resolution/graph.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/npm/resolution/graph.rs')
-rw-r--r--cli/npm/resolution/graph.rs285
1 files changed, 197 insertions, 88 deletions
diff --git a/cli/npm/resolution/graph.rs b/cli/npm/resolution/graph.rs
index a182299c7..8995b4076 100644
--- a/cli/npm/resolution/graph.rs
+++ b/cli/npm/resolution/graph.rs
@@ -411,19 +411,19 @@ impl<'a, TNpmRegistryApi: NpmRegistryApi>
}
}
- fn resolve_best_package_version_and_info(
+ fn resolve_best_package_version_and_info<'info>(
&self,
name: &str,
version_matcher: &impl NpmVersionMatcher,
- package_info: &NpmPackageInfo,
- ) -> Result<VersionAndInfo, AnyError> {
+ package_info: &'info NpmPackageInfo,
+ ) -> Result<VersionAndInfo<'info>, AnyError> {
if let Some(version) =
self.resolve_best_package_version(name, version_matcher)
{
match package_info.versions.get(&version.to_string()) {
Some(version_info) => Ok(VersionAndInfo {
version,
- info: version_info.clone(),
+ info: version_info,
}),
None => {
bail!("could not find version '{}' for '{}'", version, name)
@@ -670,16 +670,21 @@ impl<'a, TNpmRegistryApi: NpmRegistryApi>
) -> Result<Option<NpmPackageId>, AnyError> {
fn find_matching_child<'a>(
peer_dep: &NpmDependencyEntry,
+ peer_package_info: &NpmPackageInfo,
children: impl Iterator<Item = &'a NpmPackageId>,
- ) -> Option<NpmPackageId> {
+ ) -> Result<Option<NpmPackageId>, AnyError> {
for child_id in children {
if child_id.name == peer_dep.name
- && peer_dep.version_req.satisfies(&child_id.version)
+ && version_req_satisfies(
+ &peer_dep.version_req,
+ &child_id.version,
+ peer_package_info,
+ )?
{
- return Some(child_id.clone());
+ return Ok(Some(child_id.clone()));
}
}
- None
+ Ok(None)
}
// Peer dependencies are resolved based on its ancestors' siblings.
@@ -712,8 +717,11 @@ impl<'a, TNpmRegistryApi: NpmRegistryApi>
match &ancestor {
NodeParent::Node(ancestor_node_id) => {
let maybe_peer_dep_id = if ancestor_node_id.name == peer_dep.name
- && peer_dep.version_req.satisfies(&ancestor_node_id.version)
- {
+ && version_req_satisfies(
+ &peer_dep.version_req,
+ &ancestor_node_id.version,
+ peer_package_info,
+ )? {
Some(ancestor_node_id.clone())
} else {
let ancestor = self.graph.borrow_node(ancestor_node_id);
@@ -731,7 +739,11 @@ impl<'a, TNpmRegistryApi: NpmRegistryApi>
}
}
}
- find_matching_child(peer_dep, ancestor.children.values())
+ find_matching_child(
+ peer_dep,
+ peer_package_info,
+ ancestor.children.values(),
+ )?
};
if let Some(peer_dep_id) = maybe_peer_dep_id {
if existing_dep_id == Some(&peer_dep_id) {
@@ -751,9 +763,11 @@ impl<'a, TNpmRegistryApi: NpmRegistryApi>
}
NodeParent::Req => {
// in this case, the parent is the root so the children are all the package requirements
- if let Some(child_id) =
- find_matching_child(peer_dep, self.graph.package_reqs.values())
- {
+ if let Some(child_id) = find_matching_child(
+ peer_dep,
+ peer_package_info,
+ self.graph.package_reqs.values(),
+ )? {
if existing_dep_id == Some(&child_id) {
return Ok(None); // do nothing, there's already an existing child dep id for this
}
@@ -778,7 +792,7 @@ impl<'a, TNpmRegistryApi: NpmRegistryApi>
// to resolve based on the package info and will treat this just like any
// other dependency when not optional
if !peer_dep.kind.is_optional()
- // only
+ // prefer the existing dep id if it exists
&& existing_dep_id.is_none()
{
self.analyze_dependency(
@@ -917,18 +931,17 @@ impl<'a, TNpmRegistryApi: NpmRegistryApi>
}
#[derive(Clone)]
-struct VersionAndInfo {
+struct VersionAndInfo<'a> {
version: NpmVersion,
- info: NpmPackageVersionInfo,
+ info: &'a NpmPackageVersionInfo,
}
-fn get_resolved_package_version_and_info(
+fn get_resolved_package_version_and_info<'a>(
pkg_name: &str,
version_matcher: &impl NpmVersionMatcher,
- info: &NpmPackageInfo,
+ info: &'a NpmPackageInfo,
parent: Option<&NpmPackageId>,
-) -> Result<VersionAndInfo, AnyError> {
- let mut maybe_best_version: Option<VersionAndInfo> = None;
+) -> Result<VersionAndInfo<'a>, AnyError> {
if let Some(tag) = version_matcher.tag() {
// For when someone just specifies @types/node, we want to pull in a
// "known good" version of @types/node that works well with Deno and
@@ -946,26 +959,9 @@ fn get_resolved_package_version_and_info(
);
}
- if let Some(version) = info.dist_tags.get(tag) {
- match info.versions.get(version) {
- Some(info) => {
- return Ok(VersionAndInfo {
- version: NpmVersion::parse(version)?,
- info: info.clone(),
- });
- }
- None => {
- bail!(
- "Could not find version '{}' referenced in dist-tag '{}'.",
- version,
- tag,
- )
- }
- }
- } else {
- bail!("Could not find dist-tag '{}'.", tag)
- }
+ tag_to_version_info(info, tag)
} else {
+ let mut maybe_best_version: Option<VersionAndInfo> = None;
for version_info in info.versions.values() {
let version = NpmVersion::parse(&version_info.version)?;
if version_matcher.matches(&version) {
@@ -976,36 +972,73 @@ fn get_resolved_package_version_and_info(
if is_best_version {
maybe_best_version = Some(VersionAndInfo {
version,
- info: version_info.clone(),
+ info: version_info,
});
}
}
}
- }
- match maybe_best_version {
- Some(v) => Ok(v),
- // If the package isn't found, it likely means that the user needs to use
- // `--reload` to get the latest npm package information. Although it seems
- // like we could make this smart by fetching the latest information for
- // this package here, we really need a full restart. There could be very
- // interesting bugs that occur if this package's version was resolved by
- // something previous using the old information, then now being smart here
- // causes a new fetch of the package information, meaning this time the
- // previous resolution of this package's version resolved to an older
- // version, but next time to a different version because it has new information.
- None => bail!(
- concat!(
- "Could not find npm package '{}' matching {}{}. ",
- "Try retrieving the latest npm package information by running with --reload",
+ match maybe_best_version {
+ Some(v) => Ok(v),
+ // If the package isn't found, it likely means that the user needs to use
+ // `--reload` to get the latest npm package information. Although it seems
+ // like we could make this smart by fetching the latest information for
+ // this package here, we really need a full restart. There could be very
+ // interesting bugs that occur if this package's version was resolved by
+ // something previous using the old information, then now being smart here
+ // causes a new fetch of the package information, meaning this time the
+ // previous resolution of this package's version resolved to an older
+ // version, but next time to a different version because it has new information.
+ None => bail!(
+ concat!(
+ "Could not find npm package '{}' matching {}{}. ",
+ "Try retrieving the latest npm package information by running with --reload",
+ ),
+ pkg_name,
+ version_matcher.version_text(),
+ match parent {
+ Some(id) => format!(" as specified in {}", id.display()),
+ None => String::new(),
+ }
),
- pkg_name,
- version_matcher.version_text(),
- match parent {
- Some(id) => format!(" as specified in {}", id.display()),
- None => String::new(),
+ }
+ }
+}
+
+fn version_req_satisfies(
+ version_req: &NpmVersionReq,
+ version: &NpmVersion,
+ package_info: &NpmPackageInfo,
+) -> Result<bool, AnyError> {
+ match version_req.tag() {
+ Some(tag) => {
+ let tag_version = tag_to_version_info(package_info, tag)?.version;
+ Ok(tag_version == *version)
+ }
+ None => Ok(version_req.matches(version)),
+ }
+}
+
+fn tag_to_version_info<'a>(
+ info: &'a NpmPackageInfo,
+ tag: &str,
+) -> Result<VersionAndInfo<'a>, AnyError> {
+ if let Some(version) = info.dist_tags.get(tag) {
+ match info.versions.get(version) {
+ Some(info) => Ok(VersionAndInfo {
+ version: NpmVersion::parse(version)?,
+ info,
+ }),
+ None => {
+ bail!(
+ "Could not find version '{}' referenced in dist-tag '{}'.",
+ version,
+ tag,
+ )
}
- ),
+ }
+ } else {
+ bail!("Could not find dist-tag '{}'.", tag)
}
}
@@ -1022,17 +1055,18 @@ mod test {
fn test_get_resolved_package_version_and_info() {
// dist tag where version doesn't exist
let package_ref = NpmPackageReference::from_str("npm:test").unwrap();
+ let package_info = NpmPackageInfo {
+ name: "test".to_string(),
+ versions: HashMap::new(),
+ dist_tags: HashMap::from([(
+ "latest".to_string(),
+ "1.0.0-alpha".to_string(),
+ )]),
+ };
let result = get_resolved_package_version_and_info(
"test",
&package_ref.req,
- &NpmPackageInfo {
- name: "test".to_string(),
- versions: HashMap::new(),
- dist_tags: HashMap::from([(
- "latest".to_string(),
- "1.0.0-alpha".to_string(),
- )]),
- },
+ &package_info,
None,
);
assert_eq!(
@@ -1042,26 +1076,27 @@ mod test {
// dist tag where version is a pre-release
let package_ref = NpmPackageReference::from_str("npm:test").unwrap();
+ let package_info = NpmPackageInfo {
+ name: "test".to_string(),
+ versions: HashMap::from([
+ ("0.1.0".to_string(), NpmPackageVersionInfo::default()),
+ (
+ "1.0.0-alpha".to_string(),
+ NpmPackageVersionInfo {
+ version: "0.1.0-alpha".to_string(),
+ ..Default::default()
+ },
+ ),
+ ]),
+ dist_tags: HashMap::from([(
+ "latest".to_string(),
+ "1.0.0-alpha".to_string(),
+ )]),
+ };
let result = get_resolved_package_version_and_info(
"test",
&package_ref.req,
- &NpmPackageInfo {
- name: "test".to_string(),
- versions: HashMap::from([
- ("0.1.0".to_string(), NpmPackageVersionInfo::default()),
- (
- "1.0.0-alpha".to_string(),
- NpmPackageVersionInfo {
- version: "0.1.0-alpha".to_string(),
- ..Default::default()
- },
- ),
- ]),
- dist_tags: HashMap::from([(
- "latest".to_string(),
- "1.0.0-alpha".to_string(),
- )]),
- },
+ &package_info,
None,
);
assert_eq!(result.unwrap().version.to_string(), "1.0.0-alpha");
@@ -2226,6 +2261,80 @@ mod test {
);
}
+ #[tokio::test]
+ async fn resolve_dep_and_peer_dist_tag() {
+ let api = TestNpmRegistryApi::default();
+ 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");
+ 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"));
+ 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 {
+ id: NpmPackageId::from_serialized("package-a@1.0.0_package-d@1.0.0")
+ .unwrap(),
+ copy_index: 0,
+ dependencies: HashMap::from([
+ (
+ "package-b".to_string(),
+ NpmPackageId::from_serialized("package-b@2.0.0").unwrap(),
+ ),
+ (
+ "package-c".to_string(),
+ NpmPackageId::from_serialized("package-c@1.0.0_package-d@1.0.0")
+ .unwrap(),
+ ),
+ (
+ "package-d".to_string(),
+ NpmPackageId::from_serialized("package-d@1.0.0").unwrap(),
+ ),
+ ]),
+ dist: Default::default(),
+ },
+ NpmResolutionPackage {
+ id: NpmPackageId::from_serialized("package-b@2.0.0").unwrap(),
+ copy_index: 0,
+ dependencies: HashMap::new(),
+ dist: Default::default(),
+ },
+ NpmResolutionPackage {
+ id: NpmPackageId::from_serialized("package-c@1.0.0_package-d@1.0.0")
+ .unwrap(),
+ copy_index: 0,
+ dependencies: HashMap::from([(
+ "package-d".to_string(),
+ NpmPackageId::from_serialized("package-d@1.0.0").unwrap(),
+ ),]),
+ dist: Default::default(),
+ },
+ NpmResolutionPackage {
+ id: NpmPackageId::from_serialized("package-d@1.0.0").unwrap(),
+ copy_index: 0,
+ dependencies: HashMap::new(),
+ dist: Default::default(),
+ },
+ ]
+ );
+ assert_eq!(
+ package_reqs,
+ vec![(
+ "package-a@1.0".to_string(),
+ "package-a@1.0.0_package-d@1.0.0".to_string()
+ ),]
+ );
+ }
+
async fn run_resolver_and_get_output(
api: TestNpmRegistryApi,
reqs: Vec<&str>,