Fix issue with filtering exclusive target dependencies.

This commit is contained in:
Eric Huss 2021-03-11 11:41:00 -08:00
parent 80c47b7686
commit 8ce2a1bef3
2 changed files with 63 additions and 30 deletions

View file

@ -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();
@ -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
@ -780,7 +772,12 @@ 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()
@ -788,6 +785,9 @@ impl<'a, 'cfg> State<'a, 'cfg> {
.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) {

View file

@ -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();
}