Skip to content

Commit f05252c

Browse files
authored
fix: prune leftover update artifacts from the bin dir before updating (#41)
The pre-1.3.2 updater extracted directly into the binary's directory and, when it failed on Windows, could strand a partial `update-<ts>.zip` archive there. A killed run of the current updater can likewise leave a `.rb-update-<ts>` temp dir or a half-staged `.new` binary. None are ever reused, so they just accumulate in the user's bin dir. Sweep these at the start of an update: stray `update-<ts>.{zip,tar.gz}` archives, `.rb-update-*` dirs, and `rb.new` / `rb.exe.new`. Best-effort and wrapped in try/catch so cleanup never blocks the update. `.bak` is left alone — it is the intentional one-version rollback.
1 parent 25ab866 commit f05252c

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

src/commands/update.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* `rb update` — Self-replace for standalone binary, print install instructions for dev mode.
33
* Detects install method via compile-time IS_STANDALONE define.
44
*/
5-
import { writeFileSync, mkdirSync, existsSync, chmodSync, renameSync, unlinkSync, rmSync } from "node:fs";
5+
import { writeFileSync, mkdirSync, existsSync, chmodSync, renameSync, unlinkSync, rmSync, readdirSync } from "node:fs";
66
import { join, dirname } from "node:path";
77
import { homedir } from "node:os";
88
import { spawnSync } from "node:child_process";
@@ -150,6 +150,26 @@ async function updateBinary(latest: string): Promise<void> {
150150
const tmpBinPath = `${binPath}.new`;
151151
const bakBinPath = `${binPath}.bak`;
152152
const archiveBase = ext === ".tar.gz" ? "rb" : "rb.exe";
153+
154+
// Sweep leftovers from interrupted updates before starting. The pre-1.3.2
155+
// updater extracted into binDir and, on failure, could strand a partial
156+
// `update-<ts>.zip`/`.tar.gz` archive or a half-staged `.new` binary; a killed
157+
// run of the current updater can strand a `.rb-update-<ts>` temp dir. None are
158+
// reused, so clear them so they don't pile up. Never touch `.bak` — that's the
159+
// intentional one-version rollback.
160+
try {
161+
for (const name of readdirSync(binDir)) {
162+
const stale =
163+
/^update-\d+\.(zip|tar\.gz)$/.test(name) ||
164+
name.startsWith(".rb-update-") ||
165+
name === "rb.new" ||
166+
name === "rb.exe.new";
167+
if (stale) rmSync(join(binDir, name), { recursive: true, force: true });
168+
}
169+
} catch {
170+
// Best-effort — never block an update on cleanup.
171+
}
172+
153173
const extractDir = join(binDir, `.rb-update-${Date.now()}`);
154174
mkdirSync(extractDir, { recursive: true });
155175

0 commit comments

Comments
 (0)