From 086033b1357a8640a138a6738f5c5dcb65e111f4 Mon Sep 17 00:00:00 2001 From: Ilya Trefilov Date: Sat, 19 Feb 2022 15:16:18 +0300 Subject: [PATCH] Fix `Failed to match` errors in cargo-geiger (#263) This fixes the issue with `Failed to match` errors, which was caused by cargo-metadata behaviour of not resolving dev-dependencies of crate dependency. Now we check if crate is root and if not, we won't collect dev deps for this package, cause they are anyways not in krates graph, which relies on cargo-metadata `resolve` output field. Also changed argument for `Failed to match` error, cause it mentioned not the real one unmatched package. Relates to #240 --- cargo-geiger/src/graph.rs | 7 +++++-- cargo-geiger/src/mapping.rs | 6 +++++- cargo-geiger/src/mapping/metadata.rs | 19 ++++++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/cargo-geiger/src/graph.rs b/cargo-geiger/src/graph.rs index c6e52715..fad7cce8 100644 --- a/cargo-geiger/src/graph.rs +++ b/cargo-geiger/src/graph.rs @@ -51,7 +51,7 @@ pub fn build_graph<'a>( graph.graph.add_node(root_package_id.clone()), ); - let mut pending_packages = vec![root_package_id]; + let mut pending_packages = vec![root_package_id.clone()]; let graph_configuration = GraphConfiguration { target, @@ -60,12 +60,14 @@ pub fn build_graph<'a>( }; while let Some(package_id) = pending_packages.pop() { + let is_root_package = package_id == root_package_id; add_package_dependencies_to_graph( cargo_metadata_parameters, package_id, &graph_configuration, &mut graph, &mut pending_packages, + is_root_package, ); } @@ -104,6 +106,7 @@ fn add_package_dependencies_to_graph( graph_configuration: &GraphConfiguration, graph: &mut Graph, pending_packages: &mut Vec, + is_root_package: bool, ) { let index = graph.nodes[&package_id]; @@ -112,7 +115,7 @@ fn add_package_dependencies_to_graph( let dep_not_replaced_option = cargo_metadata_parameters .metadata - .deps_not_replaced(&package_id); + .deps_not_replaced(&package_id, is_root_package); match (krates_node_option, dep_not_replaced_option) { (Some(krates_node), Some(dependencies)) => { diff --git a/cargo-geiger/src/mapping.rs b/cargo-geiger/src/mapping.rs index 1f56b15a..857f5d67 100644 --- a/cargo-geiger/src/mapping.rs +++ b/cargo-geiger/src/mapping.rs @@ -35,6 +35,7 @@ pub trait DepsNotReplaced { fn deps_not_replaced( &self, package_id: &T, + is_root_package: bool, ) -> Option)>>; } @@ -72,7 +73,10 @@ pub trait GetPackageRoot: GetPackageInformation { } pub trait MatchesIgnoringSource { - fn matches_ignoring_source( + fn matches_ignoring_source< + T: GetNodeForKid, + U: GetPackageIdInformation + Display, + >( &self, krates: &T, package_id: &U, diff --git a/cargo-geiger/src/mapping/metadata.rs b/cargo-geiger/src/mapping/metadata.rs index 89448d42..5e371b83 100644 --- a/cargo-geiger/src/mapping/metadata.rs +++ b/cargo-geiger/src/mapping/metadata.rs @@ -27,6 +27,7 @@ impl DepsNotReplaced for Metadata { fn deps_not_replaced( &self, package_id: &T, + is_root_package: bool, ) -> Option)>> { let mut cargo_metadata_deps_not_replaced = vec![]; @@ -38,6 +39,12 @@ impl DepsNotReplaced for Metadata { if let Some(package_id) = dependency.to_cargo_metadata_package_id(self) { + if dependency.kind + == cargo_metadata::DependencyKind::Development + && !is_root_package + { + continue; + } if !package_id_hashset.contains(&package_id) { cargo_metadata_deps_not_replaced.push(( package_id.clone(), @@ -58,7 +65,10 @@ impl DepsNotReplaced for Metadata { } impl MatchesIgnoringSource for CargoMetadataDependency { - fn matches_ignoring_source( + fn matches_ignoring_source< + T: GetNodeForKid, + U: GetPackageIdInformation + Display, + >( &self, krates: &T, package_id: &U, @@ -69,9 +79,8 @@ impl MatchesIgnoringSource for CargoMetadataDependency { } _ => { eprintln!( - "Failed to match (ignoring source) package: {} with version: {}", - self.name, - self.req + "Failed to match (ignoring source) package: {} ", + package_id ); None } @@ -179,7 +188,7 @@ mod metadata_tests { let deps_not_replaced = resolve.deps_not_replaced(package.package_id()); let cargo_metadata_deps_not_replaced = metadata - .deps_not_replaced(&cargo_metadata_package_id) + .deps_not_replaced(&cargo_metadata_package_id, true) .unwrap(); let mut cargo_core_package_names = deps_not_replaced