Skip to content

Commit cb1a3f0

Browse files
committed
Detect incorrectly named cargo.toml for build
1 parent a2f9032 commit cb1a3f0

File tree

3 files changed

+74
-52
lines changed

3 files changed

+74
-52
lines changed

src/cargo/util/important_paths.rs

+19-5
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

+18
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(not(target_os = "macos"))]
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

+37-47
Original file line numberDiff line numberDiff line change
@@ -244,53 +244,6 @@ fn missing() {
244244
.run();
245245
}
246246

247-
#[cargo_test]
248-
#[cfg(not(target_os = "macos"))]
249-
fn pkg_missing_cargo_toml() {
250-
let p = project()
251-
.file(
252-
"cargo.toml",
253-
r#"
254-
[package]
255-
name = "foo"
256-
version = "0.1.0"
257-
authors = []
258-
"#,
259-
)
260-
.file("src/main.rs", "fn main() {}")
261-
.build();
262-
263-
cargo_process("install --path .")
264-
.arg(p.root())
265-
.with_status(101)
266-
.with_stderr(
267-
"\
268-
[ERROR] `[CWD]` does not contain a Cargo.toml but found cargo.toml please try to rename it to Cargo.toml. --path must point to a directory containing a Cargo.toml file.
269-
",
270-
)
271-
.run();
272-
}
273-
274-
#[cargo_test]
275-
#[cfg(not(target_os = "macos"))]
276-
fn git_repository_missing_cargo_toml() {
277-
let p = git::repo(&paths::root().join("foo"))
278-
.file("cargo.toml", &basic_manifest("foo", "0.1.0"))
279-
.file("src/main.rs", "fn main() {}")
280-
.build();
281-
282-
cargo_process("install --git")
283-
.arg(p.url().to_string())
284-
.with_status(101)
285-
.with_stderr(
286-
"\
287-
[UPDATING] git repository [..]
288-
[ERROR] Could not find Cargo.toml in `[..]`, but found cargo.toml please try to rename it to Cargo.toml
289-
",
290-
)
291-
.run();
292-
}
293-
294247
#[cargo_test]
295248
fn missing_current_working_directory() {
296249
cargo_process("install .")
@@ -446,6 +399,23 @@ fn install_target_dir() {
446399
assert!(path.exists());
447400
}
448401

402+
#[cargo_test]
403+
#[cfg(not(target_os = "macos"))]
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+
449419
#[cargo_test]
450420
fn multiple_crates_error() {
451421
let p = git::repo(&paths::root().join("foo"))
@@ -807,6 +777,26 @@ fn git_repo() {
807777
assert_has_installed_exe(cargo_home(), "foo");
808778
}
809779

780+
#[cargo_test]
781+
#[cfg(not(target_os = "macos"))]
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+
810800
#[cargo_test]
811801
fn list() {
812802
pkg("foo", "0.0.1");

0 commit comments

Comments
 (0)