{"id":"891293624a9b68a6","repo":"rust-lang/cargo","slug":"able-to-invoke-rustc","errorCode":null,"errorMessage":"able to invoke rustc","messagePattern":"able to invoke rustc","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/compiler/standard_lib.rs","lineNumber":230,"sourceCode":"                None,\n                false,\n            ));\n        }\n    }\n    Ok(())\n}\n\nfn detect_sysroot_src_path(ws: &Workspace<'_>) -> CargoResult<PathBuf> {\n    if let Some(s) = ws.gctx().get_env_os(\"__CARGO_TESTS_ONLY_SRC_ROOT\") {\n        return Ok(s.into());\n    }\n\n    // NOTE: This is temporary until we figure out how to acquire the source.\n    let rustc = ws.gctx().load_global_rustc(Some(ws))?;\n    let src_path = ws\n        .gctx()\n        .get_sysroot(&rustc)\n        .expect(\"able to invoke rustc\")\n        .join(\"lib\")\n        .join(\"rustlib\")\n        .join(\"src\")\n        .join(\"rust\")\n        .join(\"library\");\n    let lock = src_path.join(\"Cargo.lock\");\n    if !lock.exists() {\n        let msg = format!(\n            \"{:?} does not exist, unable to build with the standard \\\n             library, try:\\n        rustup component add rust-src\",\n            lock\n        );\n        match ws.gctx().get_env(\"RUSTUP_TOOLCHAIN\") {\n            Ok(rustup_toolchain) => {\n                anyhow::bail!(\"{} --toolchain {}\", msg, rustup_toolchain);\n            }\n            Err(_) => {\n                anyhow::bail!(msg);","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/compiler/standard_lib.rs#L212-L248","documentation":"`detect_sysroot_src_path` computes the std source location and calls `ws.gctx().get_sysroot(&rustc).expect(\"able to invoke rustc\")`. `rustc` was loaded one line above (`load_global_rustc`) and `get_sysroot` derives the sysroot by invoking that rustc. The expect treats rustc invocation as infallible because it just succeeded moments earlier; failure means rustc became un-invokable between the two calls.","triggerScenarios":"`rustc` binary deleted/moved after `load_global_rustc` (e.g. `rustup toolchain uninstall` mid-build); `RUSTC` env var changed; permissions on the rustc executable revoked mid-build; a broken `RUSTC_WRAPPER` that worked once and then failed.","commonSituations":"Concurrent toolchain uninstall/install; editor/IDE swapping `RUSTC_WRAPPER`; long-lived Cargo-as-library process outliving the toolchain it was started with; sandbox/CI that mutates PATH mid-run.","solutions":["Pin the toolchain (`rust-toolchain.toml`) and keep it installed for the whole build.","Avoid uninstalling/swapping the active toolchain while Cargo runs.","Re-run after the toolchain is stable; `cargo clean` if you switched versions."],"exampleFix":"// before\nlet src_path = ws\n    .gctx()\n    .get_sysroot(&rustc)\n    .expect(\"able to invoke rustc\")\n    .join(\"lib\")\n// after\nlet src_path = ws\n    .gctx()\n    .get_sysroot(&rustc)\n    .with_context(|| \"rustc became un-invokable while locating the sysroot; was the toolchain changed mid-build?\")?\n    .join(\"lib\")","handlingStrategy":"validation","validationCode":"// Confirm rustc is invocable and the sysroot is stable before relying on -Zbuild-std\nuse std::process::Command;\nfn rustc_sysroot_ok() -> Option<std::path::PathBuf> {\n    let out = Command::new(\"rustc\").args([\"--print\", \"sysroot\"]).output().ok()?;\n    if !out.status.success() { return None; }\n    let p = String::from_utf8_lossy(&out.stdout).trim().to_string();\n    Some(std::path::PathBuf::from(p))\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pin the toolchain with `rust-toolchain.toml` and keep it installed for the whole build.","Don't uninstall/swap the active toolchain mid-build.","Avoid changing `RUSTC`/`RUSTC_WRAPPER` during a build."],"tags":["cargo","build-std","sysroot","rustc","toolchain","internal-invariant"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}