rust-lang/cargo · error

a file should always have a parent

Error message

a file should always have a parent

What it means

In `cargo run`'s script/manifest handling, `manifest_path.parent().expect("a file should always have a parent")` extracts the directory containing the manifest. Because `manifest_path` is always a file path returned by `root_manifest`, it must have a parent directory. The expect fires only if the path degenerates to the filesystem root (`/`) or is empty — states Cargo does not believe a manifest path can reach.

Source

Thrown at src/bin/cargo/commands/run.rs:185

                    "\nhelp: there is a script with a similar name: `{suggested_script}` (requires `-Zscript`)"
                )
            } else {
                "".to_owned()
            };
            return Err(anyhow::anyhow!(
                "no such subcommand `{cmd}`{suggested_command}{suggested_script}"
            )
            .into());
        }
    }

    let manifest_path = root_manifest(Some(manifest_path), gctx)?;

    // Treat `cargo foo.rs` like `cargo install --path foo` and re-evaluate the config based on the
    // location where the script resides, rather than the environment from where it's being run.
    let parent_path = manifest_path
        .parent()
        .expect("a file should always have a parent");
    gctx.reload_rooted_at(parent_path)?;

    let mut ws = Workspace::new(&manifest_path, gctx)?;
    if gctx.cli_unstable().avoid_dev_deps {
        ws.set_require_optional_deps(false);
    }

    let mut compile_opts =
        cargo::ops::CompileOptions::new(gctx, cargo::compiler::UserIntent::Build)?;
    compile_opts.spec = cargo::ops::Packages::Default;

    cargo::ops::run(&ws, &compile_opts, args).map_err(|err| to_run_error(gctx, err))
}

fn suggested_script(cmd: &str) -> Option<String> {
    let cmd_path = Path::new(cmd);
    let mut suggestion = Path::new(".").to_owned();
    for cmd_part in cmd_path.components() {

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Re-run with an explicit, valid `--manifest-path /abs/path/to/Cargo.toml` pointing at a real file.
  2. Check `pwd` and ensure you are not at `/`; `cd` into the crate directory.
  3. Avoid invoking `cargo run` with a bare script path that resolves to the filesystem root.

Example fix

// before
let parent_path = manifest_path
    .parent()
    .expect("a file should always have a parent");
// after
let parent_path = manifest_path.parent().ok_or_else(|| {
    anyhow::anyhow!("manifest path `{}` has no parent directory", manifest_path.display())
})?;
Defensive patterns

Strategy: validation

Validate before calling

// Before calling `cargo run` with --manifest-path or a script path, confirm
// the path is a real file with a parent directory:
use std::path::Path;
fn valid_manifest_path(p: &Path) -> bool {
    p.is_file() && p.parent().map(|d| !d.as_os_str().is_empty()).unwrap_or(false)
}

Prevention

When it happens

Trigger: Passing a manifest/script path whose normalized form is the filesystem root (e.g. `/`), or an empty string that `root_manifest` somehow accepts; symlink or canonicalization edge cases that collapse a path to `/`; running `cargo run` against a path produced by a broken custom `CARGO_HOME` or `--manifest-path`.

Common situations: Scripts invoked via `cargo ./something` where `something` resolves to root; corrupted `$PWD`; a wrapper tool that passes `--manifest-path /`; unusual container/chroot layouts where the working directory is the root.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/76f21ec2cde9f799.json. Report an issue: GitHub.