Skip to content

Commit 9233aa0

Browse files
committed
Auto merge of #9607 - hi-rustin:rustin-patch-cargo-toml, r=ehuss
Detect incorrectly named cargo.toml close #9541
2 parents a2589dd + b3a1d0c commit 9233aa0

File tree

5 files changed

+98
-12
lines changed

5 files changed

+98
-12
lines changed

src/cargo/ops/cargo_install.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,19 @@ fn install_one(
212212
src.path().display()
213213
);
214214
} else {
215-
bail!(
216-
"`{}` does not contain a Cargo.toml file. \
215+
if src.path().join("cargo.toml").exists() {
216+
bail!(
217+
"`{}` does not contain a Cargo.toml file, but found cargo.toml please try to rename it to Cargo.toml. \
217218
--path must point to a directory containing a Cargo.toml file.",
218-
src.path().display()
219-
)
219+
src.path().display()
220+
)
221+
} else {
222+
bail!(
223+
"`{}` does not contain a Cargo.toml file. \
224+
--path must point to a directory containing a Cargo.toml file.",
225+
src.path().display()
226+
)
227+
}
220228
}
221229
}
222230
select_pkg(

src/cargo/ops/cargo_read_manifest.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,19 @@ pub fn read_packages(
8888
if all_packages.is_empty() {
8989
match errors.pop() {
9090
Some(err) => Err(err),
91-
None => Err(anyhow::format_err!(
92-
"Could not find Cargo.toml in `{}`",
91+
None => {
92+
if find_project_manifest_exact(path, "cargo.toml").is_ok() {
93+
Err(anyhow::format_err!(
94+
"Could not find Cargo.toml in `{}`, but found cargo.toml please try to rename it to Cargo.toml",
9395
path.display()
94-
)),
96+
))
97+
} else {
98+
Err(anyhow::format_err!(
99+
"Could not find Cargo.toml in `{}`",
100+
path.display()
101+
))
102+
}
103+
}
95104
}
96105
} else {
97106
Ok(all_packages.into_iter().map(|(_, v)| v).collect())

src/cargo/util/important_paths.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,33 @@ use std::path::{Path, PathBuf};
44

55
/// Finds the root `Cargo.toml`.
66
pub fn find_root_manifest_for_wd(cwd: &Path) -> CargoResult<PathBuf> {
7-
let file = "Cargo.toml";
7+
let valid_cargo_toml_file_name = "Cargo.toml";
8+
let invalid_cargo_toml_file_name = "cargo.toml";
9+
let mut invalid_cargo_toml_path_exists = false;
10+
811
for current in paths::ancestors(cwd, None) {
9-
let manifest = current.join(file);
12+
let manifest = current.join(valid_cargo_toml_file_name);
1013
if manifest.exists() {
1114
return Ok(manifest);
1215
}
16+
if current.join(invalid_cargo_toml_file_name).exists() {
17+
invalid_cargo_toml_path_exists = true;
18+
}
1319
}
1420

15-
anyhow::bail!(
16-
"could not find `{}` in `{}` or any parent directory",
17-
file,
21+
if invalid_cargo_toml_path_exists {
22+
anyhow::bail!(
23+
"could not find `{}` in `{}` or any parent directory, but found cargo.toml please try to rename it to Cargo.toml",
24+
valid_cargo_toml_file_name,
1825
cwd.display()
1926
)
27+
} else {
28+
anyhow::bail!(
29+
"could not find `{}` in `{}` or any parent directory",
30+
valid_cargo_toml_file_name,
31+
cwd.display()
32+
)
33+
}
2034
}
2135

2236
/// Returns the path to the `file` in `pwd`, if it exists.

tests/testsuite/build.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,24 @@ fn cargo_compile_without_manifest() {
561561
.run();
562562
}
563563

564+
#[cargo_test]
565+
#[cfg(target_os = "linux")]
566+
fn cargo_compile_with_lowercase_cargo_toml() {
567+
let p = project()
568+
.no_manifest()
569+
.file("cargo.toml", &basic_manifest("foo", "0.1.0"))
570+
.file("src/lib.rs", &main_file(r#""i am foo""#, &[]))
571+
.build();
572+
573+
p.cargo("build")
574+
.with_status(101)
575+
.with_stderr(
576+
"[ERROR] could not find `Cargo.toml` in `[..]` or any parent directory, \
577+
but found cargo.toml please try to rename it to Cargo.toml",
578+
)
579+
.run();
580+
}
581+
564582
#[cargo_test]
565583
fn cargo_compile_with_invalid_code() {
566584
let p = project()

tests/testsuite/install.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,23 @@ fn install_target_dir() {
399399
assert!(path.exists());
400400
}
401401

402+
#[cargo_test]
403+
#[cfg(target_os = "linux")]
404+
fn install_path_with_lowercase_cargo_toml() {
405+
let toml = paths::root().join("cargo.toml");
406+
fs::write(toml, "").unwrap();
407+
408+
cargo_process("install --path .")
409+
.with_status(101)
410+
.with_stderr(
411+
"\
412+
[ERROR] `[CWD]` does not contain a Cargo.toml file, \
413+
but found cargo.toml please try to rename it to Cargo.toml. --path must point to a directory containing a Cargo.toml file.
414+
",
415+
)
416+
.run();
417+
}
418+
402419
#[cargo_test]
403420
fn multiple_crates_error() {
404421
let p = git::repo(&paths::root().join("foo"))
@@ -760,6 +777,26 @@ fn git_repo() {
760777
assert_has_installed_exe(cargo_home(), "foo");
761778
}
762779

780+
#[cargo_test]
781+
#[cfg(target_os = "linux")]
782+
fn git_repo_with_lowercase_cargo_toml() {
783+
let p = git::repo(&paths::root().join("foo"))
784+
.file("cargo.toml", &basic_manifest("foo", "0.1.0"))
785+
.file("src/main.rs", "fn main() {}")
786+
.build();
787+
788+
cargo_process("install --git")
789+
.arg(p.url().to_string())
790+
.with_status(101)
791+
.with_stderr(
792+
"\
793+
[UPDATING] git repository [..]
794+
[ERROR] Could not find Cargo.toml in `[..]`, but found cargo.toml please try to rename it to Cargo.toml
795+
",
796+
)
797+
.run();
798+
}
799+
763800
#[cargo_test]
764801
fn list() {
765802
pkg("foo", "0.0.1");

0 commit comments

Comments
 (0)