{"record":{"id":"defa62fefbf1581f","repo":"tracel-ai/burn","slug":"could-not-get-cache-directory-defa62","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/fid/weights.rs","lineNumber":13,"sourceCode":"use burn_std::network::downloader::download_file_as_bytes;\nuse burn_store::{ModuleSnapshot, PytorchStore};\nuse std::fs::{File, create_dir_all};\nuse std::io::Write;\nuse std::path::PathBuf;\n\nuse super::metric::Fid;\n\nconst INCEPTION_WEIGHTS_URL: &str = \"https://github.com/mseitzer/pytorch-fid/releases/download/fid_weights/pt-inception-2015-12-05-6726825d.pth\";\n\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(\"fid\");\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\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}\n","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-train/src/metric/vision/fid/weights.rs#L1-L31","documentation":"This is the `expect` on `dirs::cache_dir()` in the FID weights module's `get_cache_dir()`, used by `load_pretrained_weights` to resolve `~/.cache/burn-dataset/fid` for the Inception weights (`pt-inception-2015-12-05-6726825d.pth`). It panics when the platform has no determinable user cache directory. Any FID computation that needs the pretrained Inception network then fails at startup.","triggerScenarios":"Calling FID `load_pretrained_weights` on a system where `dirs::cache_dir()` returns None — Linux with HOME and XDG_CACHE_HOME unset (bare Docker image, systemd service, CI runner, cron job).","commonSituations":"Root containers with no HOME; services started by init systems without user env; minimal CI images; headless servers with scrubbed environments.","solutions":["Set `XDG_CACHE_HOME` (or HOME) to a writable directory in the process environment","In Docker, add `ENV XDG_CACHE_HOME=/cache` and create or mount that path","Pre-download `pt-inception-2015-12-05-6726825d.pth` into the expected cache dir once the env is fixed","Change code to fall back to a temp dir when `dirs::cache_dir()` is None"],"exampleFix":"// before (shell)\n./fid-eval   # panic: Could not get cache directory\n// after (shell)\nexport XDG_CACHE_HOME=/tmp/.cache\nmkdir -p /tmp/.cache\n./fid-eval","handlingStrategy":"fallback","validationCode":"fn ensure_fid_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 FID evaluation\"); }","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!(\"FID weight load panicked; ensure XDG_CACHE_HOME/HOME is set\"))?;","preventionTips":["Export XDG_CACHE_HOME in shell profiles, Dockerfiles, and job specs that run FID evaluation","Mount a writable cache volume in containers; never rely on the default env in minimal images","Run an env self-check at startup that fails fast with a clear message","Use one consistent runtime user 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"}