{"record":{"id":"4b67a0f2cbab9be4","repo":"sxyazi/yazi","slug":"restore-target-already-exists-to","errorCode":null,"errorMessage":"restore target already exists: {to:?}","messagePattern":"restore target already exists: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"yazi-fs/src/trash/common.rs","lineNumber":14,"sourceCode":"#[cfg(trash_unix)]\npub(super) fn restore_item(from: &std::path::Path, to: &std::path::Path) -> std::io::Result<()> {\n\tuse std::{fs, io};\n\n\tlet is_dir = fs::symlink_metadata(from)?.is_dir();\n\tif let Some(parent) = to.parent() {\n\t\tfs::create_dir_all(parent)?;\n\t}\n\n\tmatch if is_dir { fs::create_dir(to) } else { fs::File::create_new(to).map(|_| ()) } {\n\t\tOk(()) => fs::rename(from, to),\n\t\tErr(e) if e.kind() == io::ErrorKind::AlreadyExists => Err(io::Error::new(\n\t\t\tio::ErrorKind::AlreadyExists,\n\t\t\tformat!(\"restore target already exists: {to:?}\"),\n\t\t)),\n\t\tErr(e) => Err(e),\n\t}\n}\n","sourceCodeStart":1,"sourceCodeEnd":19,"githubUrl":"https://github.com/sxyazi/yazi/blob/5f901b886b14de1f17460b6e52e9de5d67f8aba9/yazi-fs/src/trash/common.rs#L1-L19","documentation":"restore_item restores a trashed file by first creating the target exclusively (create_dir for dirs, File::create_new for files) and then renaming the backing file into place. If the target already exists, the exclusive create fails with AlreadyExists, which is re-thrown with the clearer message \"restore target already exists: {to:?}\". The library refuses to overwrite existing files during restore.","triggerScenarios":"Calling restore (yazi-fs trash restore) when a file or directory already exists at the recorded original path — e.g. a same-named file was recreated after deletion, or the item was deleted, restored once, and restored again.","commonSituations":"Restoring an old download whose filename was re-created; batch-restoring a directory that partially exists; concurrent restore operations from two instances.","solutions":["Check the target path for existence before restoring and rename the incoming item (e.g. append \" (1)\") or move the existing one aside.","Delete or move the conflicting target if it is safe to overwrite, then retry the restore.","Restore to an alternative location by adjusting the item's original path before restoring."],"exampleFix":"// before\nfs::restore(entries).await?;\n// after\nfor e in &entries {\n    if let Some(to) = &e.original {\n        if to.exists() {\n            let alt = to.with_file_name(format!(\"{} (restored)\", to.file_name().unwrap().to_string_lossy()));\n            fs::rename(&e.backing, &alt).await?;\n            continue;\n        }\n    }\n}\nfs::restore(entries).await?;","handlingStrategy":"validation","validationCode":"// Rust\nif to.exists() {\n    let to = to.with_file_name(format!(\"{} (1)\", to.file_name().unwrap().to_string_lossy()));\n}","typeGuard":null,"tryCatchPattern":"match trash::restore(entries).await {\n    Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => prompt_overwrite_or_rename(),\n    other => other?,\n}","preventionTips":["Check the original path for existence before restoring.","Auto-rename restored items on conflict instead of overwriting.","Avoid restoring the same item twice without refreshing trash state."],"tags":["trash","restore","conflict","already-exists","io"],"backgroundTag":"restore-target-already-exists","analyzedSha":"5f901b886b14de1f17460b6e52e9de5d67f8aba9","analyzedAt":"2026-09-02T18:38:25.566Z","contentChangedAt":"2026-09-02T18:38:25.566Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}