{"record":{"id":"0b7faf657ccf247b","repo":"rust-lang/cargo","slug":"could-not-find-rustup-home-dir","errorCode":null,"errorMessage":"could not find rustup home dir","messagePattern":"could not find rustup home dir","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"crates/home/src/env.rs","lineNumber":112,"sourceCode":"/// Variant of `cargo_home_with_cwd` where the environment source is\n/// parameterized.\n///\n/// This is specifically to support in-process testing scenarios\n/// as environment variables and user home metadata are normally process global\n/// state. See the `OsEnv` trait.\npub fn rustup_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> {\n    match env.var_os(\"RUSTUP_HOME\").filter(|h| !h.is_empty()) {\n        Some(home) => {\n            let home = PathBuf::from(home);\n            if home.is_absolute() {\n                Ok(home)\n            } else {\n                Ok(cwd.join(&home))\n            }\n        }\n        _ => home_dir_with_env(env)\n            .map(|d| d.join(\".rustup\"))\n            .ok_or_else(|| io::Error::new(io::ErrorKind::Other, \"could not find rustup home dir\")),\n    }\n}\n","sourceCodeStart":94,"sourceCodeEnd":115,"githubUrl":"https://github.com/rust-lang/cargo/blob/eb98b54bc9f3c74519f43d066cb3fd02ebc88df0/crates/home/src/env.rs#L94-L115","documentation":"`rustup_home_with_cwd_env` mirrors the cargo-home logic for rustup. If `RUSTUP_HOME` is set and non-empty it is used (relative to cwd if not absolute); otherwise it falls back to `home_dir_with_env(env).join(\".rustup\")`. If no home directory can be determined, it returns an `io::Error` (kind `Other`, message `\"could not find rustup home dir\"`). This blocks rustup-managed toolchain resolution.","triggerScenarios":"Invoking code that resolves the rustup home in an environment where `RUSTUP_HOME` is unset/empty and `home_dir_with_env` returns `None` (no `HOME`/`USERPROFILE`). The `_ => home_dir_with_env(env)...ok_or_else(...)` arm fires.","commonSituations":"CI containers without `HOME`, hardened services, running under accounts lacking a passwd/home entry, or test environments that clear `RUSTUP_HOME` and `HOME` simultaneously.","solutions":["Set `RUSTUP_HOME` to an absolute, writable directory (e.g. `export RUSTUP_HOME=/opt/rust/.rustup`).","Ensure `HOME` (Unix) or `USERPROFILE` (Windows) is set so the `.rustup` fallback resolves.","For containerized/service contexts, add `RUSTUP_HOME` and `HOME` to the environment definition."],"exampleFix":"# before (minimal container, no HOME)\nrustup show  # -> could not find rustup home dir\n\n# after\nexport RUSTUP_HOME=/usr/local/rustup\nexport HOME=/root\nrustup show","handlingStrategy":"fallback","validationCode":"use std::path::PathBuf;\nfn resolve_rustup_home() -> Result<PathBuf, String> {\n    if let Ok(h) = std::env::var(\"RUSTUP_HOME\") { if !h.is_empty() { return Ok(PathBuf::from(h)); } }\n    if let Ok(h) = std::env::var(\"HOME\") { if !h.is_empty() { return Ok(PathBuf::from(h).join(\".rustup\")); } }\n    Err(\"set RUSTUP_HOME or HOME\".into())\n}\n// ensure this returns Ok before invoking rustup-dependent code","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set RUSTUP_HOME explicitly in headless/container/service environments.","Keep HOME set whenever rustup or Cargo runs.","Pin toolchain locations in deployment configs to avoid home-dir discovery."],"tags":["environment","rustup-home","home-dir","config","rustup"],"backgroundTag":null,"analyzedSha":"eb98b54bc9f3c74519f43d066cb3fd02ebc88df0","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}