Skip to content

Commit 4434ed8

Browse files
authored
suggest workspace hints for boolean dependencies (#15507)
### What does this PR try to resolve? via issue #15505, Cargo currently errors on ```toml [dependencies] crc32fast = true ``` with a message about `expected a version string or a detailed dependency` It doesn’t hint that you can depend on a workspace member. This PR updates `cargo` so that when you write dep = true, the error also suggests: ```toml dep = { workspace = true } dep.workspace = true ``` ### How should we test and review this PR? In a workspace crate’s Cargo.toml, add ```toml [dependencies] crc32fast = true ``` Run `cargo build` and you should see the enhanced error with the two workspace hints. ### Additional information Regarding support for int/float, we only need a special case for boolean because they’re the only values that get the `workspace hint`. Everything else just uses the normal detailed dependency error
2 parents c0396fe + 5dd597b commit 4434ed8

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

crates/cargo-util-schemas/src/manifest/mod.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -787,12 +787,26 @@ impl<'de, P: Deserialize<'de> + Clone> de::Deserialize<'de> for TomlDependency<P
787787
where
788788
D: de::Deserializer<'de>,
789789
{
790+
use serde::de::Error as _;
791+
let expected = "a version string like \"0.9.8\" or a \
792+
detailed dependency like { version = \"0.9.8\" }";
790793
UntaggedEnumVisitor::new()
791-
.expecting(
792-
"a version string like \"0.9.8\" or a \
793-
detailed dependency like { version = \"0.9.8\" }",
794-
)
794+
.expecting(expected)
795795
.string(|value| Ok(TomlDependency::Simple(value.to_owned())))
796+
.bool(|value| {
797+
let expected = format!("invalid type: boolean `{value}`, expected {expected}");
798+
let err = if value {
799+
format!(
800+
"{expected}\n\
801+
note: if you meant to use a workspace member, you can write\n \
802+
dep.workspace = {value}"
803+
)
804+
} else {
805+
expected
806+
};
807+
808+
Err(serde_untagged::de::Error::custom(err))
809+
})
796810
.map(|value| value.deserialize().map(TomlDependency::Detailed))
797811
.deserialize(deserializer)
798812
}

tests/testsuite/bad_config.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,6 +2641,41 @@ fn bad_dependency() {
26412641
.run();
26422642
}
26432643

2644+
#[cargo_test]
2645+
fn bad_dependency_true_literal() {
2646+
let p = project()
2647+
.file(
2648+
"Cargo.toml",
2649+
r#"
2650+
[package]
2651+
name = "foo"
2652+
version = "0.0.0"
2653+
edition = "2015"
2654+
authors = []
2655+
2656+
[dependencies]
2657+
bar = true
2658+
"#,
2659+
)
2660+
.file("src/lib.rs", "")
2661+
.build();
2662+
2663+
p.cargo("check")
2664+
.with_status(101)
2665+
.with_stderr_data(str![[r#"
2666+
[ERROR] invalid type: boolean `true`, expected a version string like "0.9.8" or a detailed dependency like { version = "0.9.8" }
2667+
[NOTE] if you meant to use a workspace member, you can write
2668+
dep.workspace = true
2669+
--> Cargo.toml:9:23
2670+
|
2671+
9 | bar = true
2672+
| ^^^^
2673+
|
2674+
2675+
"#]])
2676+
.run();
2677+
}
2678+
26442679
#[cargo_test]
26452680
fn bad_debuginfo() {
26462681
let p = project()

0 commit comments

Comments
 (0)