Skip to content
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
14 changes: 13 additions & 1 deletion src/cargo/core/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,19 @@ impl Requirements<'_> {
}

fn require_crate_feature(&mut self, package: InternedString, feat: InternedString) {
self.used.insert(package);
// 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
.summary
.dependencies()
.iter()
.find(|p| p.name_in_toml() == package)
{
if dep.is_optional() {
self.used.insert(package);
}
}
self.deps
.entry(package)
.or_insert((false, BTreeSet::new()))
Expand Down
41 changes: 41 additions & 0 deletions tests/testsuite/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1870,3 +1870,44 @@ fn warn_if_default_features() {
"#.trim(),
).run();
}

#[test]
fn no_feature_for_non_optional_dep() {
let p = project()
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.0.1"
authors = []

[dependencies]
bar = { path = "bar" }
"#,
)
.file(
"src/main.rs",
r#"
#[cfg(not(feature = "bar"))]
fn main() {
}
"#,
)
.file(
"bar/Cargo.toml",
r#"
[project]
name = "bar"
version = "0.0.1"
authors = []

[features]
a = []
"#,
)
.file("bar/src/lib.rs", "pub fn bar() {}")
.build();

p.cargo("build --features bar/a").run();
}