rust-lang/cargo · error · anyhow::Error
could not find `{}` in `{}` or any parent directory
Error message
could not find `{}` in `{}` or any parent directory What it means
find_root_manifest_for_wd walked cwd and every ancestor directory and found no `Cargo.toml` anywhere up the tree (and no lowercase `cargo.toml` either). Cargo cannot operate outside of a package/workspace root, so it bails.
Source
Thrown at src/util/important_paths.rs:28
for current in paths::ancestors(cwd, None) {
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;
}
}
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.
pub fn find_project_manifest_exact(pwd: &Path, file: &str) -> CargoResult<PathBuf> {
let manifest = pwd.join(file);
if manifest.exists() {
Ok(manifest)
} else {
anyhow::bail!("Could not find `{}` in `{}`", file, pwd.display())
}
}
View on GitHub (pinned to 0e07a15537)
Solutions
- `cd` into the project directory containing (or above) a Cargo.toml.
- If starting fresh, run `cargo init` or `cargo new <name>` to create a project.
- In CI, verify the checkout/clone succeeded and that the cwd is the repo root.
- Pass `--manifest-path /abs/path/Cargo.toml` if you must run from elsewhere.
Example fix
# before (run from ~) cargo build # after cd ~/projects/myapp && cargo build # or cargo build --manifest-path ~/projects/myapp/Cargo.toml
Defensive patterns
Strategy: validation
Validate before calling
use std::path::Path;
fn ensure_inside_cargo_project(cwd: &Path) -> Result<(), anyhow::Error> {
let found = cargo_util::paths::ancestors(cwd, None)
.any(|d| d.join("Cargo.toml").exists());
if !found { anyhow::bail!("no Cargo.toml in {cwd:?} or ancestors; run cargo init/new or cd into the project"); }
Ok(())
} Type guard
fn is_inside_cargo_project(cwd: &std::path::Path) -> bool {
cargo_util::paths::ancestors(cwd, None).any(|d| d.join("Cargo.toml").exists())
} Try / catch
match find_root_manifest_for_wd(cwd) {
Err(e) if e.to_string().contains("or any parent directory") => {
eprintln!("not inside a Cargo project; cd into one or run `cargo init`");
return Err(e);
}
r => r,
} Prevention
- Confirm `pwd` before running cargo in CI/scripts.
- Run `cargo init`/`cargo new` to create a project before any package command.
- Pass `--manifest-path` with an absolute path when cwd is uncertain.
When it happens
Trigger: Running any package-scoped cargo command from a directory that is not inside a Cargo project — e.g. `cargo build` run from `~`, `/tmp`, or a freshly created empty folder.
Common situations: Wrong working directory in a terminal/IDE; CI clone step failed so the repo is empty; running cargo before `cargo init`/`cargo new`; nested workspace where the path passed is outside the workspace root.
Related errors
- Could not find `{}` in `{}`
- manifest path `{}` does not exist
- manifest path `{}` is a directory but expected a file{sugges
- could not find `{}` in `{}` or any parent directory, but fou
- package `{}` cannot be tested because it requires dev-depend
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/b0c16c2ba9105c8c.json.
Report an issue: GitHub.