{"record":{"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":172,"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":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/ops/cargo_clean.rs#L154-L190","documentation":"Thrown by cargo_clean when fs::File::open on target/CACHEDIR.TAG fails, after earlier checks already confirmed the path is a regular non-symlink file. The format string interpolates the path and the underlying io::Error, so this is an OS-level open failure (permissions, race with deletion, filesystem error) rather than a missing-file mistake, which is caught earlier.","triggerScenarios":"Another process deletes target/ between the is_file() check and the open() call; file permissions deny read access to the current user; a read-only filesystem mount; antivirus/container sandbox blocking open; CACHEDIR.TAG exists but is held exclusively on Windows.","commonSituations":"Running `cargo clean` while a build or IDE is concurrently writing to target/; CI runners with restricted perms; NFS/network filesystems with stale file handles; switched user / sudo leaving target owned by root.","solutions":["Re-run `cargo clean` with no concurrent cargo process active.","Check ownership and permissions of target/CACHEDIR.TAG and fix with chmod/chown if owned by another user.","If the filesystem is read-only or sandboxed, remount writable or relax the sandbox for the target dir.","As a last resort, remove the target directory manually: `rm -rf target`."],"exampleFix":"# before (target owned by root from a sudo build)\ncargo clean\n# after\nsudo chown -R $USER:$USER target && cargo clean","handlingStrategy":"try-catch","validationCode":"// Verify target dir is cleanable before invoking cargo clean.\nuse std::os::unix::fs::PermissionsExt;\nfn writable(p: &std::path::Path) -> bool {\n    std::fs::metadata(p).map(|m| m.permissions().mode() & 0o200 != 0).unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":"// Wrap cargo clean invocations and fall back to manual removal on open failure.\nmatch std::process::Command::new(\"cargo\").arg(\"clean\").status() {\n    Ok(s) if s.success() => {},\n    _ => { let _ = std::fs::remove_dir_all(\"target\"); }\n}","preventionTips":["Do not run `cargo clean` concurrently with builds.","Ensure the target directory is owned by the invoking user.","On CI, prefer a fresh workspace over cleaning a shared one."],"tags":["cargo-clean","io","filesystem","permissions","race-condition"],"backgroundTag":null,"analyzedSha":"98a09e7e7d62850f14e5b6132101fc1edd19a16f","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}