{"record":{"id":"d7f12b1a65a78ba1","repo":"tracel-ai/burn","slug":"failed-to-create-cache-directory-for-gram-matrix-l","errorCode":null,"errorMessage":"Failed to create cache directory for Gram Matrix Loss","messagePattern":"Failed to create cache directory for Gram Matrix Loss","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-vision/src/loss/gram_matrix/weights.rs","lineNumber":25,"sourceCode":"use std::fs::{File, create_dir_all, rename};\nuse std::io::Write;\nuse std::path::PathBuf;\n\nconst VGG19_URL: &str = \"https://download.pytorch.org/models/vgg19-dcbb9e9d.pth\";\n\n/// Resolves and returns the local cache directory for the VGG19 weights.\n///\n/// Creates the directory `~/.cache/burn-pretrained-models/loss/vgg19/`\n/// (or OS equivalent) if it does not already exist.\nfn get_cache_dir() -> PathBuf {\n    let cache_dir = dirs::cache_dir()\n        .expect(\"Failed to get cache directory for Gram Matrix Loss\")\n        .join(\"burn-pretrained-models\")\n        .join(\"loss\")\n        .join(\"vgg19\");\n\n    if !cache_dir.exists() {\n        create_dir_all(&cache_dir).expect(\"Failed to create cache directory for Gram Matrix Loss\");\n    }\n\n    cache_dir\n}\n\n/// Downloads the pretrained weights to the `cache_path` if they don't exist already.\n///\n/// Requires an active internet connection on the first run. Subsequent runs will\n/// use the locally cached `.pth` file.\nfn download_weights_if_not_saved(cache_path: &PathBuf) {\n    if !cache_path.exists() {\n        let bytes = download_file_as_bytes(\n            VGG19_URL,\n            \"Downloading VGG19 ImageNet weights for Gram Matrix Loss...\",\n        );\n\n        // Write to a temporary file. If writing gets completed, then rename to the actual/correct name.\n        // If writing is not completed, the file with the correct name (i.e. `cache_path`) will not exist","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-vision/src/loss/gram_matrix/weights.rs#L7-L43","documentation":"`get_cache_dir` in crates/burn-vision/src/loss/gram_matrix/weights.rs:25 panics when `create_dir_all` fails to create `~/.cache/burn-pretrained-models/loss/vgg19/`. The cache parent was resolvable (dirs::cache_dir() worked), but the directories could not actually be created on disk. This is a filesystem permission or state problem, not an environment problem.","triggerScenarios":"Calling `load_vgg19_weights` when `~/.cache/burn-pretrained-models/loss/vgg19` does not exist and `create_dir_all` returns Err: parent directory not writable by the current user, a file already exists at one of the path components, or a read-only filesystem.","commonSituations":"Read-only container root filesystems (Kubernetes securityContext with readOnlyRootFilesystem); ~/.cache owned by root after an earlier sudo run; path collision where ~/.cache/burn-pretrained-models exists as a regular file; immutable/locked directories.","solutions":["Check permissions on the cache path and fix ownership: `ls -la ~/.cache` then `chown -R $(id -u) ~/.cache`","Remove a conflicting regular file if a path component like ~/.cache/burn-pretrained-models is a file, not a directory: `rm ~/.cache/burn-pretrained-models`","Point the cache somewhere writable by setting XDG_CACHE_HOME to a writable directory","Mount or remount the target filesystem read-write if it is read-only (containers: add an emptyDir volume for the cache path)"],"exampleFix":"// before: panic on mkdir failure\ncreate_dir_all(&cache_dir).expect(\"Failed to create cache directory for Gram Matrix Loss\");\n// after: actionable error\ncreate_dir_all(&cache_dir)\n    .with_context(|| format!(\"Failed to create cache dir {} — check permissions\", cache_dir.display()))?;","handlingStrategy":"validation","validationCode":"// Ensure the target cache dir tree is creatable before calling the library\nlet base = dirs::cache_dir().expect(\"cache dir env\");\nlet vgg19_dir = base.join(\"burn-pretrained-models/loss/vgg19\");\nstd::fs::create_dir_all(&vgg19_dir)\n    .unwrap_or_else(|e| panic!(\"cannot create {}: {e}; fix permissions\", vgg19_dir.display()));\n// also confirm no file occupies a path component\nfor anc in [base.join(\"burn-pretrained-models\"), base.join(\"burn-pretrained-models/loss\")] {\n    assert!(!anc.is_file(), \"{} is a file, not a directory — remove it\", anc.display());\n}","typeGuard":"fn cache_dir_creatable(base: &std::path::Path) -> bool {\n    let probe = base.join(\"burn-pretrained-models/loss/vgg19\");\n    std::fs::create_dir_all(&probe).is_ok()\n        && OpenOptions::new().write(true).create(true)\n            .open(probe.join(\".probe\")).is_ok()\n}","tryCatchPattern":"let result = std::panic::catch_unwind(||\n    burn_vision::loss::gram_matrix::load_vgg19_weights(device)\n);\nif result.is_err() {\n    eprintln!(\"failed to create vgg19 cache dir — check ~/.cache permissions and read-only mounts\");\n}","preventionTips":["Never run with sudo for one dependency then as a user for another (mixed-ownership ~/.cache)","Avoid read-only root filesystems without a writable volume mounted at the cache path","Check ~/.cache for path collisions after manual experiments","Pre-create the cache directory at install/deploy time"],"tags":["filesystem","permissions","cache","panics","burn"],"backgroundTag":"permission-denied","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"}