From 5b2b5f9598e427d507d4cdfa6878ffc883bf4a74 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 7 Nov 2018 09:00:54 -0800 Subject: [PATCH] Warn/err on some unused manifest keys in workspaces. --- src/cargo/core/workspace.rs | 26 ++++++---- src/cargo/util/toml/mod.rs | 18 +++++++ tests/testsuite/workspaces.rs | 90 +++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 8 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index e6fc45d091d..3fccc4ee081 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -658,18 +658,28 @@ impl<'cfg> Workspace<'cfg> { for pkg in self.members() .filter(|p| p.manifest_path() != root_manifest) { - if pkg.manifest().original().has_profiles() { - let message = &format!( - "profiles for the non root package will be ignored, \ - specify profiles at the workspace root:\n\ + let manifest = pkg.manifest(); + let emit_warning = |what| -> CargoResult<()> { + let msg = format!( + "{} for the non root package will be ignored, \ + specify {} at the workspace root:\n\ package: {}\n\ workspace: {}", + what, + what, pkg.manifest_path().display(), - root_manifest.display() + root_manifest.display(), ); - - //TODO: remove `Eq` bound from `Profiles` when the warning is removed. - self.config.shell().warn(&message)?; + self.config.shell().warn(&msg) + }; + if manifest.original().has_profiles() { + emit_warning("profiles")?; + } + if !manifest.replace().is_empty() { + emit_warning("replace")?; + } + if !manifest.patch().is_empty() { + emit_warning("patch")?; } } } diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index d19bac3740d..514dbbbc7d7 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1086,6 +1086,24 @@ impl TomlManifest { if me.bench.is_some() { bail!("virtual manifests do not specify [[bench]]"); } + if me.dependencies.is_some() { + bail!("virtual manifests do not specify [dependencies]"); + } + if me.dev_dependencies.is_some() || me.dev_dependencies2.is_some() { + bail!("virtual manifests do not specify [dev-dependencies]"); + } + if me.build_dependencies.is_some() || me.build_dependencies2.is_some() { + bail!("virtual manifests do not specify [build-dependencies]"); + } + if me.features.is_some() { + bail!("virtual manifests do not specify [features]"); + } + if me.target.is_some() { + bail!("virtual manifests do not specify [target]"); + } + if me.badges.is_some() { + bail!("virtual manifests do not specify [badges]"); + } let mut nested_paths = Vec::new(); let mut warnings = Vec::new(); diff --git a/tests/testsuite/workspaces.rs b/tests/testsuite/workspaces.rs index e9338487829..88398174d8a 100644 --- a/tests/testsuite/workspaces.rs +++ b/tests/testsuite/workspaces.rs @@ -1879,3 +1879,93 @@ fn ws_rustc_err() { .with_stderr("[ERROR] [..]against an actual package[..]") .run(); } + +#[test] +fn ws_err_unused() { + for key in &[ + "[lib]", + "[[bin]]", + "[[example]]", + "[[test]]", + "[[bench]]", + "[dependencies]", + "[dev-dependencies]", + "[build-dependencies]", + "[features]", + "[target]", + "[badges]", + ] { + let p = project() + .file( + "Cargo.toml", + &format!( + r#" + [workspace] + members = ["a"] + + {} + "#, + key + ), + ) + .file("a/Cargo.toml", &basic_lib_manifest("a")) + .file("a/src/lib.rs", "") + .build(); + p.cargo("check") + .with_status(101) + .with_stderr(&format!( + "\ +[ERROR] failed to parse manifest at `[..]/foo/Cargo.toml` + +Caused by: + virtual manifests do not specify {} +", + key + )) + .run(); + } +} + +#[test] +fn ws_warn_unused() { + for (key, name) in &[ + ("[profile.dev]\nopt-level = 1", "profiles"), + ("[replace]\n\"bar:0.1.0\" = { path = \"bar\" }", "replace"), + ("[patch.crates-io]\nbar = { path = \"bar\" }", "patch"), + ] { + let p = project() + .file( + "Cargo.toml", + r#" + [workspace] + members = ["a"] + "#, + ) + .file( + "a/Cargo.toml", + &format!( + r#" + [package] + name = "a" + version = "0.1.0" + + {} + "#, + key + ), + ) + .file("a/src/lib.rs", "") + .build(); + p.cargo("check") + .with_status(0) + .with_stderr_contains(&format!( + "\ +[WARNING] {} for the non root package will be ignored, specify {} at the workspace root: +package: [..]/foo/a/Cargo.toml +workspace: [..]/foo/Cargo.toml +", + name, name + )) + .run(); + } +}