{"id":"68e9219d2df02a72","repo":"rust-lang/cargo","slug":"failed-to-open","errorCode":null,"errorMessage":"failed to open `{}`: {}","messagePattern":"failed to open `(.+?)`: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_clean.rs","lineNumber":162,"sourceCode":"    Ok(())\n}\n\nfn validate_target_dir_tag(target_dir_path: &Path) -> CargoResult<()> {\n    const TAG_SIGNATURE: &[u8] = b\"Signature: 8a477f597d28d172789f06886806bc55\";\n\n    let tag_path = target_dir_path.join(\"CACHEDIR.TAG\");\n\n    // per https://bford.info/cachedir the tag file must not be a symlink\n    if tag_path.is_symlink() {\n        bail!(\"expect `CACHEDIR.TAG` to be a regular file, got a symlink\");\n    }\n\n    if !tag_path.is_file() {\n        bail!(\"missing or invalid `CACHEDIR.TAG` file\");\n    }\n\n    let mut file = fs::File::open(&tag_path)\n        .map_err(|err| anyhow::anyhow!(\"failed to open `{}`: {}\", tag_path.display(), err))?;\n\n    let mut buf = [0u8; TAG_SIGNATURE.len()];\n    match file.read_exact(&mut buf) {\n        Ok(()) if &buf[..] == TAG_SIGNATURE => {}\n        Err(e) if e.kind() != io::ErrorKind::UnexpectedEof => {\n            bail!(\"failed to read `{}`: {e}\", tag_path.display());\n        }\n        _ => {\n            bail!(\"invalid signature in `CACHEDIR.TAG` file\");\n        }\n    }\n\n    Ok(())\n}\n\nfn clean_specs(\n    clean_ctx: &mut CleanContext<'_>,\n    ws: &Workspace<'_>,","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_clean.rs#L144-L180","documentation":"A low-level I/O failure while opening the `CACHEDIR.TAG` file inside `validate_target_dir_tag`. `fs::File::open` is mapped to an anyhow error at mod.rs:162 that reports the tag path and the underlying OS error. It surfaces, for example, permission-denied or a dangling path encountered after the existence checks passed.","triggerScenarios":"`cargo clean --target-dir <dir>` where `CACHEDIR.TAG` exists and is a regular file, but `open()` fails (e.g. EACCES, or a race where the file was removed between the `is_file()` check and the open).","commonSituations":"Permission issues on the target directory (read-only checkout, container with mismatched UID), filesystem race with another process, or a corrupted/inaccessible tag file.","solutions":["Check permissions on `<dir>/CACHEDIR.TAG` and its parent: ensure the cargo process can read them.","If running in a container/CI, verify the UID/GID matches the directory owner.","Remove the directory manually (`rm -rf <dir>`) if it is safe and let cargo recreate it on the next build."],"exampleFix":"# before\ncargo clean --target-dir ./target   # EACCES on CACHEDIR.TAG\n\n# after\nsudo chown -R $USER ./target\ncargo clean --target-dir ./target","handlingStrategy":"try-catch","validationCode":"// Pre-check read permission on CACHEDIR.TAG before invoking cargo clean --target-dir.\nuse std::path::Path;\nfn tag_readable(path: &Path) -> bool {\n    let tag = path.join(\"CACHEDIR.TAG\");\n    std::fs::File::open(&tag).is_ok()\n}","typeGuard":null,"tryCatchPattern":"// Cargo clean returns a CargoResult; map IO-classified errors to a friendlier message.\nmatch ops::clean(&ws, &opts) {\n    Ok(()) => {},\n    Err(e) if e.to_string().contains(\"failed to open\") => {\n        eprintln!(\"target dir not readable; check permissions on {}\", opts_dir.display());\n        return Err(e);\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Run cargo under the UID that owns the target directory.","In containers, chown the target dir to the cargo user in the image.","Add a pre-clean permission probe in your build scripts."],"tags":["cargo-clean","io","permissions","filesystem"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}