Skip to content

Commit

Permalink
fix(cargo): Add a warning on package and project in the same Cargo.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Sep 23, 2022
1 parent 0c05e14 commit 76e9e1b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,8 +1578,19 @@ impl TomlManifest {
let cargo_features = me.cargo_features.as_ref().unwrap_or(&empty);
let features = Features::new(cargo_features, config, &mut warnings, source_id.is_path())?;

let package = me.project.clone().or_else(|| me.package.clone());
let package = &mut package.ok_or_else(|| anyhow!("no `package` section found"))?;
let mut package = match (&me.package, &me.project) {
(Some(_), Some(project)) => {
config.shell().warn(format!(
"manifest at `{}` contains both `project` and `package`, \
this could become a hard error in the future",
package_root.display()
))?;
project.clone()
}
(Some(package), None) => package.clone(),
(None, Some(project)) => project.clone(),
(None, None) => bail!("no `package` section found"),
};

let workspace_config = match (me.workspace.as_ref(), package.workspace.as_ref()) {
(Some(toml_config), None) => {
Expand Down
30 changes: 30 additions & 0 deletions tests/testsuite/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,3 +1024,33 @@ fn rustc_workspace_wrapper_excludes_published_deps() {
.with_stdout_does_not_contain("WRAPPER CALLED: rustc --crate-name baz [..]")
.run();
}

#[cargo_test]
fn warn_manifest_package_and_project() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
[project]
name = "foo"
version = "0.0.1"
"#,
)
.file("src/main.rs", "fn main() {}")
.build();

p.cargo("check")
.with_status(0)
.with_stderr(
"\
[WARNING] manifest at `[CWD]` contains both `project` and `package`, this could become a hard error in the future
[CHECKING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
)
.run();
}

0 comments on commit 76e9e1b

Please sign in to comment.