{"id":"0c3bd43dcc4b16b9","repo":"rust-lang/rust","slug":"failed-to-spawn-cargo","errorCode":null,"errorMessage":"Failed to spawn cargo: {}","messagePattern":"Failed to spawn cargo: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_codegen_cranelift/scripts/cargo-clif.rs","lineNumber":84,"sourceCode":"    rustflags_to_cmd_env(\n        &mut cmd,\n        \"RUSTFLAGS\",\n        &rustflags_from_env(\"RUSTFLAGS\")\n            .into_iter()\n            .chain(rustflags.iter().map(|flag| flag.clone()))\n            .collect::<Vec<_>>(),\n    );\n    rustflags_to_cmd_env(\n        &mut cmd,\n        \"RUSTDOCFLAGS\",\n        &rustflags_from_env(\"RUSTDOCFLAGS\")\n            .into_iter()\n            .chain(rustflags.iter().map(|flag| flag.clone()))\n            .collect::<Vec<_>>(),\n    );\n\n    #[cfg(unix)]\n    panic!(\"Failed to spawn cargo: {}\", cmd.exec());\n\n    #[cfg(not(unix))]\n    std::process::exit(cmd.spawn().unwrap().wait().unwrap().code().unwrap_or(1));\n}\n","sourceCodeStart":66,"sourceCodeEnd":89,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_codegen_cranelift/scripts/cargo-clif.rs#L66-L89","documentation":"Panics in the cargo-clif launcher shim when Command::exec() (unix exec replacement) fails to replace the process with cargo. cargo-clif assembles RUSTFLAGS/RUSTDOCFLAGS pointing rustc at the cranelift codegen backend and then replaces itself with cargo; exec failure means the toolchain cargo binary could not be started.","triggerScenarios":"Reached only on unix at the end of cargo-clif.rs after building the cargo Command and setting RUSTFLAGS/RUSTDOCFLAGS. CommandExt::exec returns the io::Error only when the spawn/exec syscall itself fails (the cargo binary could not be executed); it does NOT report cargo's exit code.","commonSituations":"cargo binary not on PATH or the CARGO env var points at a nonexistent/unexecutable path; rustup toolchain named by TOOLCHAIN_NAME is not installed (`rustup toolchain list`); the resolved cargo is not executable (chmod, broken rustup install, truncated download); PATH corrupted inside a wrapper script; attempting to run cargo-clif outside the rustbuild/rustup environment it was built for.","solutions":["Run `rustup toolchain list` and confirm the toolchain named at build time is installed; install it if missing.","Run `which cargo` / `$CARGO --version` in the same shell to confirm the binary exists and is executable.","Re-run `./y.rs prepare` so the toolchain is downloaded and the scripts know TOOLCHAIN_NAME.","If invoking cargo-clif manually, source the env setup emitted by the build instead of relying on ambient PATH."],"exampleFix":"// before\n#[cfg(unix)]\npanic!(\"Failed to spawn cargo: {}\", cmd.exec());\n// after\n#[cfg(unix)]\n{\n    let cargo = env::var_os(\"CARGO\").unwrap_or_else(|| \"cargo\".into());\n    eprintln!(\"cargo-clif: exec {:?} via {:?}\", cargo, std::env::var(\"RUSTUP_TOOLCHAIN\").ok());\n    panic!(\"Failed to spawn cargo: {}\", cmd.exec());\n}","handlingStrategy":"validation","validationCode":"fn ensure_tool(name: &str) -> Result<(), String> {\n    if std::process::Command::new(name).arg(\"--version\").output().is_err() {\n        if let Err(_) = which::which(name) {\n            return Err(format!(\"{} not found on PATH; install the rust toolchain / set RUSTUP_HOME\", name));\n        }\n    }\n    Ok(())\n}\n// call ensure_tool(\"cargo\") before invoking cargo-clif","typeGuard":"fn cargo_available() -> bool {\n    std::process::Command::new(\"cargo\").arg(\"--version\").status().is_ok()\n}","tryCatchPattern":"use std::panic;\nlet r = panic::catch_unwind(|| Command::new(\"cargo\").args(args).status());\nif r.is_err() { eprintln!(\"cargo could not be spawned; check PATH/RUSTUP_HOME\"); std::process::exit(1); }","preventionTips":["Confirm `cargo --version` runs in the same shell/env you invoke cargo-clif from.","Source rustup's env (or run through `rustup run`) so PATH points at the active toolchain.","Do not run the build under environments that strip PATH (minimal containers, systemd units without DefaultEnvironment).","On Windows, ensure the cargo.exe directory is not blocked by antivirus or missing from PATHEXT."],"tags":["cg-clif","launcher","process-spawn","unix","rustup"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}