Skip to content

Commit 8d747f5

Browse files
committed
downloading binary: if unlinking previous binary fails, try renaming it
1 parent 95f2500 commit 8d747f5

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

cli/release-staging/index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,30 @@ async function downloadBinary(version) {
240240
throw new Error(`Unsupported platform: ${process.platform} ${process.arch}`)
241241
}
242242

243-
const downloadUrl = `${process.env.NEXT_PUBLIC_CODEBUFF_APP_URL || 'https://codebuff.com'}/api/releases/download/${version}/${fileName}`
243+
const downloadUrl = `${
244+
process.env.NEXT_PUBLIC_CODEBUFF_APP_URL || 'https://codebuff.com'
245+
}/api/releases/download/${version}/${fileName}`
244246

245247
fs.mkdirSync(CONFIG.configDir, { recursive: true })
246248

247249
if (fs.existsSync(CONFIG.binaryPath)) {
248-
fs.unlinkSync(CONFIG.binaryPath)
250+
try {
251+
fs.unlinkSync(CONFIG.binaryPath)
252+
} catch (err) {
253+
// Fallback: try renaming the locked/undeletable binary
254+
const backupPath = CONFIG.binaryPath + `.old.${Date.now()}`
255+
256+
try {
257+
fs.renameSync(CONFIG.binaryPath, backupPath)
258+
} catch (renameErr) {
259+
// If we can't unlink OR rename, we can't safely proceed
260+
throw new Error(
261+
`Failed to replace existing binary. ` +
262+
`unlink error: ${err.code || err.message}, ` +
263+
`rename error: ${renameErr.code || renameErr.message}`,
264+
)
265+
}
266+
}
249267
}
250268

251269
term.write('Downloading...')

0 commit comments

Comments
 (0)