Skip to content

Commit

Permalink
Take into account DependencyKind when collecting package target depen…
Browse files Browse the repository at this point in the history
…dencies (#2235)

When filtering out disabled dependencies for the Bazel targets,
`collect_deps_selectable()` now takes into account the kind of
dependency in question. Previously, if a disabled optional normal
dependency was mandatory for a build script, it was erroneously filtered
out.

This change resolves #2059
  • Loading branch information
dmitrii-ubskii committed Nov 3, 2023
1 parent 9341d1f commit d680d81
Show file tree
Hide file tree
Showing 5 changed files with 5,632 additions and 8 deletions.
41 changes: 33 additions & 8 deletions crate_universe/src/metadata/dependency.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Gathering dependencies is the largest part of annotating.

use anyhow::{bail, Result};
use cargo_metadata::{Metadata as CargoMetadata, Node, NodeDep, Package, PackageId};
use cargo_metadata::{
DependencyKind, Metadata as CargoMetadata, Node, NodeDep, Package, PackageId,
};
use cargo_platform::Platform;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -47,8 +49,8 @@ impl DependencySet {
.partition(|dep| is_dev_dependency(dep));

(
collect_deps_selectable(node, dev, metadata),
collect_deps_selectable(node, normal, metadata),
collect_deps_selectable(node, dev, metadata, DependencyKind::Normal),
collect_deps_selectable(node, normal, metadata, DependencyKind::Normal),
)
};

Expand All @@ -63,8 +65,8 @@ impl DependencySet {
.partition(|dep| is_dev_dependency(dep));

(
collect_deps_selectable(node, dev, metadata),
collect_deps_selectable(node, normal, metadata),
collect_deps_selectable(node, dev, metadata, DependencyKind::Development),
collect_deps_selectable(node, normal, metadata, DependencyKind::Development),
)
};

Expand All @@ -81,8 +83,8 @@ impl DependencySet {
.partition(|dep| is_proc_macro_package(&metadata[&dep.pkg]));

(
collect_deps_selectable(node, proc_macro, metadata),
collect_deps_selectable(node, normal, metadata),
collect_deps_selectable(node, proc_macro, metadata, DependencyKind::Build),
collect_deps_selectable(node, normal, metadata, DependencyKind::Build),
)
};

Expand Down Expand Up @@ -126,6 +128,7 @@ fn is_optional_crate_enabled(
dep: &NodeDep,
target: Option<&Platform>,
metadata: &CargoMetadata,
kind: DependencyKind,
) -> bool {
let pkg = &metadata[&parent.id];

Expand All @@ -141,6 +144,7 @@ fn is_optional_crate_enabled(
if let Some(toml_dep) = pkg
.dependencies
.iter()
.filter(|&d| d.kind == kind)
.filter(|&d| d.target.as_ref() == target)
.filter(|&d| d.optional)
.find(|&d| sanitize_module_name(&d.name) == dep.name)
Expand All @@ -155,6 +159,7 @@ fn collect_deps_selectable(
node: &Node,
deps: Vec<&NodeDep>,
metadata: &cargo_metadata::Metadata,
kind: DependencyKind,
) -> SelectList<Dependency> {
let mut selectable = SelectList::default();

Expand All @@ -165,7 +170,7 @@ fn collect_deps_selectable(
let alias = get_target_alias(&dep.name, dep_pkg);

for kind_info in &dep.dep_kinds {
if is_optional_crate_enabled(node, dep, kind_info.target.as_ref(), metadata) {
if is_optional_crate_enabled(node, dep, kind_info.target.as_ref(), metadata, kind) {
selectable.insert(
Dependency {
package_id: dep.pkg.clone(),
Expand Down Expand Up @@ -684,4 +689,24 @@ mod test {
.expect("Iterating over known keys should never panic")
.any(|dep| { dep.target_name == "mio" }));
}

#[test]
fn optional_deps_disabled_build_dep_enabled() {
let metadata = metadata::optional_deps_disabled_build_dep_enabled();

let node = find_metadata_node("gherkin", &metadata);
let dependencies = DependencySet::new_for_node(node, &metadata);

assert!(!dependencies
.normal_deps
.get_iter(None)
.expect("Iterating over known keys should never panic")
.any(|dep| dep.target_name == "serde"));

assert!(dependencies
.build_deps
.get_iter(None)
.expect("Iterating over known keys should never panic")
.any(|dep| dep.target_name == "serde"));
}
}
8 changes: 8 additions & 0 deletions crate_universe/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ pub mod metadata {
.unwrap()
}

pub fn optional_deps_disabled_build_dep_enabled() -> cargo_metadata::Metadata {
serde_json::from_str(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/test_data/metadata/crate_optional_deps_disabled_build_dep_enabled/metadata.json"
)))
.unwrap()
}

pub fn optional_deps_enabled() -> cargo_metadata::Metadata {
serde_json::from_str(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "crate-with-optional-deps"
version = "0.1.0"
edition = "2021" # make sure resolver=2 is enabled for this test

# Required to satisfy cargo but no `lib.rs` is expected to
# exist within test data.
[lib]
path = "lib.rs"

[dependencies]
gherkin = "=0.13.0"
Loading

0 comments on commit d680d81

Please sign in to comment.