tracel-ai/burn · error

Failed to create cache directory

Error message

Failed to create cache directory

What it means

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.

Source

Thrown at crates/burn-train/src/metric/vision/dists/weights.rs:27

use super::metric::Dists;

/// URL for pretrained DISTS alpha/beta weights from the official repository.
/// Reference: https://github.com/dingkeyan93/DISTS
const DISTS_WEIGHTS_URL: &str =
    "https://github.com/dingkeyan93/DISTS/raw/master/DISTS_pytorch/weights.pt";

/// URL for ImageNet pretrained VGG16 backbone weights from PyTorch.
const VGG16_IMAGENET_URL: &str = "https://download.pytorch.org/models/vgg16-397923af.pth";

/// Get the cache directory for DISTS weights.
fn get_cache_dir() -> PathBuf {
    let cache_dir = dirs::cache_dir()
        .expect("Could not get cache directory")
        .join("burn-dataset")
        .join("dists");

    if !cache_dir.exists() {
        create_dir_all(&cache_dir).expect("Failed to create cache directory");
    }

    cache_dir
}

/// Download file if not cached.
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 and load pretrained weights into a DISTS module.
///
/// This loads both:
/// 1. ImageNet pretrained VGG16 backbone weights

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Fix ownership/permissions: `mkdir -p ~/.cache/burn-dataset && chown -R $(whoami) ~/.cache/burn-dataset`
  2. Remove any non-directory file at `~/.cache/burn-dataset/dists` or `~/.cache/burn-dataset`
  3. Set XDG_CACHE_HOME to a writable path and pre-create it
  4. In Docker/K8s, mount an empty writable volume at the cache path

Example fix

// before (shell)
cargo run -- metric   # panics: Failed to create cache directory
// after (shell)
sudo rm -rf ~/.cache/burn-dataset   # if root-owned leftovers
mkdir -p ~/.cache/burn-dataset/dists
cargo run -- metric
Defensive patterns

Strategy: validation

Validate before calling

let dists_dir = dirs::cache_dir().unwrap().join("burn-dataset").join("dists");
std::fs::create_dir_all(&dists_dir).expect("cannot prepare dists cache dir");
if dists_dir.exists() && !dists_dir.is_dir() {
    eprintln!("{} blocks the cache dir; remove it", dists_dir.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!("dists cache dir creation failed; check ~/.cache permissions"))?;

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/259a7cf5a087dd00. Report an issue: GitHub.