{"record":{"id":"1e57878c0b031bcb","repo":"nikivdev/code","slug":"refusing-to-overwrite-1e5787","errorCode":null,"errorMessage":"Refusing to overwrite {}","messagePattern":"Refusing to overwrite (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ext.rs","lineNumber":202,"sourceCode":"        if candidate.exists() {\n            return current;\n        }\n        if !current.pop() {\n            return cwd;\n        }\n    }\n}\n\nfn copy_dir_all(from: &Path, to: &Path) -> Result<()> {\n    fs::create_dir_all(to).with_context(|| format!(\"failed to create {}\", to.display()))?;\n    for entry in fs::read_dir(from).with_context(|| format!(\"failed to read {}\", from.display()))? {\n        let entry = entry?;\n        let path = entry.path();\n        let file_type = entry.file_type()?;\n        let target = to.join(entry.file_name());\n\n        if target.exists() {\n            bail!(\"Refusing to overwrite {}\", target.display());\n        }\n\n        if file_type.is_dir() {\n            copy_dir_all(&path, &target)?;\n        } else if file_type.is_file() {\n            fs::copy(&path, &target)\n                .with_context(|| format!(\"failed to copy {}\", path.display()))?;\n        } else if file_type.is_symlink() {\n            let link_target = fs::read_link(&path)\n                .with_context(|| format!(\"failed to read link {}\", path.display()))?;\n            copy_symlink(&link_target, &target)?;\n        }\n    }\n    Ok(())\n}\n\nfn copy_symlink(target: &Path, dest: &Path) -> Result<()> {\n    #[cfg(unix)]","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/ext.rs#L184-L220","documentation":"copy_dir_all performs a strictly non-overwriting recursive copy: before copying each entry it checks whether the target path exists and aborts if so. This guards against races and against resuming onto a partially populated destination.","triggerScenarios":"Another process creates files in the destination between the top-level dest.exists() check and the copy loop; a previous failed import left partial files and copy_dir_all is re-invoked on the same target; recursive descent encounters a pre-existing subdirectory.","commonSituations":"Concurrent imports of the same source; cleaning up a failed import by deleting only some files; copying onto a directory that already contains a file with the same name (e.g. README.md).","solutions":["Remove the partially populated destination directory entirely and re-run the import from scratch.","Ensure no other process is writing to the destination concurrently.","If re-importing intentionally, clear conflicting entries first rather than copying over them."],"exampleFix":"// before\ncopy_dir_all(&source_workspace, &dest)?; // dest has leftovers\n// after\nif dest.exists() { std::fs::remove_dir_all(&dest)?; }\ncopy_dir_all(&source_workspace, &dest)?;","handlingStrategy":"validation","validationCode":"fn dir_is_empty(p: &Path) -> std::io::Result<bool> {\n    Ok(!p.exists() || std::fs::read_dir(p)?.next().is_none())\n}\nif !dir_is_empty(Path::new(&dest))? { /* clear or abort */ }","typeGuard":"fn copy_target_clear(to: &Path, name: &OsStr) -> bool {\n    !to.join(name).exists()\n}","tryCatchPattern":"match copy_dir_all(&src, &dest) {\n    Err(e) if e.to_string().starts_with(\"Refusing to overwrite\") => {\n        std::fs::remove_dir_all(&dest)?; // clear partial copy\n        copy_dir_all(&src, &dest)?;\n    }\n    r => r?,\n}","preventionTips":["Always delete a failed import's destination directory before retrying.","Serialize imports (no concurrent imports into the same ext/ dir).","Copy into a temp dir and rename atomically to avoid partial states."],"tags":["filesystem","race-condition","copy"],"backgroundTag":"refusing-to-overwrite-existing-file","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}