Skip to content

fix: improve message for inactive weak optional feature with edition2024 #14019

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

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 7 additions & 0 deletions src/cargo/core/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ fn build_feature_map(

// Validation of the feature name will be performed in the resolver.
if !is_any_dep {
// editon2024 stops expose implicit features, which will strip weak optional dependencies from `dependencies`
if *weak {
bail!(
"feature `{feature}` includes `{fv}`, activate it in a feature with `dep:{dep_name}` if `{dep_name}` is an enabled dependency"
);
}

bail!(
"feature `{}` includes `{}`, but `{}` is not a dependency",
feature,
Expand Down
69 changes: 69 additions & 0 deletions tests/testsuite/lints/unused_optional_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,72 @@ warning: unused optional dependency
)
.run();
}

#[cargo_test(nightly, reason = "edition2024 is not stable")]
fn inactive_weak_optional_dep() {
Package::new("dep", "0.1.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["edition2024"]
[package]
name = "foo"
version = "0.1.0"
edition = "2024"

[dependencies]

[features]
feat = ["dep?/feat"]
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("check -Zcargo-lints")
.masquerade_as_nightly_cargo(&["cargo-lints", "edition2024"])
.with_status(101)
.with_stderr(
"\
error: failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
feature `feat` includes `dep?/feat`, activate it in a feature with `dep:dep` if `dep` is an enabled dependency
",
)
.run();

let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["edition2024"]
[package]
name = "foo"
version = "0.1.0"
edition = "2024"

[dependencies]
dep = { version = "0.1.0", optional = true }

[features]
feat = ["dep?/feat"]
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("check -Zcargo-lints")
.masquerade_as_nightly_cargo(&["cargo-lints", "edition2024"])
.with_status(101)
.with_stderr(
"\
error: failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
feature `feat` includes `dep?/feat`, activate it in a feature with `dep:dep` if `dep` is an enabled dependency
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if dep is an enabled dependency

I feel the wording could be improved here. To help in coming up with ideas, could you expand on what you are trying to convey

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the messge need to include the cases that dependency inactive and dependency missing, if dep is an enabled dependency means the former.

#14026 is another PR to distinguish these. And this PR may require more detailed wording to address both cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest keep this PR to polish the message unless #14026 is merged

",
)
.run();
}