{"record":{"id":"929331cac2399ac0","repo":"Hmbown/CodeWhale","slug":"failed-to-install-new-binary-at","errorCode":null,"errorMessage":"failed to install new binary at {}: {}","messagePattern":"failed to install new binary at (.+?): (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/cli/src/update.rs","lineNumber":1778,"sourceCode":"\n    #[cfg(windows)]\n    {\n        let backup = backup_path_for(target);\n        if target.exists() {\n            std::fs::rename(target, &backup).with_context(|| {\n                format!(\n                    \"failed to move current executable {} to {}\",\n                    target.display(),\n                    backup.display()\n                )\n            })?;\n        }\n\n        if let Err(err) = tmp.persist(target) {\n            if backup.exists() {\n                let _ = std::fs::rename(&backup, target);\n            }\n            bail!(\n                \"failed to install new binary at {}: {}\",\n                target.display(),\n                err.error\n            );\n        }\n\n        let _ = std::fs::remove_file(&backup);\n    }\n\n    #[cfg(not(windows))]\n    {\n        tmp.persist(target)\n            .map_err(|err| err.error)\n            .with_context(|| format!(\"failed to rename temp file to {}\", target.display()))?;\n    }\n\n    Ok(())\n}","sourceCodeStart":1760,"sourceCodeEnd":1796,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/0c42157ee52f9d55af2b506d71b46249910f77d3/crates/cli/src/update.rs#L1760-L1796","documentation":"During self-update on Windows, after the current executable was moved aside to a backup, atomically persisting the new temporary binary onto the target path failed. The original binary is restored from the backup, so the installation is rolled back rather than left half-done; the underlying persist error is appended to the message.","triggerScenarios":"tempfile persist(target) fails: target directory not writable (installed under Program Files without elevation), the old binary is still running/locked (running process holds a write lock on Windows), antivirus quarantines or locks the new file, or the temp dir and target are on different volumes (EXDEV-style persist failure).","commonSituations":"CLI installed to a protected directory and updated without admin rights; a second Codewhale instance (TUI session, background agent) still running during update; over-aggressive AV scanning freshly written executables; TMP redirected to a different drive.","solutions":["Close all running Codewhale processes (TUI sessions, background agents) and retry the update","Run the update from a shell with write access to the install directory (elevate if installed under Program Files)","Read the appended persist error: 'Access is denied' → permissions/AV lock; different-drive error → set TMP to the same volume as the target","Add an AV exclusion for the Codewhale install directory if scans repeatedly lock the binary","Fall back to a manual install: download the asset, verify it, and replace the binary while Codewhale is not running"],"exampleFix":"# before\n$ codewhale update\nfailed to install new binary at C:\\Program Files\\codewhale\\codewhale.exe: Access is denied (os error 5)\n\n# after\n# close all codewhale.exe processes, then from an elevated shell\n$ codewhale update","handlingStrategy":"retry","validationCode":"// Before updating, confirm the install target is writable and not in use:\nuse std::fs::OpenOptions;\nlet probe = OpenOptions::new().write(true).open(target).is_ok();\n// On Windows also ensure no codewhale.exe processes remain:\nlet busy = std::process::Command::new(\"tasklist\")\n    .args([\"/FI\", \"IMAGENAME eq codewhale.exe\"]) \n    .output().map(|o| String::from_utf8_lossy(&o.stdout).contains(\"codewhale.exe\"))\n    .unwrap_or(false);\nif !probe || busy { /* defer the update */ }","typeGuard":null,"tryCatchPattern":"match install_binary(&tmp, target) {\n    Ok(()) => Ok(()),\n    Err(e) if e.to_string().contains(\"failed to install new binary\") => {\n        // updater already restored the backup; tell user to close processes,\n        // gain write access, then retry once\n        report_and_retry_after_user_action(e)\n    }\n    Err(e) => Err(e),\n}","preventionTips":["Close all Codewhale sessions before updating","Install the CLI in a user-writable directory rather than Program Files","Keep TMP on the same volume as the install target","Add AV exclusions for the binary path if locks recur"],"tags":["update","install","windows","file-lock","permissions"],"backgroundTag":"file-replace-permission-denied","analyzedSha":"0c42157ee52f9d55af2b506d71b46249910f77d3","analyzedAt":"2026-08-20T21:50:45.477Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}