Skip to content

Commit

Permalink
Fix Failed to match errors in cargo-geiger (#263)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ilyatrefilov authored Feb 19, 2022
1 parent dd15051 commit 086033b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
7 changes: 5 additions & 2 deletions cargo-geiger/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
);
}

Expand Down Expand Up @@ -104,6 +106,7 @@ fn add_package_dependencies_to_graph(
graph_configuration: &GraphConfiguration,
graph: &mut Graph,
pending_packages: &mut Vec<PackageId>,
is_root_package: bool,
) {
let index = graph.nodes[&package_id];

Expand All @@ -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)) => {
Expand Down
6 changes: 5 additions & 1 deletion cargo-geiger/src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub trait DepsNotReplaced {
fn deps_not_replaced<T: ToCargoMetadataPackage + Display>(
&self,
package_id: &T,
is_root_package: bool,
) -> Option<Vec<(CargoMetadataPackageId, HashSet<CargoMetadataDependency>)>>;
}

Expand Down Expand Up @@ -72,7 +73,10 @@ pub trait GetPackageRoot: GetPackageInformation {
}

pub trait MatchesIgnoringSource {
fn matches_ignoring_source<T: GetNodeForKid, U: GetPackageIdInformation>(
fn matches_ignoring_source<
T: GetNodeForKid,
U: GetPackageIdInformation + Display,
>(
&self,
krates: &T,
package_id: &U,
Expand Down
19 changes: 14 additions & 5 deletions cargo-geiger/src/mapping/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl DepsNotReplaced for Metadata {
fn deps_not_replaced<T: ToCargoMetadataPackage + Display>(
&self,
package_id: &T,
is_root_package: bool,
) -> Option<Vec<(CargoMetadataPackageId, HashSet<CargoMetadataDependency>)>>
{
let mut cargo_metadata_deps_not_replaced = vec![];
Expand All @@ -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(),
Expand All @@ -58,7 +65,10 @@ impl DepsNotReplaced for Metadata {
}

impl MatchesIgnoringSource for CargoMetadataDependency {
fn matches_ignoring_source<T: GetNodeForKid, U: GetPackageIdInformation>(
fn matches_ignoring_source<
T: GetNodeForKid,
U: GetPackageIdInformation + Display,
>(
&self,
krates: &T,
package_id: &U,
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 086033b

Please sign in to comment.