Skip to content

Commit cabee4f

Browse files
committed
fix(cargo): Add a warning on package and project in the same Cargo.toml
1 parent 1850549 commit cabee4f

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,8 +1578,21 @@ impl TomlManifest {
15781578
let cargo_features = me.cargo_features.as_ref().unwrap_or(&empty);
15791579
let features = Features::new(cargo_features, config, &mut warnings, source_id.is_path())?;
15801580

1581-
let package = me.project.clone().or_else(|| me.package.clone());
1582-
let package = &mut package.ok_or_else(|| anyhow!("no `package` section found"))?;
1581+
let mut package = match (&me.package, &me.project) {
1582+
(Some(_), Some(project)) => {
1583+
if source_id.is_path() {
1584+
config.shell().warn(format!(
1585+
"manifest at `{}` contains both `project` and `package`, \
1586+
this could become a hard error in the future",
1587+
package_root.display()
1588+
))?;
1589+
}
1590+
project.clone()
1591+
}
1592+
(Some(package), None) => package.clone(),
1593+
(None, Some(project)) => project.clone(),
1594+
(None, None) => bail!("no `package` section found"),
1595+
};
15831596

15841597
let workspace_config = match (me.workspace.as_ref(), package.workspace.as_ref()) {
15851598
(Some(toml_config), None) => {

tests/testsuite/check.rs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cargo_test_support::install::exe;
66
use cargo_test_support::paths::CargoPathExt;
77
use cargo_test_support::registry::Package;
88
use cargo_test_support::tools;
9-
use cargo_test_support::{basic_manifest, project};
9+
use cargo_test_support::{basic_manifest, git, project};
1010

1111
#[cargo_test]
1212
fn check_success() {
@@ -1024,3 +1024,83 @@ fn rustc_workspace_wrapper_excludes_published_deps() {
10241024
.with_stdout_does_not_contain("WRAPPER CALLED: rustc --crate-name baz [..]")
10251025
.run();
10261026
}
1027+
1028+
#[cargo_test]
1029+
fn warn_manifest_package_and_project() {
1030+
let p = project()
1031+
.file(
1032+
"Cargo.toml",
1033+
r#"
1034+
[package]
1035+
name = "foo"
1036+
version = "0.0.1"
1037+
1038+
[project]
1039+
name = "foo"
1040+
version = "0.0.1"
1041+
"#,
1042+
)
1043+
.file("src/main.rs", "fn main() {}")
1044+
.build();
1045+
1046+
p.cargo("check")
1047+
.with_stderr(
1048+
"\
1049+
[WARNING] manifest at `[CWD]` contains both `project` and `package`, this could become a hard error in the future
1050+
[CHECKING] foo v0.0.1 ([CWD])
1051+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1052+
",
1053+
)
1054+
.run();
1055+
}
1056+
1057+
#[cargo_test]
1058+
fn git_manifest_package_and_project() {
1059+
let p = project();
1060+
let git_project = git::new("bar", |p| {
1061+
p.file(
1062+
"Cargo.toml",
1063+
r#"
1064+
[package]
1065+
name = "bar"
1066+
version = "0.0.1"
1067+
1068+
[project]
1069+
name = "bar"
1070+
version = "0.0.1"
1071+
"#,
1072+
)
1073+
.file("src/lib.rs", "")
1074+
});
1075+
1076+
let p = p
1077+
.file(
1078+
"Cargo.toml",
1079+
&format!(
1080+
r#"
1081+
[package]
1082+
name = "foo"
1083+
version = "0.0.1"
1084+
1085+
[dependencies.bar]
1086+
version = "0.0.1"
1087+
git = '{}'
1088+
1089+
"#,
1090+
git_project.url()
1091+
),
1092+
)
1093+
.file("src/main.rs", "fn main() {}")
1094+
.build();
1095+
1096+
p.cargo("check")
1097+
.with_stderr(
1098+
"\
1099+
[UPDATING] git repository `[..]`
1100+
[CHECKING] bar v0.0.1 ([..])
1101+
[CHECKING] foo v0.0.1 ([CWD])
1102+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1103+
",
1104+
)
1105+
.run();
1106+
}

0 commit comments

Comments
 (0)