Skip to content

Commit 237f86e

Browse files
committed
Auto merge of #6276 - ehuss:ws-unused, r=alexcrichton
Warn/err on some unused manifest keys in workspaces. - Err on dependencies, features, target, badges in virtual root. - Warn on replace/patch in members. Closes #6248, Closes #4870, Closes #4947
2 parents 077087a + 5b2b5f9 commit 237f86e

File tree

3 files changed

+126
-8
lines changed

3 files changed

+126
-8
lines changed

src/cargo/core/workspace.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -658,18 +658,28 @@ impl<'cfg> Workspace<'cfg> {
658658
for pkg in self.members()
659659
.filter(|p| p.manifest_path() != root_manifest)
660660
{
661-
if pkg.manifest().original().has_profiles() {
662-
let message = &format!(
663-
"profiles for the non root package will be ignored, \
664-
specify profiles at the workspace root:\n\
661+
let manifest = pkg.manifest();
662+
let emit_warning = |what| -> CargoResult<()> {
663+
let msg = format!(
664+
"{} for the non root package will be ignored, \
665+
specify {} at the workspace root:\n\
665666
package: {}\n\
666667
workspace: {}",
668+
what,
669+
what,
667670
pkg.manifest_path().display(),
668-
root_manifest.display()
671+
root_manifest.display(),
669672
);
670-
671-
//TODO: remove `Eq` bound from `Profiles` when the warning is removed.
672-
self.config.shell().warn(&message)?;
673+
self.config.shell().warn(&msg)
674+
};
675+
if manifest.original().has_profiles() {
676+
emit_warning("profiles")?;
677+
}
678+
if !manifest.replace().is_empty() {
679+
emit_warning("replace")?;
680+
}
681+
if !manifest.patch().is_empty() {
682+
emit_warning("patch")?;
673683
}
674684
}
675685
}

src/cargo/util/toml/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,24 @@ impl TomlManifest {
10861086
if me.bench.is_some() {
10871087
bail!("virtual manifests do not specify [[bench]]");
10881088
}
1089+
if me.dependencies.is_some() {
1090+
bail!("virtual manifests do not specify [dependencies]");
1091+
}
1092+
if me.dev_dependencies.is_some() || me.dev_dependencies2.is_some() {
1093+
bail!("virtual manifests do not specify [dev-dependencies]");
1094+
}
1095+
if me.build_dependencies.is_some() || me.build_dependencies2.is_some() {
1096+
bail!("virtual manifests do not specify [build-dependencies]");
1097+
}
1098+
if me.features.is_some() {
1099+
bail!("virtual manifests do not specify [features]");
1100+
}
1101+
if me.target.is_some() {
1102+
bail!("virtual manifests do not specify [target]");
1103+
}
1104+
if me.badges.is_some() {
1105+
bail!("virtual manifests do not specify [badges]");
1106+
}
10891107

10901108
let mut nested_paths = Vec::new();
10911109
let mut warnings = Vec::new();

tests/testsuite/workspaces.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,3 +1879,93 @@ fn ws_rustc_err() {
18791879
.with_stderr("[ERROR] [..]against an actual package[..]")
18801880
.run();
18811881
}
1882+
1883+
#[test]
1884+
fn ws_err_unused() {
1885+
for key in &[
1886+
"[lib]",
1887+
"[[bin]]",
1888+
"[[example]]",
1889+
"[[test]]",
1890+
"[[bench]]",
1891+
"[dependencies]",
1892+
"[dev-dependencies]",
1893+
"[build-dependencies]",
1894+
"[features]",
1895+
"[target]",
1896+
"[badges]",
1897+
] {
1898+
let p = project()
1899+
.file(
1900+
"Cargo.toml",
1901+
&format!(
1902+
r#"
1903+
[workspace]
1904+
members = ["a"]
1905+
1906+
{}
1907+
"#,
1908+
key
1909+
),
1910+
)
1911+
.file("a/Cargo.toml", &basic_lib_manifest("a"))
1912+
.file("a/src/lib.rs", "")
1913+
.build();
1914+
p.cargo("check")
1915+
.with_status(101)
1916+
.with_stderr(&format!(
1917+
"\
1918+
[ERROR] failed to parse manifest at `[..]/foo/Cargo.toml`
1919+
1920+
Caused by:
1921+
virtual manifests do not specify {}
1922+
",
1923+
key
1924+
))
1925+
.run();
1926+
}
1927+
}
1928+
1929+
#[test]
1930+
fn ws_warn_unused() {
1931+
for (key, name) in &[
1932+
("[profile.dev]\nopt-level = 1", "profiles"),
1933+
("[replace]\n\"bar:0.1.0\" = { path = \"bar\" }", "replace"),
1934+
("[patch.crates-io]\nbar = { path = \"bar\" }", "patch"),
1935+
] {
1936+
let p = project()
1937+
.file(
1938+
"Cargo.toml",
1939+
r#"
1940+
[workspace]
1941+
members = ["a"]
1942+
"#,
1943+
)
1944+
.file(
1945+
"a/Cargo.toml",
1946+
&format!(
1947+
r#"
1948+
[package]
1949+
name = "a"
1950+
version = "0.1.0"
1951+
1952+
{}
1953+
"#,
1954+
key
1955+
),
1956+
)
1957+
.file("a/src/lib.rs", "")
1958+
.build();
1959+
p.cargo("check")
1960+
.with_status(0)
1961+
.with_stderr_contains(&format!(
1962+
"\
1963+
[WARNING] {} for the non root package will be ignored, specify {} at the workspace root:
1964+
package: [..]/foo/a/Cargo.toml
1965+
workspace: [..]/foo/Cargo.toml
1966+
",
1967+
name, name
1968+
))
1969+
.run();
1970+
}
1971+
}

0 commit comments

Comments
 (0)