rust-lang/cargo · error · anyhow::Error

no argv[0]

Error message

no argv[0]

What it means

Thrown by the from_argv() helper inside cargo_exe() when env::args_os() yields no elements at all — the process was started without even an argv[0]. This is the third and last-resort fallback in Cargo's binary-detection chain, reached only when both $CARGO and env::current_exe() did not yield a valid cargo path.

Solutions

  1. Ensure the process is launched with at least argv[0] set to the executable path or name
  2. Set the CARGO environment variable so the from_env fallback succeeds before from_argv is reached
  3. Ensure /proc is mounted so env::current_exe() resolves before from_argv is reached
Defensive patterns

Strategy: validation

Validate before calling

// Ensure argv[0] exists before relying on cargo_exe()
fn ensure_argv0() -> Result<(), anyhow::Error> {
    if std::env::args_os().next().is_none() {
        anyhow::bail!("process started without argv[0]; cannot locate cargo");
    }
    Ok(())
}

Prevention

When it happens

Trigger: Process launched with a completely empty argument vector (no argv[0]), which is theoretically possible via direct execve with a zero-length argv array but extremely rare in practice.

Common situations: Custom process spawners that call execve with an empty argv. Debugging tools or sandboxing layers that strip argv. Essentially never encountered in normal CLI usage.

Related errors


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

Appendix: source

Thrown at src/context/mod.rs:583

                    // Try fetching the path to `cargo` using `env::current_exe()`.
                    // The method varies per operating system and might fail; in particular,
                    // it depends on `/proc` being mounted on Linux, and some environments
                    // (like containers or chroots) may not have that available.
                    let exe = env::current_exe()?;
                    Ok(exe)
                }

                fn from_argv() -> CargoResult<PathBuf> {
                    // Grab `argv[0]` and attempt to resolve it to an absolute path.
                    // If `argv[0]` has one component, it must have come from a `PATH` lookup,
                    // so probe `PATH` in that case.
                    // Otherwise, it has multiple components and is either:
                    // - a relative path (e.g., `./cargo`, `target/debug/cargo`), or
                    // - an absolute path (e.g., `/usr/local/bin/cargo`).
                    let argv0 = env::args_os()
                        .map(PathBuf::from)
                        .next()
                        .ok_or_else(|| anyhow!("no argv[0]"))?;
                    paths::resolve_executable(&argv0)
                }

                // Determines whether `path` is a cargo binary.
                // See: https://github.com/rust-lang/cargo/issues/15099#issuecomment-2666737150
                fn is_cargo(path: &Path) -> bool {
                    path.file_stem() == Some(OsStr::new("cargo"))
                }

                let from_current_exe = from_current_exe();
                if from_current_exe.as_deref().is_ok_and(is_cargo) {
                    return from_current_exe;
                }

                let from_argv = from_argv();
                if from_argv.as_deref().is_ok_and(is_cargo) {
                    return from_argv;
                }

View on GitHub (pinned to 98a09e7e7d)