{"record":{"id":"3fdcbedf884f34a0","repo":"gleam-lang/gleam","slug":"failed-to-determine-user-cache-directory","errorCode":null,"errorMessage":"Failed to determine user cache directory","messagePattern":"Failed to determine user cache directory","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/paths.rs","lineNumber":182,"sourceCode":"    global_hexpm_cache().join(\"credentials.toml\")\n}\n\npub fn global_hexpm_legacy_credentials_path() -> Utf8PathBuf {\n    global_hexpm_cache().join(\"credentials\")\n}\n\nfn global_hexpm_cache() -> Utf8PathBuf {\n    default_global_gleam_cache().join(\"hex\").join(\"hexpm\")\n}\n\nfn global_packages_cache() -> Utf8PathBuf {\n    global_hexpm_cache().join(\"packages\")\n}\n\npub fn default_global_gleam_cache() -> Utf8PathBuf {\n    Utf8PathBuf::from_path_buf(\n        dirs_next::cache_dir()\n            .expect(\"Failed to determine user cache directory\")\n            .join(\"gleam\"),\n    )\n    .expect(\"Non Utf8 Path\")\n}\n\npub fn unnest(within: &Utf8Path) -> Utf8PathBuf {\n    let mut path = Utf8PathBuf::new();\n    for _ in within {\n        path = path.join(\"..\");\n    }\n    path\n}\n\n#[test]\nfn paths() {\n    assert!(default_global_gleam_cache().ends_with(\"gleam\"));\n\n    assert!(global_packages_cache().ends_with(\"hex/hexpm/packages\"));","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/gleam-lang/gleam/blob/15b07c783065c92bb016b1b74ed0995a5259850f/compiler-core/src/paths.rs#L164-L200","documentation":"`default_global_gleam_cache` resolves the user's cache directory via dirs_next::cache_dir() and expects it to exist. On systems with no resolvable cache dir (no XDG_CACHE_HOME, no HOME, or unsupported platform), dirs_next returns None and this panic fires.","triggerScenarios":"Running any Gleam command that touches the global hexpm/gleam cache (build, deps download, publish) in an environment where the OS cache directory cannot be determined: missing $HOME/XDG_CACHE_HOME, restricted service accounts, containers without HOME set.","commonSituations":"CI containers running as non-root without HOME, systemd services with stripped environments, exotic platforms dirs_next doesn't support, Windows profile redirection issues.","solutions":["Set HOME (and XDG_CACHE_HOME on Linux) to a writable directory before running gleam.","In containers/CI: ENV HOME=/tmp or XDG_CACHE_HOME=/tmp/.cache.","If you control the code, fall back to a temp dir instead of panicking when cache_dir() is None."],"exampleFix":"// before\ndirs_next::cache_dir()\n    .expect(\"Failed to determine user cache directory\")\n// after\ndirs_next::cache_dir().unwrap_or_else(|| {\n    std::env::temp_dir().join(\".gleam-cache\")\n})","handlingStrategy":"fallback","validationCode":"// shell: ensure cache dir can be resolved\n[ -n \"${XDG_CACHE_HOME:-${HOME:-}}\" ] || { export HOME=\"${HOME:-/tmp}\"; }","typeGuard":null,"tryCatchPattern":"// in code\nlet dir = dirs_next::cache_dir().unwrap_or_else(|| std::env::temp_dir().join(\"gleam-cache\"));","preventionTips":["Set HOME (and XDG_CACHE_HOME on Linux) in CI containers and services.","Run gleam as a user with a resolvable profile/cache directory.","Prefer a temp-dir fallback over panicking when the cache dir is unavailable."],"tags":["rust","environment","cache","paths"],"backgroundTag":"missing-env-var","analyzedSha":"15b07c783065c92bb016b1b74ed0995a5259850f","analyzedAt":"2026-09-14T11:14:59.388Z","contentChangedAt":"2026-09-14T11:14:59.388Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}