Skip to content
Open
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/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,13 +577,25 @@ fn normalize_toml(
normalized_toml.badges = original_toml.badges.clone();
} else {
if let Some(field) = original_toml.requires_package().next() {
bail!("this virtual manifest specifies a `{field}` section, which is not allowed");
return Err(field_requires_package_error(field));
}
}

Ok(normalized_toml)
}

fn field_requires_package_error(field: &str) -> anyhow::Error {
let (table_open, table_close) = if matches!(field, "bin" | "example" | "test" | "bench") {
("[[", "]]")
} else {
("[", "]")
};
let toml_table = format_args!("{table_open}{field}{table_close}");
anyhow!(
"this virtual manifest does not have a `[package]` section, which is required when specifying the `{toml_table}` section"
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, didn't notice the wording change before because of everything else that changed (which is one reason why splitting up changes into individual commits can be helpful).

You are right that the old message is bad, focusing on terminology that is unlikely to be meaningful to the user. Something both messages have in common though is they are indirect about the problem at hand

e.g. the old message should have said something like:

`{field}` section is invalid in virtual manifests

Looking at the new wording, one possible suggestion is:

`{toml_table}` requires a `[package]` section

or

`[package]` section is missing
note: required by `{toml_table}`

When we switch this to annotate snippets, we could move the note to an annotation on the {toml_table} in the source.

)
}

fn normalize_patch<'a>(
gctx: &GlobalContext,
original_patch: Option<&BTreeMap<String, BTreeMap<PackageName, TomlDependency>>>,
Expand Down
3 changes: 1 addition & 2 deletions tests/testsuite/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2296,7 +2296,6 @@ fn ws_err_unused() {
"[badges]",
"[lints]",
] {
let key = table.trim_start_matches('[').trim_end_matches(']');
let p = project()
.file(
"Cargo.toml",
Expand All @@ -2319,7 +2318,7 @@ fn ws_err_unused() {
[ERROR] failed to parse manifest at `[..]/foo/Cargo.toml`

Caused by:
this virtual manifest specifies a `{key}` section, which is not allowed
this virtual manifest does not have a `[package]` section, which is required when specifying the `{table}` section
",
))
.run();
Expand Down