{"id":"f5c6af20b69d89a6","repo":"rust-lang/cargo","slug":"cargo-not-set","errorCode":null,"errorMessage":"$CARGO not set","messagePattern":"\\$CARGO not set","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/context/mod.rs","lineNumber":564,"sourceCode":"                None\n            },\n            self,\n        )\n    }\n\n    /// Gets the path to the `cargo` executable.\n    pub fn cargo_exe(&self) -> CargoResult<&Path> {\n        self.cargo_exe\n            .try_borrow_with(|| {\n                let from_env = || -> CargoResult<PathBuf> {\n                    // Try re-using the `cargo` set in the environment already. This allows\n                    // commands that use Cargo as a library to inherit (via `cargo <subcommand>`)\n                    // or set (by setting `$CARGO`) a correct path to `cargo` when the current exe\n                    // is not actually cargo (e.g., `cargo-*` binaries, Valgrind, `ld.so`, etc.).\n                    let exe = self\n                        .get_env_os(crate::CARGO_ENV)\n                        .map(PathBuf::from)\n                        .ok_or_else(|| anyhow!(\"$CARGO not set\"))?;\n                    Ok(exe)\n                };\n\n                fn from_current_exe() -> CargoResult<PathBuf> {\n                    // Try fetching the path to `cargo` using `env::current_exe()`.\n                    // The method varies per operating system and might fail; in particular,\n                    // it depends on `/proc` being mounted on Linux, and some environments\n                    // (like containers or chroots) may not have that available.\n                    let exe = env::current_exe()?;\n                    Ok(exe)\n                }\n\n                fn from_argv() -> CargoResult<PathBuf> {\n                    // Grab `argv[0]` and attempt to resolve it to an absolute path.\n                    // If `argv[0]` has one component, it must have come from a `PATH` lookup,\n                    // so probe `PATH` in that case.\n                    // Otherwise, it has multiple components and is either:\n                    // - a relative path (e.g., `./cargo`, `target/debug/cargo`), or","sourceCodeStart":546,"sourceCodeEnd":582,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/context/mod.rs#L546-L582","documentation":"cargo_exe (mod.rs:553-604) resolves the path to the running `cargo` binary. It tries current_exe() and argv[0] first; if neither yields a path whose file_stem is 'cargo', it falls back to the $CARGO environment variable. If $CARGO is also unset, it bails '$CARGO not set'. This fallback exists for cargo-as-library consumers, valgrind, ld.so, or cargo-* subcommand wrappers where current_exe/argv0 are not 'cargo'.","triggerScenarios":"Using cargo as a library from a binary that is not named cargo, under a launcher (valgrind/ld.so/wrapper) that obscures argv[0] and current_exe, without setting $CARGO. Also when cargo needs to re-invoke itself (e.g. `cargo invoke`) but cannot locate its own binary.","commonSituations":"A custom cargo-* subcommand that links against the cargo crate; running cargo under valgrind or a dynamic linker; stripped/oddly-named binaries.","solutions":["Set `CARGO` to the absolute path of the cargo binary before invoking the process.","Name the binary 'cargo' or invoke it through a normal PATH lookup so argv[0]/current_exe resolve correctly.","Ensure /proc is mounted on Linux (current_exe depends on it)."],"exampleFix":"# before\nvalgrind my-cargo-wrapper build   # current_exe/argv0 obscured\n# after\nexport CARGO=$(command -v cargo)\nvalgrind my-cargo-wrapper build","handlingStrategy":"validation","validationCode":"# For cargo-as-library or wrapper binaries, set CARGO explicitly:\nexport CARGO=\"${CARGO:-$(command -v cargo)}\"\nexec \"$CARGO\" \"$@\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set CARGO when invoking cargo through a launcher (valgrind, ld.so, wrapper).","Name cargo-wrapper binaries distinctly and set CARGO rather than relying on argv[0].","Ensure /proc is mounted on Linux so current_exe() works."],"tags":["environment","cargo-exe","self-invoke","subcommand"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}