tracel-ai/burn · error
Failed to create cache directory
Error message
Failed to create cache directory
What it means
`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.
Source
Thrown at crates/burn-train/src/metric/vision/afine/weights.rs:54
use std::path::PathBuf;
use super::calibrators::{AfineAdapter, FrCalibratorWithLimit, NrCalibrator};
use super::metric::Afine;
/// Source URL for `afine.pth` on Hugging Face.
const AFINE_URL: &str =
"https://huggingface.co/chaofengc/IQA-PyTorch-Weights/resolve/main/afine.pth";
/// Cached filename inside `~/.cache/burn-dataset/afine/`.
const CACHE_FILENAME: &str = "afine.pth";
fn get_cache_dir() -> PathBuf {
let cache_dir = dirs::cache_dir()
.expect("Could not get cache directory")
.join("burn-dataset")
.join("afine");
if !cache_dir.exists() {
create_dir_all(&cache_dir).expect("Failed to create cache directory");
}
cache_dir
}
fn download_if_needed(url: &str, cache_path: &PathBuf, message: &str) {
if !cache_path.exists() {
let bytes = download_file_as_bytes(url, message);
let mut file = File::create(cache_path).expect("Failed to create cache file");
file.write_all(&bytes).expect("Failed to write weights");
}
}
/// Download `afine.pth` (if not cached) and load all six shards into
/// the matching submodules of an `Afine` previously produced by
/// `AfineConfig::init`.
///
/// Errors during loading are logged via `log::warn!` and the function
/// returns the module unconditionally — `allow_partial(true)` plusView on GitHub (pinned to d16f7ba2ed)
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
Example fix
// before (shell) cargo run -- train // after (shell) export XDG_CACHE_HOME=$HOME/.cache mkdir -p $XDG_CACHE_HOME/burn-dataset/afine cargo run -- train
Defensive patterns
Strategy: validation
Validate before calling
// Pre-check writability of the cache path before calling the library
let cache = dirs::cache_dir().unwrap().join("burn-dataset").join("afine");
match std::fs::create_dir_all(&cache) {
Ok(_) => {},
Err(e) => panic!("cache dir unusable: {} ({e})", cache.display()),
}
// detect a non-directory blocking the path
if cache.exists() && !cache.is_dir() { eprintln!("{} is a file, not a dir", cache.display()); } Type guard
fn cache_dir_writable(dir: &std::path::Path) -> bool {
dir.is_dir() && std::fs::write(dir.join(".probe"), b"").is_ok()
} Try / catch
let weights = std::panic::catch_unwind(load_pretrained_weights)
.map_err(|_| anyhow!("cache dir creation failed; fix ~/.cache/burn-dataset permissions"))?; Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Failed to create cache directory
- Failed to create cache directory
- Could not get cache directory
- Failed to create cache file
- Could not get cache directory
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/74b810ce76ca2860.
Report an issue: GitHub.