Skip to content

Commit

Permalink
Detect incorrectly named cargo.toml for build
Browse files Browse the repository at this point in the history
  • Loading branch information
Rustin170506 committed Jun 22, 2021
1 parent a2f9032 commit cb1a3f0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 52 deletions.
24 changes: 19 additions & 5 deletions src/cargo/util/important_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,33 @@ use std::path::{Path, PathBuf};

/// Finds the root `Cargo.toml`.
pub fn find_root_manifest_for_wd(cwd: &Path) -> CargoResult<PathBuf> {
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.
Expand Down
18 changes: 18 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
84 changes: 37 additions & 47 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 .")
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit cb1a3f0

Please sign in to comment.