From d9daaf7a84c1feb0b0389b443918030b2c5558af Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Mon, 21 Jun 2021 15:24:50 +0800 Subject: [PATCH 1/4] Detect incorrectly named cargo.toml for install --git --- src/cargo/ops/cargo_read_manifest.rs | 15 +++++++-- tests/testsuite/install.rs | 47 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/cargo/ops/cargo_read_manifest.rs b/src/cargo/ops/cargo_read_manifest.rs index 732ef39d613..d55208b88f1 100644 --- a/src/cargo/ops/cargo_read_manifest.rs +++ b/src/cargo/ops/cargo_read_manifest.rs @@ -88,10 +88,19 @@ pub fn read_packages( if all_packages.is_empty() { match errors.pop() { Some(err) => Err(err), - None => Err(anyhow::format_err!( - "Could not find Cargo.toml in `{}`", + None => { + if find_project_manifest_exact(path, "cargo.toml").is_ok() { + Err(anyhow::format_err!( + "Could not find Cargo.toml in `{}`, but found cargo.toml please try to rename it to Cargo.toml", path.display() - )), + )) + } else { + Err(anyhow::format_err!( + "Could not find Cargo.toml in `{}`", + path.display() + )) + } + } } } else { Ok(all_packages.into_iter().map(|(_, v)| v).collect()) diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index c2d3eef6fe4..0830ea85acf 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -244,6 +244,53 @@ fn missing() { .run(); } +#[cargo_test] +fn pkg_missing_cargo_toml() { + let p = project() + .file( + "Cargo1.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + authors = [] + "#, + ) + .file("src/main.rs", "fn main() {}") + .build(); + + cargo_process("install") + .arg(p.root()) + .with_status(101) + .with_stderr( + "\ +[UPDATING] [..] index +[ERROR] could not find `[..]` in registry `[..]` with version `*` +", + ) + .run(); +} + +#[cargo_test] +#[cfg(not(target_os = "macos"))] +fn git_repository_missing_cargo_toml() { + let p = git::repo(&paths::root().join("foo")) + .file("cargo.toml", &basic_manifest("foo", "0.1.0")) + .file("src/main.rs", "fn main() {}") + .build(); + + cargo_process("install --git") + .arg(p.url().to_string()) + .with_status(101) + .with_stderr( + "\ +[UPDATING] git repository [..] +[ERROR] Could not find Cargo.toml in `[..]`, but found cargo.toml please try to rename it to Cargo.toml +", + ) + .run(); +} + #[cargo_test] fn missing_current_working_directory() { cargo_process("install .") From a2f903275a5506b638312a522b63f5757333547b Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 22 Jun 2021 14:12:27 +0800 Subject: [PATCH 2/4] Detect incorrectly named cargo.toml for install --path --- src/cargo/ops/cargo_install.rs | 16 ++++++++++++---- tests/testsuite/install.rs | 8 ++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index 10dd8e091c6..8965b89480b 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -212,11 +212,19 @@ fn install_one( src.path().display() ); } else { - bail!( - "`{}` does not contain a Cargo.toml file. \ + if src.path().join("cargo.toml").exists() { + bail!( + "`{}` does not contain a Cargo.toml file, but found cargo.toml please try to rename it to Cargo.toml. \ --path must point to a directory containing a Cargo.toml file.", - src.path().display() - ) + src.path().display() + ) + } else { + bail!( + "`{}` does not contain a Cargo.toml file. \ + --path must point to a directory containing a Cargo.toml file.", + src.path().display() + ) + } } } select_pkg( diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index 0830ea85acf..6ad36edd916 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -245,10 +245,11 @@ fn missing() { } #[cargo_test] +#[cfg(not(target_os = "macos"))] fn pkg_missing_cargo_toml() { let p = project() .file( - "Cargo1.toml", + "cargo.toml", r#" [package] name = "foo" @@ -259,13 +260,12 @@ fn pkg_missing_cargo_toml() { .file("src/main.rs", "fn main() {}") .build(); - cargo_process("install") + cargo_process("install --path .") .arg(p.root()) .with_status(101) .with_stderr( "\ -[UPDATING] [..] index -[ERROR] could not find `[..]` in registry `[..]` with version `*` +[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. ", ) .run(); From cb1a3f05f09eb794dfe36bccf7ee109d569c271e Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 22 Jun 2021 14:43:19 +0800 Subject: [PATCH 3/4] Detect incorrectly named cargo.toml for build --- src/cargo/util/important_paths.rs | 24 +++++++-- tests/testsuite/build.rs | 18 +++++++ tests/testsuite/install.rs | 84 ++++++++++++++----------------- 3 files changed, 74 insertions(+), 52 deletions(-) diff --git a/src/cargo/util/important_paths.rs b/src/cargo/util/important_paths.rs index 4d34519c2ba..224c4ab8b86 100644 --- a/src/cargo/util/important_paths.rs +++ b/src/cargo/util/important_paths.rs @@ -4,19 +4,33 @@ use std::path::{Path, PathBuf}; /// Finds the root `Cargo.toml`. pub fn find_root_manifest_for_wd(cwd: &Path) -> CargoResult { - let file = "Cargo.toml"; + let valid_cargo_toml_file_name = "Cargo.toml"; + let invalid_cargo_toml_file_name = "cargo.toml"; + let mut invalid_cargo_toml_path_exists = false; + for current in paths::ancestors(cwd, None) { - let manifest = current.join(file); + let manifest = current.join(valid_cargo_toml_file_name); if manifest.exists() { return Ok(manifest); } + if current.join(invalid_cargo_toml_file_name).exists() { + invalid_cargo_toml_path_exists = true; + } } - anyhow::bail!( - "could not find `{}` in `{}` or any parent directory", - file, + if invalid_cargo_toml_path_exists { + anyhow::bail!( + "could not find `{}` in `{}` or any parent directory, but found cargo.toml please try to rename it to Cargo.toml", + valid_cargo_toml_file_name, cwd.display() ) + } else { + anyhow::bail!( + "could not find `{}` in `{}` or any parent directory", + valid_cargo_toml_file_name, + cwd.display() + ) + } } /// Returns the path to the `file` in `pwd`, if it exists. diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index fc11e666df0..7cbc0c3317e 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -561,6 +561,24 @@ fn cargo_compile_without_manifest() { .run(); } +#[cargo_test] +#[cfg(not(target_os = "macos"))] +fn cargo_compile_with_lowercase_cargo_toml() { + let p = project() + .no_manifest() + .file("cargo.toml", &basic_manifest("foo", "0.1.0")) + .file("src/lib.rs", &main_file(r#""i am foo""#, &[])) + .build(); + + p.cargo("build") + .with_status(101) + .with_stderr( + "[ERROR] could not find `Cargo.toml` in `[..]` or any parent directory, \ + but found cargo.toml please try to rename it to Cargo.toml", + ) + .run(); +} + #[cargo_test] fn cargo_compile_with_invalid_code() { let p = project() diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index 6ad36edd916..6c0f2cbcd0a 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -244,53 +244,6 @@ fn missing() { .run(); } -#[cargo_test] -#[cfg(not(target_os = "macos"))] -fn pkg_missing_cargo_toml() { - let p = project() - .file( - "cargo.toml", - r#" - [package] - name = "foo" - version = "0.1.0" - authors = [] - "#, - ) - .file("src/main.rs", "fn main() {}") - .build(); - - cargo_process("install --path .") - .arg(p.root()) - .with_status(101) - .with_stderr( - "\ -[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. -", - ) - .run(); -} - -#[cargo_test] -#[cfg(not(target_os = "macos"))] -fn git_repository_missing_cargo_toml() { - let p = git::repo(&paths::root().join("foo")) - .file("cargo.toml", &basic_manifest("foo", "0.1.0")) - .file("src/main.rs", "fn main() {}") - .build(); - - cargo_process("install --git") - .arg(p.url().to_string()) - .with_status(101) - .with_stderr( - "\ -[UPDATING] git repository [..] -[ERROR] Could not find Cargo.toml in `[..]`, but found cargo.toml please try to rename it to Cargo.toml -", - ) - .run(); -} - #[cargo_test] fn missing_current_working_directory() { cargo_process("install .") @@ -446,6 +399,23 @@ fn install_target_dir() { assert!(path.exists()); } +#[cargo_test] +#[cfg(not(target_os = "macos"))] +fn install_path_with_lowercase_cargo_toml() { + let toml = paths::root().join("cargo.toml"); + fs::write(toml, "").unwrap(); + + cargo_process("install --path .") + .with_status(101) + .with_stderr( + "\ +[ERROR] `[CWD]` does not contain a Cargo.toml file, \ +but found cargo.toml please try to rename it to Cargo.toml. --path must point to a directory containing a Cargo.toml file. +", + ) + .run(); +} + #[cargo_test] fn multiple_crates_error() { let p = git::repo(&paths::root().join("foo")) @@ -807,6 +777,26 @@ fn git_repo() { assert_has_installed_exe(cargo_home(), "foo"); } +#[cargo_test] +#[cfg(not(target_os = "macos"))] +fn git_repo_with_lowercase_cargo_toml() { + let p = git::repo(&paths::root().join("foo")) + .file("cargo.toml", &basic_manifest("foo", "0.1.0")) + .file("src/main.rs", "fn main() {}") + .build(); + + cargo_process("install --git") + .arg(p.url().to_string()) + .with_status(101) + .with_stderr( + "\ +[UPDATING] git repository [..] +[ERROR] Could not find Cargo.toml in `[..]`, but found cargo.toml please try to rename it to Cargo.toml +", + ) + .run(); +} + #[cargo_test] fn list() { pkg("foo", "0.0.1"); From b3a1d0ceba1358263e26310346be8f87f2a68e87 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 22 Jun 2021 15:36:32 +0800 Subject: [PATCH 4/4] Only testing on linux --- tests/testsuite/build.rs | 2 +- tests/testsuite/install.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 7cbc0c3317e..0948ac1dad7 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -562,7 +562,7 @@ fn cargo_compile_without_manifest() { } #[cargo_test] -#[cfg(not(target_os = "macos"))] +#[cfg(target_os = "linux")] fn cargo_compile_with_lowercase_cargo_toml() { let p = project() .no_manifest() diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index 6c0f2cbcd0a..dfb5f96f19a 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -400,7 +400,7 @@ fn install_target_dir() { } #[cargo_test] -#[cfg(not(target_os = "macos"))] +#[cfg(target_os = "linux")] fn install_path_with_lowercase_cargo_toml() { let toml = paths::root().join("cargo.toml"); fs::write(toml, "").unwrap(); @@ -778,7 +778,7 @@ fn git_repo() { } #[cargo_test] -#[cfg(not(target_os = "macos"))] +#[cfg(target_os = "linux")] fn git_repo_with_lowercase_cargo_toml() { let p = git::repo(&paths::root().join("foo")) .file("cargo.toml", &basic_manifest("foo", "0.1.0"))