Skip to content

Commit

Permalink
Fix order-dependent feature resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Jun 22, 2020
1 parent 56636e9 commit ab73b2c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/cargo/core/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,13 @@ impl Requirements<'_> {
// If `package` is indeed an optional dependency then we activate the
// feature named `package`, but otherwise if `package` is a required
// dependency then there's no feature associated with it.
if let Some(dep) = self
if self
.summary
.dependencies()
.iter()
.find(|p| p.name_in_toml() == package)
.any(|dep| dep.name_in_toml() == package && dep.is_optional())
{
if dep.is_optional() {
self.used.insert(package);
}
self.used.insert(package);
}
self.deps
.entry(package)
Expand Down
79 changes: 78 additions & 1 deletion tests/testsuite/features.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Tests for `[features]` table.

use cargo_test_support::paths::CargoPathExt;
use cargo_test_support::registry::Package;
use cargo_test_support::registry::{Dependency, Package};
use cargo_test_support::{basic_manifest, project};

#[cargo_test]
Expand Down Expand Up @@ -2113,3 +2113,80 @@ fn slash_optional_enables() {

p.cargo("check --features dep/feat").run();
}

#[cargo_test]
fn registry_summary_order_doesnt_matter() {
// Checks for an issue where the resolver depended on the order of entries
// in the registry summary. If there was a non-optional dev-dependency
// that appeared before an optional normal dependency, then the resolver
// would not activate the optional dependency with a pkg/featname feature
// syntax.
Package::new("dep", "0.1.0")
.feature("feat1", &[])
.file(
"src/lib.rs",
r#"
#[cfg(feature="feat1")]
pub fn work() {
println!("it works");
}
"#,
)
.publish();
Package::new("bar", "0.1.0")
.feature("bar_feat", &["dep/feat1"])
.add_dep(Dependency::new("dep", "0.1.0").dev())
.add_dep(Dependency::new("dep", "0.1.0").optional(true))
.file(
"src/lib.rs",
r#"
// This will fail to compile without `dep` optional dep activated.
extern crate dep;
pub fn doit() {
dep::work();
}
"#,
)
.publish();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[dependencies]
bar = { version="0.1", features = ["bar_feat"] }
"#,
)
.file(
"src/main.rs",
r#"
fn main() {
bar::doit();
}
"#,
)
.build();

p.cargo("run")
.with_stderr(
"\
[UPDATING] [..]
[DOWNLOADING] crates ...
[DOWNLOADED] [..]
[DOWNLOADED] [..]
[COMPILING] dep v0.1.0
[COMPILING] bar v0.1.0
[COMPILING] foo v0.1.0 [..]
[FINISHED] [..]
[RUNNING] `target/debug/foo[EXE]`
",
)
.with_stdout("it works")
.run();
}

0 comments on commit ab73b2c

Please sign in to comment.