{"record":{"id":"23e4146df44b8194","repo":"rust-lang/cargo","slug":"cargo-couldn-t-find-your-home-directory-this-prob","errorCode":null,"errorMessage":"Cargo couldn't find your home directory. This probably means that $HOME was not set.","messagePattern":"Cargo couldn't find your home directory\\. This probably means that \\$HOME was not set\\.","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"critical","filePath":"src/context/mod.rs","lineNumber":424,"sourceCode":"            progress_config: ProgressConfig::default(),\n            env_config: Default::default(),\n            nightly_features_allowed: matches!(&*features::channel(), \"nightly\" | \"dev\"),\n            ws_roots: Default::default(),\n            global_cache_tracker: Default::default(),\n            deferred_global_last_use: Default::default(),\n        }\n    }\n\n    /// Creates a new instance, with all default settings.\n    ///\n    /// This does only minimal initialization. In particular, it does not load\n    /// any config files from disk. Those will be loaded lazily as-needed.\n    pub fn default() -> CargoResult<GlobalContext> {\n        let shell = Shell::new();\n        let cwd =\n            env::current_dir().context(\"couldn't get the current directory of the process\")?;\n        let homedir = homedir(&cwd).ok_or_else(|| {\n            anyhow!(\n                \"Cargo couldn't find your home directory. \\\n                 This probably means that $HOME was not set.\"\n            )\n        })?;\n        Ok(GlobalContext::new(shell, cwd, homedir))\n    }\n\n    /// Gets the user's Cargo home directory (OS-dependent).\n    pub fn home(&self) -> &Filesystem {\n        &self.home_path\n    }\n\n    /// Returns a path to display to the user with the location of their home\n    /// config file (to only be used for displaying a diagnostics suggestion,\n    /// such as recommending where to add a config value).\n    pub fn diagnostic_home_config(&self) -> String {\n        let home = self.home_path.as_path_unlocked();\n        let path = match self.get_file_path(home, \"config\", false) {","sourceCodeStart":406,"sourceCodeEnd":442,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/context/mod.rs#L406-L442","documentation":"`GlobalContext::default()` could not determine the user's home directory (`homedir(&cwd)` returned `None`). Cargo needs `$HOME` (or the OS equivalent) to locate `~/.cargo` for registries, caches, and global config, so it bails early with this hint.","triggerScenarios":"Running cargo in an environment where neither `HOME`, nor platform-specific APIs (Windows `USERPROFILE`, etc.) resolve to a writable directory; running as a user with no passwd entry; minimal containers that clear the environment.","commonSituations":"Docker images that `env -i` and forget to set `HOME`; systemd services without `Environment=HOME=...`; chroot/nobody-user contexts; CI runners that strip env.","solutions":["Export `HOME` (or `USERPROFILE` on Windows) before invoking cargo: `export HOME=/tmp`.","Set `CARGO_HOME` to an explicit writable path which removes the dependency on `$HOME`.","Ensure the running user has a valid passwd entry (`getent passwd <user>`).","In containers, do not use `env -i` without re-exporting `HOME`."],"exampleFix":"// before (Dockerfile)\nRUN env -i cargo build --release\n\n// after\nENV HOME=/tmp\nENV CARGO_HOME=/tmp/cargo\nRUN cargo build --release","handlingStrategy":"validation","validationCode":"fn home_or_cargo_home_set() -> bool {\n    std::env::var_os(\"HOME\").or(std::env::var_os(\"USERPROFILE\")).or(std::env::var_os(\"CARGO_HOME\")).is_some()\n}","typeGuard":"null","tryCatchPattern":"match GlobalContext::default() {\n    Err(e) if e.to_string().contains(\"home directory\") => { std::env::set_var(\"HOME\", \"/tmp\"); GlobalContext::default()? }\n    r => r?,\n}","preventionTips":["Always set `HOME` (or `CARGO_HOME`) in containers and services.","Avoid `env -i` without re-exporting essential vars.","CI boot: assert `[[ -n \"$HOME\" ]] || export HOME=\"$PWD/home\"`."],"tags":["cargo","environment","home-directory","container","startup"],"backgroundTag":null,"analyzedSha":"98a09e7e7d62850f14e5b6132101fc1edd19a16f","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}