rust-lang/cargo · 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 found neither Cargo.toml nor cargo.toml in cwd or any ancestor. Cargo is therefore not running inside any package/workspace root.

Solutions

  1. cd into the project directory containing Cargo.toml.
  2. Pass --manifest-path to point at an existing Cargo.toml.
  3. Create a new project with `cargo new` / `cargo init` if none exists.

Example fix

# before
# cd /tmp && 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_in_cargo_project(cwd: &Path) -> Result<PathBuf, String> {
    for dir in paths::ancestors(cwd, None) {
        let m = dir.join("Cargo.toml");
        if m.is_file() { return Ok(m); }
    }
    Err(format!("no Cargo.toml in {cwd:?} or any parent; cd into the project or run `cargo new`"))
}

Type guard

fn is_inside_cargo_project(cwd: &std::path::Path) -> bool {
    paths::ancestors(cwd, None).any(|d| d.join("Cargo.toml").is_file())
}

Prevention

When it happens

Trigger: Invoking a package-scoped cargo subcommand (build, run, test, etc.) from a directory that is not inside a Cargo project.

Common situations: Wrong shell cwd; running cargo in a freshly cloned empty repo before adding a manifest; running from /tmp or home dir.

Related errors


AI-assisted analysis of rust-lang/cargo@eb98b54bc9 (2026-08-11). Data as JSON: /api/errors/b0c16c2ba9105c8c. Report an issue: GitHub.

Appendix: 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 eb98b54bc9)