{"id":"76f21ec2cde9f799","repo":"rust-lang/cargo","slug":"a-file-should-always-have-a-parent","errorCode":null,"errorMessage":"a file should always have a parent","messagePattern":"a file should always have a parent","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/bin/cargo/commands/run.rs","lineNumber":185,"sourceCode":"                    \"\\nhelp: there is a script with a similar name: `{suggested_script}` (requires `-Zscript`)\"\n                )\n            } else {\n                \"\".to_owned()\n            };\n            return Err(anyhow::anyhow!(\n                \"no such subcommand `{cmd}`{suggested_command}{suggested_script}\"\n            )\n            .into());\n        }\n    }\n\n    let manifest_path = root_manifest(Some(manifest_path), gctx)?;\n\n    // Treat `cargo foo.rs` like `cargo install --path foo` and re-evaluate the config based on the\n    // location where the script resides, rather than the environment from where it's being run.\n    let parent_path = manifest_path\n        .parent()\n        .expect(\"a file should always have a parent\");\n    gctx.reload_rooted_at(parent_path)?;\n\n    let mut ws = Workspace::new(&manifest_path, gctx)?;\n    if gctx.cli_unstable().avoid_dev_deps {\n        ws.set_require_optional_deps(false);\n    }\n\n    let mut compile_opts =\n        cargo::ops::CompileOptions::new(gctx, cargo::compiler::UserIntent::Build)?;\n    compile_opts.spec = cargo::ops::Packages::Default;\n\n    cargo::ops::run(&ws, &compile_opts, args).map_err(|err| to_run_error(gctx, err))\n}\n\nfn suggested_script(cmd: &str) -> Option<String> {\n    let cmd_path = Path::new(cmd);\n    let mut suggestion = Path::new(\".\").to_owned();\n    for cmd_part in cmd_path.components() {","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/bin/cargo/commands/run.rs#L167-L203","documentation":"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.","triggerScenarios":"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`.","commonSituations":"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.","solutions":["Re-run with an explicit, valid `--manifest-path /abs/path/to/Cargo.toml` pointing at a real file.","Check `pwd` and ensure you are not at `/`; `cd` into the crate directory.","Avoid invoking `cargo run` with a bare script path that resolves to the filesystem root."],"exampleFix":"// before\nlet parent_path = manifest_path\n    .parent()\n    .expect(\"a file should always have a parent\");\n// after\nlet parent_path = manifest_path.parent().ok_or_else(|| {\n    anyhow::anyhow!(\"manifest path `{}` has no parent directory\", manifest_path.display())\n})?;","handlingStrategy":"validation","validationCode":"// Before calling `cargo run` with --manifest-path or a script path, confirm\n// the path is a real file with a parent directory:\nuse std::path::Path;\nfn valid_manifest_path(p: &Path) -> bool {\n    p.is_file() && p.parent().map(|d| !d.as_os_str().is_empty()).unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always run `cargo run` from inside the crate directory, or pass an absolute `--manifest-path`.","Avoid invoking `cargo` with a bare path that could resolve to the filesystem root.","Don't set `CARGO_HOME` or `--manifest-path` to `/` or empty."],"tags":["cargo","path-handling","manifest","internal-invariant"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}