cargo/util/
important_paths.rs

1use crate::util::errors::CargoResult;
2use cargo_util::paths;
3use std::path::{Path, PathBuf};
4
5/// Finds the root `Cargo.toml`.
6pub fn find_root_manifest_for_wd(cwd: &Path) -> CargoResult<PathBuf> {
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
11    for current in paths::ancestors(cwd, None) {
12        let manifest = current.join(valid_cargo_toml_file_name);
13        if manifest.exists() {
14            return Ok(manifest);
15        }
16        if current.join(invalid_cargo_toml_file_name).exists() {
17            invalid_cargo_toml_path_exists = true;
18        }
19    }
20
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,
25        cwd.display()
26    )
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    }
34}
35
36/// Returns the path to the `file` in `pwd`, if it exists.
37pub fn find_project_manifest_exact(pwd: &Path, file: &str) -> CargoResult<PathBuf> {
38    let manifest = pwd.join(file);
39
40    if manifest.exists() {
41        Ok(manifest)
42    } else {
43        anyhow::bail!("Could not find `{}` in `{}`", file, pwd.display())
44    }
45}