gleam-lang/gleam · error
Failed to determine user cache directory
Error message
Failed to determine user cache directory
What it means
`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.
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.
Example fix
// before
dirs_next::cache_dir()
.expect("Failed to determine user cache directory")
// after
dirs_next::cache_dir().unwrap_or_else(|| {
std::env::temp_dir().join(".gleam-cache")
}) Defensive patterns
Strategy: fallback
Validate before calling
// shell: ensure cache dir can be resolved
[ -n "${XDG_CACHE_HOME:-${HOME:-}}" ] || { export HOME="${HOME:-/tmp}"; } Try / catch
// in code
let dir = dirs_next::cache_dir().unwrap_or_else(|| std::env::temp_dir().join("gleam-cache")); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: CI containers running as non-root without HOME, systemd services with stripped environments, exotic platforms dirs_next doesn't support, Windows profile redirection issues.
Understand the failure class
Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.
Related errors
AI-assisted analysis of gleam-lang/gleam@15b07c7830 (2026-09-14).
Data as JSON: /api/errors/3fdcbedf884f34a0.
Report an issue: GitHub.
Appendix: source
Thrown at compiler-core/src/paths.rs:182
global_hexpm_cache().join("credentials.toml")
}
pub fn global_hexpm_legacy_credentials_path() -> Utf8PathBuf {
global_hexpm_cache().join("credentials")
}
fn global_hexpm_cache() -> Utf8PathBuf {
default_global_gleam_cache().join("hex").join("hexpm")
}
fn global_packages_cache() -> Utf8PathBuf {
global_hexpm_cache().join("packages")
}
pub fn default_global_gleam_cache() -> Utf8PathBuf {
Utf8PathBuf::from_path_buf(
dirs_next::cache_dir()
.expect("Failed to determine user cache directory")
.join("gleam"),
)
.expect("Non Utf8 Path")
}
pub fn unnest(within: &Utf8Path) -> Utf8PathBuf {
let mut path = Utf8PathBuf::new();
for _ in within {
path = path.join("..");
}
path
}
#[test]
fn paths() {
assert!(default_global_gleam_cache().ends_with("gleam"));
assert!(global_packages_cache().ends_with("hex/hexpm/packages"));View on GitHub (pinned to 15b07c7830)