{"record":{"id":"b008e05e30ca435b","repo":"tracel-ai/burn","slug":"could-not-get-cache-directory-b008e0","errorCode":null,"errorMessage":"Could not get cache directory","messagePattern":"Could not get cache directory","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-train/src/metric/vision/dists/weights.rs","lineNumber":22,"sourceCode":"use burn_store::{ModuleSnapshot, PytorchStore};\nuse std::fs::{File, create_dir_all};\nuse std::io::Write;\nuse std::path::PathBuf;\n\nuse super::metric::Dists;\n\n/// URL for pretrained DISTS alpha/beta weights from the official repository.\n/// Reference: https://github.com/dingkeyan93/DISTS\nconst DISTS_WEIGHTS_URL: &str =\n    \"https://github.com/dingkeyan93/DISTS/raw/master/DISTS_pytorch/weights.pt\";\n\n/// URL for ImageNet pretrained VGG16 backbone weights from PyTorch.\nconst VGG16_IMAGENET_URL: &str = \"https://download.pytorch.org/models/vgg16-397923af.pth\";\n\n/// Get the cache directory for DISTS weights.\nfn get_cache_dir() -> PathBuf {\n    let cache_dir = dirs::cache_dir()\n        .expect(\"Could not get cache directory\")\n        .join(\"burn-dataset\")\n        .join(\"dists\");\n\n    if !cache_dir.exists() {\n        create_dir_all(&cache_dir).expect(\"Failed to create cache directory\");\n    }\n\n    cache_dir\n}\n\n/// Download file if not cached.\nfn download_if_needed(url: &str, cache_path: &PathBuf, message: &str) {\n    if !cache_path.exists() {\n        let bytes = download_file_as_bytes(url, message);\n        let mut file = File::create(cache_path).expect(\"Failed to create cache file\");\n        file.write_all(&bytes).expect(\"Failed to write weights\");\n    }\n}","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-train/src/metric/vision/dists/weights.rs#L4-L40","documentation":"This is the `expect` on `dirs::cache_dir()` in the DISTS weights module's `get_cache_dir()`, used by `load_pretrained_weights` to locate `~/.cache/burn-dataset/dists`. It panics when the OS-level user cache directory cannot be determined (no `$XDG_CACHE_HOME`/`$HOME` on Linux, or no platform cache path). Any DISTS pretrained-weight load then aborts before downloading the VGG16 backbone.","triggerScenarios":"Calling `load_pretrained_weights` for the DISTS metric on a host where `dirs::cache_dir()` is None — Linux process with HOME and XDG_CACHE_HOME unset (minimal Docker image, systemd unit, CI job, cron).","commonSituations":"Containers running as root with scrubbed env; daemons/services without a user session; stripped-down CI images; platforms where the `dirs` crate has no cache-dir convention.","solutions":["Export `XDG_CACHE_HOME=/some/writable/dir` (or set HOME) before launching the program","In Docker, add `ENV XDG_CACHE_HOME=/cache` plus a created/mounted directory","Pre-provision the dists cache so the environment issue is caught early, or run with a fixed env via a wrapper script","Patch to fall back to `std::env::temp_dir()` when `dirs::cache_dir()` returns None"],"exampleFix":"// before (systemd unit)\n[Service]\nExecStart=/usr/local/bin/trainer\n// after (systemd unit)\n[Service]\nEnvironment=XDG_CACHE_HOME=/var/cache/trainer\nExecStart=/usr/local/bin/trainer","handlingStrategy":"fallback","validationCode":"fn ensure_dists_cache_base() -> std::path::PathBuf {\n    let base = dirs::cache_dir()\n        .or_else(|| std::env::var(\"XDG_CACHE_HOME\").ok().map(std::path::PathBuf::from))\n        .unwrap_or_else(std::env::temp_dir);\n    std::fs::create_dir_all(&base).expect(\"cache base unusable\");\n    base\n}\nif dirs::cache_dir().is_none() { eprintln!(\"set XDG_CACHE_HOME before loading DISTS weights\"); }","typeGuard":"fn cache_dir_available() -> bool { dirs::cache_dir().is_some() }","tryCatchPattern":"let weights = std::panic::catch_unwind(load_pretrained_weights)\n    .map_err(|_| anyhow!(\"DISTS weight load panicked; ensure XDG_CACHE_HOME/HOME is set\"))?;","preventionTips":["Set XDG_CACHE_HOME explicitly in every deployment manifest (Docker, K8s, systemd, CI)","Mount a writable volume at the cache location in containers","Validate the runtime environment (env vars present) as a startup health check","Keep the runtime user consistent across runs"],"tags":["panic","filesystem","cache-dir","environment"],"backgroundTag":"cache-dir-unavailable","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}