{"record":{"id":"259a7cf5a087dd00","repo":"tracel-ai/burn","slug":"failed-to-create-cache-directory-259a7c","errorCode":null,"errorMessage":"Failed to create cache directory","messagePattern":"Failed to create cache directory","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-train/src/metric/vision/dists/weights.rs","lineNumber":27,"sourceCode":"use 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}\n\n/// Download and load pretrained weights into a DISTS module.\n///\n/// This loads both:\n/// 1. ImageNet pretrained VGG16 backbone weights","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-train/src/metric/vision/dists/weights.rs#L9-L45","documentation":"In the DISTS weights module, `get_cache_dir()` panics via `create_dir_all(&cache_dir).expect(\"Failed to create cache directory\")` when `~/.cache/burn-dataset/dists` cannot be created. The OS cache root was resolved, but making the subdirectory failed — typically permissions, a read-only filesystem, or a non-directory file occupying the path. This fires on the first `load_pretrained_weights` call before any download.","triggerScenarios":"First DISTS `load_pretrained_weights` call when the dists cache dir is absent and `create_dir_all` fails: unwritable `~/.cache`, root-owned `burn-dataset` from an earlier root run, read-only container FS, or a file named `dists`/`burn-dataset` blocking the path.","commonSituations":"Mixed-user setups (root created cache, user runs later); immutable/read-only container images without a writable cache mount; disk quotas preventing mkdir; leftovers from a previous tooling run.","solutions":["Fix ownership/permissions: `mkdir -p ~/.cache/burn-dataset && chown -R $(whoami) ~/.cache/burn-dataset`","Remove any non-directory file at `~/.cache/burn-dataset/dists` or `~/.cache/burn-dataset`","Set XDG_CACHE_HOME to a writable path and pre-create it","In Docker/K8s, mount an empty writable volume at the cache path"],"exampleFix":"// before (shell)\ncargo run -- metric   # panics: Failed to create cache directory\n// after (shell)\nsudo rm -rf ~/.cache/burn-dataset   # if root-owned leftovers\nmkdir -p ~/.cache/burn-dataset/dists\ncargo run -- metric","handlingStrategy":"validation","validationCode":"let dists_dir = dirs::cache_dir().unwrap().join(\"burn-dataset\").join(\"dists\");\nstd::fs::create_dir_all(&dists_dir).expect(\"cannot prepare dists cache dir\");\nif dists_dir.exists() && !dists_dir.is_dir() {\n    eprintln!(\"{} blocks the cache dir; remove it\", dists_dir.display());\n}","typeGuard":"fn cache_dir_writable(dir: &std::path::Path) -> bool {\n    dir.is_dir() && std::fs::write(dir.join(\".probe\"), b\"\").is_ok()\n}","tryCatchPattern":"let weights = std::panic::catch_unwind(load_pretrained_weights)\n    .map_err(|_| anyhow!(\"dists cache dir creation failed; check ~/.cache permissions\"))?;","preventionTips":["Pre-create ~/.cache/burn-dataset/dists at image build or app startup","Chown the cache tree to the actual runtime user; avoid root-created caches in shared homes","Mount writable volumes where the image rootfs is read-only","Scan for non-directory files colliding with cache paths during setup"],"tags":["panic","filesystem","permissions","cache-dir"],"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"}