diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 023a5b5c1e7..8de66b0ef42 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -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" + ) +} + fn normalize_patch<'a>( gctx: &GlobalContext, original_patch: Option<&BTreeMap>>, diff --git a/tests/testsuite/workspaces.rs b/tests/testsuite/workspaces.rs index 0f8ab785baf..7ca3272833e 100644 --- a/tests/testsuite/workspaces.rs +++ b/tests/testsuite/workspaces.rs @@ -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", @@ -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();