Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with filtering exclusive target dependencies. #9255

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,33 +235,28 @@ fn compute_deps(
}

let id = unit.pkg.package_id();
let filtered_deps = state
.deps(unit, unit_for)
.into_iter()
.filter(|&(_id, deps)| {
deps.iter().any(|dep| {
// If this target is a build command, then we only want build
// dependencies, otherwise we want everything *other than* build
// dependencies.
if unit.target.is_custom_build() != dep.is_build() {
return false;
}
let filtered_deps = state.deps(unit, unit_for, &|dep| {
// If this target is a build command, then we only want build
// dependencies, otherwise we want everything *other than* build
// dependencies.
if unit.target.is_custom_build() != dep.is_build() {
return false;
}

// If this dependency is **not** a transitive dependency, then it
// only applies to test/example targets.
if !dep.is_transitive()
&& !unit.target.is_test()
&& !unit.target.is_example()
&& !unit.mode.is_any_test()
{
return false;
}
// If this dependency is **not** a transitive dependency, then it
// only applies to test/example targets.
if !dep.is_transitive()
&& !unit.target.is_test()
&& !unit.target.is_example()
&& !unit.mode.is_any_test()
{
return false;
}

// If we've gotten past all that, then this dependency is
// actually used!
true
})
});
// If we've gotten past all that, then this dependency is
// actually used!
true
});

let mut ret = Vec::new();
let mut dev_deps = Vec::new();
Expand Down Expand Up @@ -419,10 +414,7 @@ fn compute_deps_doc(
state: &mut State<'_, '_>,
unit_for: UnitFor,
) -> CargoResult<Vec<UnitDep>> {
let deps = state
.deps(unit, unit_for)
.into_iter()
.filter(|&(_id, deps)| deps.iter().any(|dep| dep.kind() == DepKind::Normal));
let deps = state.deps(unit, unit_for, &|dep| dep.kind() == DepKind::Normal);

// To document a library, we depend on dependencies actually being
// built. If we're documenting *all* libraries, then we also depend on
Expand Down Expand Up @@ -780,14 +772,22 @@ impl<'a, 'cfg> State<'a, 'cfg> {
}

/// Returns a filtered set of dependencies for the given unit.
fn deps(&self, unit: &Unit, unit_for: UnitFor) -> Vec<(PackageId, &HashSet<Dependency>)> {
fn deps(
&self,
unit: &Unit,
unit_for: UnitFor,
filter: &dyn Fn(&Dependency) -> bool,
) -> Vec<(PackageId, &HashSet<Dependency>)> {
let pkg_id = unit.pkg.package_id();
let kind = unit.kind;
self.resolve()
.deps(pkg_id)
.filter(|&(_id, deps)| {
assert!(!deps.is_empty());
deps.iter().any(|dep| {
if !filter(dep) {
return false;
}
// If this dependency is only available for certain platforms,
// make sure we're only enabling it for that platform.
if !self.target_data.dep_platform_activated(dep, kind) {
Expand Down
33 changes: 33 additions & 0 deletions tests/testsuite/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,36 @@ Caused by:
)
.run();
}

#[cargo_test]
fn exclusive_dep_kinds() {
// Checks for a bug where the same package with different cfg expressions
// was not being filtered correctly.
Package::new("bar", "1.0.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"

[target.'cfg(abc)'.dependencies]
bar = "1.0"

[target.'cfg(not(abc))'.build-dependencies]
bar = "1.0"
"#,
)
.file("src/lib.rs", "")
.file("build.rs", "extern crate bar; fn main() {}")
.build();

p.cargo("check").run();
p.change_file("src/lib.rs", "extern crate bar;");
p.cargo("check")
.with_status(101)
// can't find crate for `bar`
.with_stderr_contains("[..]E0463[..]")
.run();
}