fix(cargo): Add a warning on package and project in the same Cargo.toml

This commit is contained in:
Scott Schafer 2022-09-26 10:32:07 -06:00
parent 185054946b
commit cabee4f511
2 changed files with 96 additions and 3 deletions

View file

@ -1578,8 +1578,21 @@ 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)) => {
if source_id.is_path() {
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) => {

View file

@ -6,7 +6,7 @@ use cargo_test_support::install::exe;
use cargo_test_support::paths::CargoPathExt;
use cargo_test_support::registry::Package;
use cargo_test_support::tools;
use cargo_test_support::{basic_manifest, project};
use cargo_test_support::{basic_manifest, git, project};
#[cargo_test]
fn check_success() {
@ -1024,3 +1024,83 @@ 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_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();
}
#[cargo_test]
fn git_manifest_package_and_project() {
let p = project();
let git_project = git::new("bar", |p| {
p.file(
"Cargo.toml",
r#"
[package]
name = "bar"
version = "0.0.1"
[project]
name = "bar"
version = "0.0.1"
"#,
)
.file("src/lib.rs", "")
});
let p = p
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.0.1"
[dependencies.bar]
version = "0.0.1"
git = '{}'
"#,
git_project.url()
),
)
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("check")
.with_stderr(
"\
[UPDATING] git repository `[..]`
[CHECKING] bar v0.0.1 ([..])
[CHECKING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
)
.run();
}