{"record":{"id":"74b810ce76ca2860","repo":"tracel-ai/burn","slug":"failed-to-create-cache-directory","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/afine/weights.rs","lineNumber":54,"sourceCode":"use std::path::PathBuf;\n\nuse super::calibrators::{AfineAdapter, FrCalibratorWithLimit, NrCalibrator};\nuse super::metric::Afine;\n\n/// Source URL for `afine.pth` on Hugging Face.\nconst AFINE_URL: &str =\n    \"https://huggingface.co/chaofengc/IQA-PyTorch-Weights/resolve/main/afine.pth\";\n\n/// Cached filename inside `~/.cache/burn-dataset/afine/`.\nconst CACHE_FILENAME: &str = \"afine.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(\"afine\");\n    if !cache_dir.exists() {\n        create_dir_all(&cache_dir).expect(\"Failed to create cache directory\");\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\n/// Download `afine.pth` (if not cached) and load all six shards into\n/// the matching submodules of an `Afine` previously produced by\n/// `AfineConfig::init`.\n///\n/// Errors during loading are logged via `log::warn!` and the function\n/// returns the module unconditionally — `allow_partial(true)` plus","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-train/src/metric/vision/afine/weights.rs#L36-L72","documentation":"`get_cache_dir()` in the afine weights module calls `create_dir_all` on `~/.cache/burn-dataset/afine` and panics with this message if directory creation fails. This means the cache parent exists or was found, but the directory could not be created — usually a permissions problem or the path colliding with an existing non-directory file. It surfaces whenever `load_pretrained_weights` runs and the cache dir does not yet exist.","triggerScenarios":"First call to `load_pretrained_weights` (afine) when `~/.cache/burn-dataset/afine` is missing and `create_dir_all` fails: read-only home/cache filesystem, permission denied under a different effective user, or a regular file named `afine` (or `burn-dataset`) already exists at that path.","commonSituations":"Running the app once as root (creating root-owned `~/.cache/burn-dataset`) then again as a normal user; read-only container filesystems; read-only `$HOME` mounts; a stale file blocking the directory path.","solutions":["Check and fix permissions on the cache path: `mkdir -p ~/.cache/burn-dataset && chown -R $(whoami) ~/.cache/burn-dataset`","Remove any non-directory file colliding with `~/.cache/burn-dataset/afine` (`rm` the file) or move it aside","Point XDG_CACHE_HOME at a writable location and pre-create the directory","If a prior run created it as root in Docker, rebuild the image or run all stages with the same user"],"exampleFix":"// before (shell)\ncargo run -- train\n// after (shell)\nexport XDG_CACHE_HOME=$HOME/.cache\nmkdir -p $XDG_CACHE_HOME/burn-dataset/afine\ncargo run -- train","handlingStrategy":"validation","validationCode":"// Pre-check writability of the cache path before calling the library\nlet cache = dirs::cache_dir().unwrap().join(\"burn-dataset\").join(\"afine\");\nmatch std::fs::create_dir_all(&cache) {\n    Ok(_) => {},\n    Err(e) => panic!(\"cache dir unusable: {} ({e})\", cache.display()),\n}\n// detect a non-directory blocking the path\nif cache.exists() && !cache.is_dir() { eprintln!(\"{} is a file, not a dir\", cache.display()); }","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!(\"cache dir creation failed; fix ~/.cache/burn-dataset permissions\"))?;","preventionTips":["Create ~/.cache/burn-dataset yourself at install/startup time with correct ownership","Never run first-time setup as root and later runs as a normal user in the same home","Ensure container filesystems/mounts at the cache path are writable","Check that no file named like the cache directory exists at that path"],"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"}