diff --git a/CHANGELOG.md b/CHANGELOG.md index 843e772..89e0695 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ This changelog is automatically generated by AI when releases are published. ## [Unreleased] +### Fixed + +- **Upgrading on Windows while the background service runs works, and a failed upgrade says so.** Windows does not replace a program that is running, and the background service runs `autter.exe`. An npm upgrade (`npm install -g @autter/cli@`) then failed with EPERM, kept the old version and still exited 0; `autter upgrade` ran the installer in the background and exited 0 before it knew the result. Now the npm package, `autter upgrade` and `install.ps1` stop the service first (as `autter bg shutdown` does), rename a binary that is still in use to `autter.exe.old` and put the new one in its place, and start the service again on the new binary if it was running. The `.old` file is deleted once nothing runs it, by the next install or when the service starts. `autter upgrade` on Windows now waits for the installer and exits with its result. + - If the binary still cannot be replaced (for example, another program holds it open), the install exits non-zero, says the file is in use and to run `autter bg shutdown` and try again, and leaves the previous version installed and working. + - On macOS and Linux nothing changes, except that the npm install now fails instead of passing when it cannot move the new binary into place. + ## [3.9.1] - 2026-11-13 ### Fixed diff --git a/install.ps1 b/install.ps1 index 708ec5a..198a616 100644 --- a/install.ps1 +++ b/install.ps1 @@ -40,8 +40,13 @@ $dryRun = ($env:AUTTER_INSTALL_DRY_RUN -eq '1') # no operator that chains them. $NpmFallbackHint = "This is usually temporary: wait a minute and run the installer again.`nIf it keeps failing on this network, install with npm instead (needs Node.js 18+):`n npm install -g @autter/cli`n autter onboard" +# Set when the background service was running and was stopped to replace +# its binary: it is started again at the end, on whatever binary is then in +# place (also when the install fails and the previous binary stays). +$serviceWasRunning = $false + function Start-DaemonIfRequested { - if ($env:AUTTER_RESTART_DAEMON_AFTER_INSTALL -ne '1') { + if ($env:AUTTER_RESTART_DAEMON_AFTER_INSTALL -ne '1' -and -not $serviceWasRunning) { return } # A dry run starts nothing, also when it stops on an error. @@ -137,80 +142,122 @@ function Stop-AutterBackgroundService { } } -function Get-AutterManagedProcesses { +function Wait-ForFileAvailable { param( - [Parameter(Mandatory = $true)][string]$InstallDir - ) - - $targetPaths = @( - (Normalize-PathString (Join-Path $InstallDir 'autter.exe')), - (Normalize-PathString (Join-Path $InstallDir 'git.exe')) + [Parameter(Mandatory = $true)][string]$Path, + [Parameter(Mandatory = $false)][int]$MaxWaitSeconds = 10 ) - $processes = @(Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object { - $_.ProcessId -ne $PID -and - $_.ExecutablePath -and - ($targetPaths -contains (Normalize-PathString $_.ExecutablePath)) - }) - - return $processes + $deadline = (Get-Date).AddSeconds($MaxWaitSeconds) + while ((Get-Date) -lt $deadline) { + if (Test-FileAvailable -Path $Path) { + return $true + } + Start-Sleep -Milliseconds 250 + } + return (Test-FileAvailable -Path $Path) } -function Stop-AutterManagedProcesses { +# Stop the background service before its binary is replaced, the way +# `autter bg shutdown` does, and wait for it to let go of the file. Returns +# whether a service was running, so it is started again afterwards. A file +# that stays held is not fatal: Install-ExecutableFile renames it aside. +function Stop-ServiceForReplace { param( - [Parameter(Mandatory = $true)][string]$InstallDir + [Parameter(Mandatory = $true)][string]$AutterExe ) - $processes = @(Get-AutterManagedProcesses -InstallDir $InstallDir) - if ($processes.Count -eq 0) { + if (-not (Test-Path -LiteralPath $AutterExe)) { return $false } + $wasRunning = Stop-AutterBackgroundService -AutterExe $AutterExe + if ($wasRunning) { + Write-Host 'Stopped the autter background service to replace its binary.' + if (-not (Wait-ForFileAvailable -Path $AutterExe -MaxWaitSeconds 15)) { + [void](Stop-AutterBackgroundService -AutterExe $AutterExe -Hard) + [void](Wait-ForFileAvailable -Path $AutterExe -MaxWaitSeconds 5) + } + } + return $wasRunning +} - $pids = @($processes | Sort-Object ProcessId -Unique | Select-Object -ExpandProperty ProcessId) - Write-Warning ("Stopping lingering autter processes: {0}" -f ($pids -join ', ')) +# Put $Source at $Destination. Windows does not replace or delete an .exe that +# a running process has open (the background service, a git command run +# through autter, `autter upgrade` itself), but it does rename one. So the +# file in place is renamed aside to .old (or ..old) and the +# new one moved in; if that fails, the old one is moved back. The aside file +# is deleted by Remove-LeftoverExecutables once nothing runs it. +# Returns @{ Error = $null or why it failed; Aside = $null or the aside path }. +function Install-ExecutableFile { + param( + [Parameter(Mandatory = $true)][string]$Source, + [Parameter(Mandatory = $true)][string]$Destination + ) - foreach ($managedPid in $pids) { + $aside = $null + if (Test-Path -LiteralPath $Destination) { + $aside = "$Destination.old" + if (Test-Path -LiteralPath $aside) { + try { + Remove-Item -LiteralPath $aside -Force -ErrorAction Stop + } catch { + $aside = "$Destination.$PID.old" + } + } try { - Stop-Process -Id $managedPid -Force -ErrorAction Stop - } catch { } + [IO.File]::Move($Destination, $aside) + } catch { + return [PSCustomObject]@{ Error = $_.Exception.Message; Aside = $null } + } } - - return $true + try { + [IO.File]::Move($Source, $Destination) + } catch { + $reason = $_.Exception.Message + if ($aside) { + try { + [IO.File]::Move($aside, $Destination) + } catch { + $reason += " The previous file could not be put back either: rename $aside to $Destination." + } + } + return [PSCustomObject]@{ Error = $reason; Aside = $null } + } + return [PSCustomObject]@{ Error = $null; Aside = $aside } } -function Wait-ForFileAvailable { +# Put the previous binary back after the new one failed its check. +function Restore-PreviousExecutable { param( - [Parameter(Mandatory = $true)][string]$Path, - [Parameter(Mandatory = $true)][string]$InstallDir, - [Parameter(Mandatory = $false)][int]$MaxWaitSeconds = 300, - [Parameter(Mandatory = $false)][int]$RetryIntervalSeconds = 5, - [Parameter(Mandatory = $false)][int]$ForceKillAfterSeconds = 20 + [Parameter(Mandatory = $false)][string]$Aside, + [Parameter(Mandatory = $true)][string]$Destination ) - $elapsed = 0 - $autterExe = Join-Path $InstallDir 'autter.exe' - - [void](Stop-AutterBackgroundService -AutterExe $autterExe) - - while ($elapsed -lt $MaxWaitSeconds) { - if (Test-FileAvailable -Path $Path) { - return $true - } + if (-not $Aside -or -not (Test-Path -LiteralPath $Aside)) { + return $false + } + try { + Remove-Item -LiteralPath $Destination -Force -ErrorAction SilentlyContinue + [IO.File]::Move($Aside, $Destination) + return $true + } catch { + return $false + } +} - if ($elapsed -ge $ForceKillAfterSeconds) { - [void](Stop-AutterBackgroundService -AutterExe $autterExe -Hard) - [void](Stop-AutterManagedProcesses -InstallDir $InstallDir) - } +# Delete binaries renamed aside by earlier installs that nothing runs any +# more. One still running stays until the next install (the background +# service also deletes them when it starts). +function Remove-LeftoverExecutables { + param( + [Parameter(Mandatory = $true)][string]$InstallDir + ) - if (-not (Test-FileAvailable -Path $Path)) { - if ($elapsed -eq 0) { - Write-Host "Waiting for file to be available: $Path" -ForegroundColor Yellow - } - Start-Sleep -Seconds $RetryIntervalSeconds - $elapsed += $RetryIntervalSeconds + foreach ($pattern in @('autter.exe.*old', 'git.exe.*old')) { + foreach ($leftover in @(Get-ChildItem -LiteralPath $InstallDir -Filter $pattern -File -ErrorAction SilentlyContinue)) { + try { Remove-Item -LiteralPath $leftover.FullName -Force -ErrorAction Stop } catch { } } } - return $false } function Verify-Checksum { @@ -511,7 +558,7 @@ if ($dryRun) { } if (Test-Path -LiteralPath $dryExe) { - [void]$drySteps.Add("replace $dryExe (the autter background service is stopped first, so the file is free)") + [void]$drySteps.Add("replace $dryExe (the autter background service is stopped first and started again afterwards; a file still in use is renamed aside to autter.exe.old)") } else { [void]$drySteps.Add("install it as $dryExe") } @@ -917,15 +964,15 @@ Verify-Checksum -File $tmpFile -BinaryName $downloadedBinaryName $finalExe = Join-Path $installDir 'autter.exe' -# Wait for autter.exe to be available if it exists and is in use -if (Test-Path -LiteralPath $finalExe) { - if (-not (Wait-ForFileAvailable -Path $finalExe -InstallDir $installDir -MaxWaitSeconds 300 -RetryIntervalSeconds 5)) { - Remove-Item -Force -ErrorAction SilentlyContinue $tmpFile - Write-ErrorAndExit "Timeout waiting for $finalExe to be available. Please close any running autter processes and try again." - } -} +# Stop the background service first; it is started again (on the new +# binary) by Start-DaemonIfRequested below. +$serviceWasRunning = Stop-ServiceForReplace -AutterExe $finalExe -Move-Item -Force -Path $tmpFile -Destination $finalExe +$swap = Install-ExecutableFile -Source $tmpFile -Destination $finalExe +if ($swap.Error) { + Remove-Item -Force -ErrorAction SilentlyContinue $tmpFile + Write-ErrorAndExit ("Could not replace {0}: the file is in use ({1})`nThe autter background service, a git command run through autter, or another program (such as an antivirus scan) still has it open, and Windows does not replace a running program.`nThe previous autter is still installed and works.`nFix: run 'autter bg shutdown', then run the installer again." -f $finalExe, $swap.Error) +} try { Unblock-File -Path $finalExe -ErrorAction SilentlyContinue } catch { } # This installer now owns the binary: drop the npm package's marker, so the # background service updates it again and `autter upgrade` stops pointing at npm. @@ -941,25 +988,29 @@ try { } catch { $versionError = $_.Exception.Message } -if ($versionError) { +if ($versionError -or $versionExitCode -ne 0 -or [string]::IsNullOrWhiteSpace($installedVersion)) { + $versionDetail = if ($versionError) { " $versionError" } else { "`n$installedVersion" } Remove-Item -Force -ErrorAction SilentlyContinue $finalExe - Write-ErrorAndExit "The autter binary could not run on this system: $versionError" -} -if ($versionExitCode -ne 0 -or [string]::IsNullOrWhiteSpace($installedVersion)) { - Remove-Item -Force -ErrorAction SilentlyContinue $finalExe - Write-ErrorAndExit "The autter binary could not run on this system:`n$installedVersion" + if (Restore-PreviousExecutable -Aside $swap.Aside -Destination $finalExe) { + $versionDetail += "`nThe previous autter is still installed." + } + Write-ErrorAndExit "The autter binary could not run on this system:$versionDetail" } Write-Host "Installed autter $installedVersion" # Refresh git.exe for existing wrapper users (it's a copy, not a symlink on Windows) $gitShim = Join-Path $installDir 'git.exe' if (Test-Path -LiteralPath $gitShim) { - if (-not (Wait-ForFileAvailable -Path $gitShim -InstallDir $installDir -MaxWaitSeconds 300 -RetryIntervalSeconds 5)) { - Write-ErrorAndExit "Timeout waiting for $gitShim to be available. Please close any running git processes and try again." + $shimTmp = Join-Path $installDir "git.tmp.$PID.exe" + Copy-Item -Force -Path $finalExe -Destination $shimTmp + $shimSwap = Install-ExecutableFile -Source $shimTmp -Destination $gitShim + if ($shimSwap.Error) { + Remove-Item -Force -ErrorAction SilentlyContinue $shimTmp + Write-ErrorAndExit ("autter.exe was updated, but {0} could not be replaced: the file is in use ({1})`nA git command run through autter is probably still running. The previous git.exe still works.`nFix: let running git commands finish, run 'autter bg shutdown', then run the installer again." -f $gitShim, $shimSwap.Error) } - Copy-Item -Force -Path $finalExe -Destination $gitShim try { Unblock-File -Path $gitShim -ErrorAction SilentlyContinue } catch { } } +Remove-LeftoverExecutables -InstallDir $installDir # Login user with install token if provided $needLogin = $false @@ -978,7 +1029,8 @@ if ($env:INSTALL_NONCE -and $env:API_BASE) { Write-Host 'IDE/agent hooks and git capture are configured during onboard (with consent)...' Write-Host " Run: $finalExe onboard" -# Best-effort restart only for daemon-initiated self-updates. +# Start the background service again if it was running before (or for the +# service's own self-update), now on the new binary. Best effort. Start-DaemonIfRequested $skipPathUpdate = $env:AUTTER_SKIP_PATH_UPDATE -eq '1' diff --git a/npm/bin/autter.js b/npm/bin/autter.js index feaa5d8..ede8081 100644 --- a/npm/bin/autter.js +++ b/npm/bin/autter.js @@ -27,7 +27,10 @@ async function main() { } catch (err) { if (binaryExists) { // The binary from before still runs; install this version next time. - console.error(`autter: could not install this package's version of the binary (${err.message}); running the one already installed`); + const [first, ...rest] = String(err.message).split('\n'); + console.error(`autter: could not install this package's version of the binary: ${first}`); + for (const line of rest) console.error(`autter: ${line}`); + console.error('autter: running the one already installed'); } else { console.error(`autter: could not install the native binary: ${err.message}`); console.error('autter: alternative installer: curl -fsSL https://api.autter.dev/install.sh | bash'); diff --git a/npm/install.js b/npm/install.js index f2d9656..a694513 100644 --- a/npm/install.js +++ b/npm/install.js @@ -325,10 +325,266 @@ function reportInstallPing(tag) { }).catch(() => {}); } +// --- putting the binary in place -------------------------------------------- +// +// Windows will not replace or delete an .exe that a running process has open +// (rename-over fails with EPERM), and the autter background service runs +// ~/.autter/bin/autter.exe. It will rename one, though. So on Windows the +// service is stopped first (`autter bg shutdown`, as a user would), and if the +// file is still held the old binary is renamed aside to autter.exe.old and the +// new one put in its place. The aside file is deleted once nothing runs it: +// right away, or by the next install. If the swap fails anyway the old binary +// stays where it was and the install fails, naming the cause and the fix. + +const SERVICE_STOP_TIMEOUT_MS = 15_000; +const SERVICE_START_TIMEOUT_MS = 90_000; +const RELEASE_WAIT_MS = 10_000; +const ASIDE_SUFFIX = '.old'; + +// The binary could not be put in place. The previous one is still installed. +class SwapError extends Error { + constructor(message, cause) { + super(message); + this.name = 'SwapError'; + this.code = 'AUTTER_SWAP_FAILED'; + this.inUse = isInUseError(cause); + this.cause = cause; + } +} + +// The errors Windows gives for a file another process has open. +function isInUseError(err) { + return Boolean(err) && ['EPERM', 'EACCES', 'EBUSY'].includes(err.code); +} + +function reinstallCommand(packageVersion = pkg.version) { + return packageVersion === DEV_VERSION + ? 'npm install -g @autter/cli' + : `npm install -g @autter/cli@${packageVersion}`; +} + +function swapErrorMessage(dest, err) { + const code = (err && err.code) || 'unknown error'; + if (isInUseError(err)) { + return [ + `could not replace ${dest}: the file is in use (${code}).`, + 'The autter background service or another autter command still has it open, and Windows does not replace a running program.', + 'The previous autter is still installed and works.', + `Fix: run \`autter bg shutdown\`, then run the install again: ${reinstallCommand()}`, + ].join('\n'); + } + return [ + `could not put the new autter binary at ${dest} (${(err && err.message) || code}).`, + 'The previous autter is still installed and works.', + ].join('\n'); +} + +// Leftovers of earlier swaps next to `dest`: autter.exe.old, autter.exe..old. +function leftoverBinaries(dest, fsImpl = fs) { + const dir = path.dirname(dest); + const base = path.basename(dest); + let names; + try { + names = fsImpl.readdirSync(dir); + } catch { + return []; + } + return names + .filter((name) => name === base + ASIDE_SUFFIX || (name.startsWith(`${base}.`) && name.endsWith(ASIDE_SUFFIX))) + .map((name) => path.join(dir, name)); +} + +// Delete leftovers no process runs any more; the ones still running stay. +function removeLeftoverBinaries(dest, fsImpl = fs) { + for (const file of leftoverBinaries(dest, fsImpl)) { + try { + fsImpl.rmSync(file, { force: true }); + } catch { + // still running: removed by a later install + } + } +} + +function asidePath(dest, pid, fsImpl = fs) { + const plain = dest + ASIDE_SUFFIX; + if (!fsImpl.existsSync(plain)) return plain; + return `${dest}.${pid}${ASIDE_SUFFIX}`; +} + +// `autter bg shutdown` on the installed binary. Exits 0 only when a service +// was running and was asked to stop, so the result says whether to start it +// again afterwards. +function stopService(bin, execFile = execFileSync) { + try { + execFile(bin, ['bg', 'shutdown'], { stdio: 'ignore', timeout: SERVICE_STOP_TIMEOUT_MS, windowsHide: true }); + return true; + } catch { + return false; + } +} + +// `autter bg start` on the new binary. Returns null, or why it failed. +function startService(bin, execFile = execFileSync) { + try { + execFile(bin, ['bg', 'start'], { stdio: 'ignore', timeout: SERVICE_START_TIMEOUT_MS, windowsHide: true }); + return null; + } catch (err) { + return err.message || String(err); + } +} + +function sleepMs(ms) { + Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); +} + +// Whether no process holds `file` open for running (opening it for writing +// then fails with EBUSY on Windows). +function fileReleased(file, fsImpl = fs) { + try { + fsImpl.closeSync(fsImpl.openSync(file, 'r+')); + return true; + } catch (err) { + return !isInUseError(err); + } +} + +// Wait for the stopped service to let go of `file`. Not fatal: renaming +// aside works on a file that is still running. +function waitForRelease(file, { fsImpl = fs, timeoutMs = RELEASE_WAIT_MS, sleep = sleepMs, now = Date.now } = {}) { + const deadline = now() + timeoutMs; + for (;;) { + if (fileReleased(file, fsImpl)) return true; + if (now() >= deadline) return false; + sleep(200); + } +} + +// Move the downloaded file `tmp` to `dest`. Returns { serviceWasRunning, +// aside }: `aside` is where the previous binary went (Windows, file in use), +// for finishInstall to delete or restore. Throws SwapError with the previous +// binary in place and `tmp` removed. +function replaceBinary(tmp, dest, deps = {}) { + const { + fs: fsImpl = fs, + platform = process.platform, + stopService: stop = stopService, + startService: start = startService, + waitForRelease: wait = (file) => waitForRelease(file, { fsImpl }), + pid = process.pid, + log = console.log, + } = deps; + const discardTmp = () => { + try { + fsImpl.rmSync(tmp, { force: true }); + } catch { + // best effort + } + }; + + if (platform !== 'win32' || !fsImpl.existsSync(dest)) { + try { + fsImpl.renameSync(tmp, dest); + } catch (err) { + discardTmp(); + throw new SwapError(swapErrorMessage(dest, err), err); + } + return { serviceWasRunning: false, aside: null }; + } + + removeLeftoverBinaries(dest, fsImpl); + const serviceWasRunning = stop(dest); + if (serviceWasRunning) { + log('autter: stopped the background service to replace the binary'); + wait(dest); + } + const restartOld = () => { + if (serviceWasRunning) start(dest); + }; + + try { + fsImpl.renameSync(tmp, dest); + return { serviceWasRunning, aside: null }; + } catch (err) { + if (!isInUseError(err)) { + discardTmp(); + restartOld(); + throw new SwapError(swapErrorMessage(dest, err), err); + } + } + + // Still held: rename the running file aside, which Windows allows. + const aside = asidePath(dest, pid, fsImpl); + try { + fsImpl.renameSync(dest, aside); + } catch (err) { + discardTmp(); + restartOld(); + throw new SwapError(swapErrorMessage(dest, err), err); + } + try { + fsImpl.renameSync(tmp, dest); + } catch (err) { + try { + fsImpl.renameSync(aside, dest); + } catch (restoreErr) { + discardTmp(); + throw new SwapError( + `${swapErrorMessage(dest, err)}\nThe previous binary could not be put back either (${restoreErr.message}): rename ${aside} to ${dest}.`, + err, + ); + } + discardTmp(); + restartOld(); + throw new SwapError(swapErrorMessage(dest, err), err); + } + return { serviceWasRunning, aside }; +} + +// After the new binary is in place: check it runs (putting the previous one +// back if it does not), delete the aside file, and start the background +// service again on the new binary if it was running before. +function finishInstall(dest, swap, deps = {}) { + const { + fs: fsImpl = fs, + verify = verifyBinaryRuns, + startService: start = startService, + warn = console.warn, + } = deps; + try { + verify(dest); + } catch (err) { + if (swap.aside) { + try { + fsImpl.rmSync(dest, { force: true }); + fsImpl.renameSync(swap.aside, dest); + err.message += '\nThe previous autter is still installed.'; + } catch { + // the error below still says what failed + } + } + if (swap.serviceWasRunning && fsImpl.existsSync(dest)) start(dest); + throw err; + } + if (swap.aside) { + try { + fsImpl.rmSync(swap.aside, { force: true }); + } catch { + // the old process still runs it: the next install removes it + } + } + if (swap.serviceWasRunning) { + const failure = start(dest); + if (failure) { + warn(`autter: could not start the background service again (${failure}); run 'autter bg restart'`); + } + } +} + // Make sure the native binary exists at ~/.autter/bin, downloading it when // missing or (for a pinned package version) when a different version is -// present. Returns { bin, downloaded }. -async function ensureBinary() { +// present. Returns { bin, downloaded }. `deps` replaces the file system and +// process calls in tests. +async function ensureBinary(deps = {}) { const asset = assetName(); // throws early on unsupported platforms const dest = binaryDest(); let tag = releaseTag(); @@ -359,20 +615,17 @@ async function ensureBinary() { fs.mkdirSync(installDir(), { recursive: true }); const tmp = path.join(installDir(), `autter.tmp.${process.pid}`); fs.writeFileSync(tmp, buf, { mode: 0o755 }); - try { - fs.renameSync(tmp, dest); - } catch (err) { - // Windows cannot rename over a running binary; retry after removing it. - try { - fs.rmSync(dest, { force: true }); - fs.renameSync(tmp, dest); - } catch { - fs.rmSync(tmp, { force: true }); - throw err; - } - } + installDownloadedBinary(tmp, dest, deps); + + await reportInstallPing(tag); + return { bin: dest, downloaded: true }; +} - if (process.platform === 'darwin') { +// Put the verified download `tmp` at `dest` and check that it runs. +function installDownloadedBinary(tmp, dest, deps = {}) { + const swap = replaceBinary(tmp, dest, deps); + + if ((deps.platform || process.platform) === 'darwin') { try { execFileSync('xattr', ['-d', 'com.apple.quarantine', dest], { stdio: 'ignore' }); } catch { @@ -380,15 +633,13 @@ async function ensureBinary() { } } - verifyBinaryRuns(dest); - - await reportInstallPing(tag); - return { bin: dest, downloaded: true }; + finishInstall(dest, swap, deps); + return swap; } // postinstall entry point. Ordinary transient download/platform errors defer to // bin/autter.js, but an integrity failure must fail installation closed. -async function main() { +async function main({ ensure = ensureBinary } = {}) { if (process.env.AUTTER_NPM_SKIP_DOWNLOAD === '1') { console.log('autter: AUTTER_NPM_SKIP_DOWNLOAD=1, skipping binary download'); return; @@ -404,11 +655,18 @@ async function main() { let result; try { - result = await ensureBinary(); + result = await ensure(); } catch (err) { if (/checksums\.txt|checksum mismatch|no checksum entry/.test(err.message)) { throw err; } + // The binary was downloaded but could not be put in place (on Windows, + // usually because the background service still runs it). Deferring to + // the launcher would leave the old version installed while npm reports + // success, so the install fails instead; the old binary still works. + if (err instanceof SwapError) { + throw err; + } console.warn(`autter: could not install the native binary now (${err.message})`); console.warn('autter: it will be downloaded on first run instead'); return; @@ -429,23 +687,34 @@ async function main() { module.exports = { DEV_VERSION, + SwapError, assetName, binaryDest, downloadUrl, ensureBinary, + finishInstall, + installDownloadedBinary, installMarkerPath, + leftoverBinaries, launcherNeedsInstall, readInstallMarker, + main, releaseTag, + removeLeftoverBinaries, + replaceBinary, resolveLatestTag, spawnFailureHelp, + startService, + stopService, verifyChecksum, writeInstallMarker, }; if (require.main === module) { main().catch((err) => { - console.error(`autter: postinstall failed (${err.message})`); + const [first, ...rest] = String(err.message).split('\n'); + console.error(`autter: postinstall failed: ${first}`); + for (const line of rest) console.error(`autter: ${line}`); process.exitCode = 1; }); } diff --git a/npm/install.test.js b/npm/install.test.js index 916307b..2953c18 100644 --- a/npm/install.test.js +++ b/npm/install.test.js @@ -9,13 +9,19 @@ const path = require('node:path'); const pkg = require('./package.json'); const { DEV_VERSION, + SwapError, assetName, binaryDest, ensureBinary, + finishInstall, installMarkerPath, launcherNeedsInstall, + leftoverBinaries, + main, readInstallMarker, releaseTag, + removeLeftoverBinaries, + replaceBinary, resolveLatestTag, spawnFailureHelp, verifyChecksum, @@ -276,3 +282,279 @@ test( assert.equal(readInstallMarker(home), null); }, ); + +// --- replacing a binary that may be running --------------------------------- +// +// A stand-in for the Windows file system: a file in `running` is the image of +// a live process, so it can be renamed but not replaced or deleted (EPERM); a +// file in `locked` is open without delete sharing (an antivirus scan, another +// installer), so it cannot even be renamed (EBUSY). +function windowsFs(files, { running = [], locked = [], failRename = () => null } = {}) { + const live = new Set(running); + const held = new Set(locked); + const calls = []; + const fail = (code, op, p) => Object.assign(new Error(`${code}: operation not permitted, ${op} '${p}'`), { code }); + return { + files, + calls, + live, + existsSync: (p) => files.has(p), + readdirSync: (dir) => [...files.keys()].filter((p) => path.dirname(p) === dir).map((p) => path.basename(p)), + renameSync(from, to) { + calls.push(`rename ${path.basename(from)} -> ${path.basename(to)}`); + const injected = failRename(from, to); + if (injected) throw injected; + if (!files.has(from)) throw fail('ENOENT', 'rename', from); + if (held.has(from)) throw fail('EBUSY', 'rename', from); + if (files.has(to) && (live.has(to) || held.has(to))) throw fail('EPERM', 'rename', to); + files.set(to, files.get(from)); + files.delete(from); + if (live.delete(from)) live.add(to); + }, + rmSync(p) { + calls.push(`rm ${path.basename(p)}`); + if (live.has(p) || held.has(p)) throw fail('EPERM', 'unlink', p); + files.delete(p); + }, + }; +} + +const DIR = path.join(path.sep, 'home', 'dev', '.autter', 'bin'); +const DEST = path.join(DIR, 'autter.exe'); +const TMP = path.join(DIR, 'autter.tmp.42'); + +// The process calls replaceBinary makes, recorded in order. +function serviceDeps(fsImpl, { running }) { + const events = []; + return { + events, + deps: { + fs: fsImpl, + platform: 'win32', + pid: 42, + log: () => {}, + warn: (msg) => events.push(`warn ${msg}`), + stopService: (bin) => { + events.push(`stop ${path.basename(bin)}`); + return running; + }, + startService: (bin) => { + events.push(`start ${path.basename(bin)} (${fsImpl.files.get(bin)})`); + return null; + }, + waitForRelease: () => { + events.push('wait'); + return false; + }, + verify: () => {}, + }, + }; +} + +test('Windows: a binary the background service still runs is renamed aside, and the service restarts on the new one', () => { + const fsImpl = windowsFs(new Map([[DEST, 'old'], [TMP, 'new']]), { running: [DEST] }); + const { events, deps } = serviceDeps(fsImpl, { running: true }); + + const swap = replaceBinary(TMP, DEST, deps); + assert.deepEqual(swap, { serviceWasRunning: true, aside: `${DEST}.old` }); + assert.equal(fsImpl.files.get(DEST), 'new'); + assert.equal(fsImpl.files.get(`${DEST}.old`), 'old'); + assert.deepEqual(fsImpl.calls, [ + 'rename autter.tmp.42 -> autter.exe', // EPERM: still running + 'rename autter.exe -> autter.exe.old', + 'rename autter.tmp.42 -> autter.exe', + ]); + + finishInstall(DEST, swap, deps); + // Stopped before the swap, started after it, on the new binary. + assert.deepEqual(events, ['stop autter.exe', 'wait', 'start autter.exe (new)']); + // The old process still runs the aside file, so it stays for now ... + assert.ok(fsImpl.files.has(`${DEST}.old`)); + + // ... and the next install deletes it once that process is gone. + fsImpl.live.clear(); + removeLeftoverBinaries(DEST, fsImpl); + assert.equal(fsImpl.files.has(`${DEST}.old`), false); +}); + +test('Windows: a stopped service lets go of the file, and nothing is left behind', () => { + const fsImpl = windowsFs(new Map([[DEST, 'old'], [TMP, 'new']])); + const { events, deps } = serviceDeps(fsImpl, { running: true }); + const swap = replaceBinary(TMP, DEST, deps); + assert.deepEqual(swap, { serviceWasRunning: true, aside: null }); + finishInstall(DEST, swap, deps); + assert.deepEqual([...fsImpl.files.keys()], [DEST]); + assert.equal(fsImpl.files.get(DEST), 'new'); + assert.deepEqual(events, ['stop autter.exe', 'wait', 'start autter.exe (new)']); +}); + +test('Windows: a service that was not running is not started', () => { + const fsImpl = windowsFs(new Map([[DEST, 'old'], [TMP, 'new']])); + const { events, deps } = serviceDeps(fsImpl, { running: false }); + finishInstall(DEST, replaceBinary(TMP, DEST, deps), deps); + assert.equal(fsImpl.files.get(DEST), 'new'); + assert.deepEqual(events, ['stop autter.exe']); +}); + +test('Windows: a file in use that cannot be moved fails loudly and leaves the old binary working', () => { + const fsImpl = windowsFs(new Map([[DEST, 'old'], [TMP, 'new']]), { locked: [DEST] }); + const { events, deps } = serviceDeps(fsImpl, { running: true }); + + assert.throws( + () => replaceBinary(TMP, DEST, deps), + (err) => { + assert.ok(err instanceof SwapError); + assert.equal(err.code, 'AUTTER_SWAP_FAILED'); + assert.equal(err.inUse, true); + assert.match(err.message, /could not replace .*autter\.exe: the file is in use \(EBUSY\)/); + assert.match(err.message, /previous autter is still installed and works/); + assert.match(err.message, /Fix: run `autter bg shutdown`, then run the install again: npm install -g @autter\/cli/); + return true; + }, + ); + // The old binary is where it was, the download is gone, and the service + // that was stopped runs again on the old binary. + assert.deepEqual([...fsImpl.files.entries()], [[DEST, 'old']]); + assert.deepEqual(events, ['stop autter.exe', 'wait', 'start autter.exe (old)']); +}); + +test('Windows: when the new file cannot be moved in after the old one went aside, the old one is put back', () => { + let attempts = 0; + const failRename = (from, to) => { + if (from === TMP && to === DEST && ++attempts === 2) { + return Object.assign(new Error('EIO: i/o error'), { code: 'EIO' }); + } + return null; + }; + const fsImpl = windowsFs(new Map([[DEST, 'old'], [TMP, 'new']]), { running: [DEST], failRename }); + const { events, deps } = serviceDeps(fsImpl, { running: true }); + assert.throws(() => replaceBinary(TMP, DEST, deps), SwapError); + assert.deepEqual([...fsImpl.files.entries()], [[DEST, 'old']]); + assert.deepEqual(events, ['stop autter.exe', 'wait', 'start autter.exe (old)']); +}); + +test('Windows: a new binary that does not run is replaced by the previous one', () => { + const fsImpl = windowsFs(new Map([[DEST, 'old'], [TMP, 'new']]), { running: [DEST] }); + const { events, deps } = serviceDeps(fsImpl, { running: true }); + const swap = replaceBinary(TMP, DEST, deps); + fsImpl.live.clear(); // the old service process has exited + deps.verify = () => { + throw new Error('The autter binary could not run on this system'); + }; + assert.throws(() => finishInstall(DEST, swap, deps), /previous autter is still installed/); + assert.deepEqual([...fsImpl.files.entries()], [[DEST, 'old']]); + assert.deepEqual(events, ['stop autter.exe', 'wait', 'start autter.exe (old)']); +}); + +test('leftovers of earlier swaps are found and removed; other files stay', () => { + const files = new Map([ + [DEST, 'current'], + [`${DEST}.old`, 'x'], + [`${DEST}.1234.old`, 'x'], + [path.join(DIR, 'git.exe'), 'shim'], + [path.join(DIR, 'autter.exe.bak'), 'kept'], + ]); + const fsImpl = windowsFs(files, { running: [`${DEST}.1234.old`] }); + assert.deepEqual(leftoverBinaries(DEST, fsImpl), [`${DEST}.old`, `${DEST}.1234.old`]); + removeLeftoverBinaries(DEST, fsImpl); + // The one a process still runs stays until the next install. + assert.deepEqual([...fsImpl.files.keys()], [DEST, `${DEST}.1234.old`, path.join(DIR, 'git.exe'), path.join(DIR, 'autter.exe.bak')]); +}); + +test('other platforms: a failed rename is reported, and the background service is not touched', () => { + const failRename = () => Object.assign(new Error('EACCES: permission denied'), { code: 'EACCES' }); + const fsImpl = windowsFs(new Map([[DEST, 'old'], [TMP, 'new']]), { failRename }); + const { events, deps } = serviceDeps(fsImpl, { running: true }); + deps.platform = 'linux'; + assert.throws(() => replaceBinary(TMP, DEST, deps), SwapError); + assert.deepEqual([...fsImpl.files.entries()], [[DEST, 'old']]); + assert.deepEqual(events, []); + + // A rename that works is one rename, as before. + const ok = windowsFs(new Map([[DEST, 'old'], [TMP, 'new']]), { running: [DEST] }); + const okDeps = serviceDeps(ok, { running: true }); + okDeps.deps.platform = 'darwin'; + ok.live.clear(); // a running binary is replaced by rename on unix + assert.deepEqual(replaceBinary(TMP, DEST, okDeps.deps), { serviceWasRunning: false, aside: null }); + assert.deepEqual(ok.calls, ['rename autter.tmp.42 -> autter.exe']); + assert.deepEqual(okDeps.events, []); +}); + +test('the postinstall fails when the binary cannot be replaced, and still defers other errors to the launcher', async (t) => { + withEnv(t, { AUTTER_NPM_SKIP_DOWNLOAD: undefined }); + const quiet = (name) => { + const original = console[name]; + console[name] = () => {}; + t.after(() => { + console[name] = original; + }); + }; + quiet('log'); + quiet('warn'); + const inUse = new SwapError('could not replace autter.exe: the file is in use (EPERM).', { code: 'EPERM' }); + await assert.rejects(main({ ensure: async () => { throw inUse; } }), (err) => err === inUse); + // A network failure still lets npm finish; the launcher downloads later. + await main({ ensure: async () => { throw new Error('download failed (HTTP 502)'); } }); +}); + +test( + 'an npm upgrade over a running binary swaps it on the real file system and restarts the service', + { skip: process.platform === 'win32' ? 'the stand-in binary is a shell script' : false }, + async (t) => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), 'autter-npm-swap-')); + withEnv(t, { HOME: home, AUTTER_RELEASE_TAG: 'v9.9.9', AUTTER_NO_INSTALL_PING: '1' }); + const realFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = realFetch; + fs.rmSync(home, { recursive: true, force: true }); + }); + const dest = binaryDest(); + assert.ok(dest.startsWith(home + path.sep), `refusing to install into ${dest}`); + fs.mkdirSync(path.dirname(dest), { recursive: true }); + fs.writeFileSync(dest, '#!/bin/sh\necho 1.0.0\n', { mode: 0o755 }); + + const asset = assetName(); + const binary = Buffer.from('#!/bin/sh\necho 9.9.9\n'); + const sha256 = crypto.createHash('sha256').update(binary).digest('hex'); + globalThis.fetch = async (url) => { + switch (String(url)) { + case `${RELEASES}/download/v9.9.9/${asset}`: + return new Response(binary); + case `${RELEASES}/download/v9.9.9/checksums.txt`: + return new Response(`${sha256} ${asset}\n`); + default: + throw new Error(`unexpected request: ${url}`); + } + }; + + // Windows' answer to renaming over a running binary, once. + let refused = false; + const fsImpl = { + ...fs, + renameSync(from, to) { + if (to === dest && !refused) { + refused = true; + throw Object.assign(new Error(`EPERM: operation not permitted, rename '${from}' -> '${to}'`), { code: 'EPERM' }); + } + return fs.renameSync(from, to); + }, + }; + const started = []; + const result = await ensureBinary({ + fs: fsImpl, + platform: 'win32', + log: () => {}, + stopService: () => true, + waitForRelease: () => false, + startService: (bin) => { + started.push([bin, fs.readFileSync(bin, 'utf8')]); + return null; + }, + }); + assert.deepEqual(result, { bin: dest, downloaded: true }); + assert.deepEqual(fs.readFileSync(dest), binary); + assert.deepEqual(started, [[dest, binary.toString()]]); + // Nothing runs the aside file here, so it is gone already. + assert.deepEqual(fs.readdirSync(path.dirname(dest)), [path.basename(dest)]); + }, +); diff --git a/scripts/windows-e2e.ps1 b/scripts/windows-e2e.ps1 index 072c3ec..df68046 100644 --- a/scripts/windows-e2e.ps1 +++ b/scripts/windows-e2e.ps1 @@ -17,6 +17,11 @@ # doctor doctor --json and doctor --report # lifecycle bg shutdown, start, restart; then the service process killed # with Stop-Process -Force and started again over its stale lock +# upgrade the npm bootstrapper replaces autter.exe while the service runs +# it (stopped, swapped, started again on the new binary); a +# running exe is renamed aside; a file held open fails non-zero +# with the fix and is left in place; install.ps1 over the +# running service # vscode Code.exe ahead of bin\code.cmd on PATH: doctor and # install-hooks --dry-run run code.cmd and never Code.exe; a # Code.exe with no wrapper is asked --version once and no more @@ -443,6 +448,121 @@ try { } } catch { Fail ("section error: " + $_.Exception.Message) } +# --- upgrade ---------------------------------------------------------------- + +Begin-Section 'upgrade' +try { + $node = Get-Command node -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1 + if (-not $node) { Fail 'node is not on PATH' } else { + $installJs = Join-Path $RepoRoot 'npm\install.js' + $swapJs = Join-Path $script:Root 'npm-swap.js' + $swapSource = @' +const [mode, installJs, a, b] = process.argv.slice(2); +const npm = require(installJs); +try { + if (mode === 'swap') { + console.log(JSON.stringify(npm.installDownloadedBinary(a, b))); + } else { + npm.removeLeftoverBinaries(a); + } +} catch (err) { + console.error(err.message); + process.exit(1); +} +'@ + [IO.File]::WriteAllText($swapJs, $swapSource, (New-Object Text.UTF8Encoding($false))) + + # 1. What `npm install -g @autter/cli@` does when the service + # runs the installed binary. + $r = Invoke-Autter -Arguments @('bg', 'start') -TimeoutSec 90 + $pidBefore = Get-ServicePid + Check ($null -ne $pidBefore) 'the service runs before the npm swap' + $tmp = Join-Path $BinDir 'autter.tmp.e2e' + Copy-Item -LiteralPath $Autter -Destination $tmp -Force + $r = Invoke-Exe -File $node.Source -Arguments @($swapJs, 'swap', $installJs, $tmp, $script:AutterExe) -TimeoutSec 240 -Label 'npm swap over the running service' + Check ($r.ExitCode -eq 0) 'the npm swap over the running service exits 0' (Show-Output $r) + $swap = $null + try { $swap = ConvertFrom-Json $r.Stdout.Trim().Split("`n")[-1] } catch { } + Check ($null -ne $swap -and $swap.serviceWasRunning) 'the npm swap stopped the running service first' (Show-Output $r) + if ($null -ne $swap) { Write-Host (" aside file used: {0}" -f $swap.aside) } + Check (-not (Test-Path -LiteralPath $tmp)) 'the downloaded file was moved into place' + $restarted = Wait-Until -TimeoutSec 60 -Condition { + $p = Get-ServicePid + return ($null -ne $p -and $p -ne $pidBefore) + } + Check $restarted 'the service runs again, as a new process, after the npm swap' + $r = Invoke-Autter -Arguments @('--version') + Check ($r.ExitCode -eq 0) 'autter.exe runs after the npm swap' (Show-Output $r) + + # 2. A running exe is renamed aside and replaced. A copy of PING.EXE + # stands in for a process nobody can stop. + $swapDir = Join-Path $script:Root 'swap' + New-Item -ItemType Directory -Force -Path $swapDir | Out-Null + $runningExe = Join-Path $swapDir 'autter.exe' + Copy-Item -LiteralPath (Join-Path $env:SystemRoot 'System32\PING.EXE') -Destination $runningExe -Force + $pinger = Start-Process -FilePath $runningExe -ArgumentList @('-n', '120', '127.0.0.1') -PassThru -WindowStyle Hidden + try { + $tmp = Join-Path $swapDir 'autter.tmp.e2e' + Copy-Item -LiteralPath $Autter -Destination $tmp -Force + $r = Invoke-Exe -File $node.Source -Arguments @($swapJs, 'swap', $installJs, $tmp, $runningExe) -TimeoutSec 120 -Label 'npm swap over a running exe' + Check ($r.ExitCode -eq 0) 'the npm swap over a running exe exits 0' (Show-Output $r) + $swap = $null + try { $swap = ConvertFrom-Json $r.Stdout.Trim().Split("`n")[-1] } catch { } + Check ($null -ne $swap -and $swap.aside) 'the running exe was renamed aside' (Show-Output $r) + $v = Invoke-Exe -File $runningExe -Arguments @('--version') -Label 'swapped autter.exe --version' + Check ($v.ExitCode -eq 0 -and $v.Stdout -match '\d+\.\d+\.\d+') 'the new autter.exe is in place and runs' (Show-Output $v) + Check (-not $pinger.HasExited) 'the process running the old file was not stopped' + } finally { + try { Stop-Process -Id $pinger.Id -Force -ErrorAction Stop } catch { } + try { $pinger.WaitForExit(10000) | Out-Null } catch { } + } + $r = Invoke-Exe -File $node.Source -Arguments @($swapJs, 'cleanup', $installJs, $runningExe) -Label 'npm leftover cleanup' + $left = @(Get-ChildItem -LiteralPath $swapDir -Filter 'autter.exe*.old' -File -ErrorAction SilentlyContinue) + Check ($left.Count -eq 0) 'the aside file is deleted once nothing runs it' (($left | ForEach-Object { $_.Name }) -join ', ') + + # 3. A file held open without delete sharing cannot even be renamed: + # the swap fails non-zero, says why and what to do, and leaves the + # old file in place. + $before = (Get-FileHash -LiteralPath $runningExe -Algorithm SHA256).Hash + $tmp = Join-Path $swapDir 'autter.tmp.e2e' + [IO.File]::WriteAllBytes($tmp, [byte[]](1, 2, 3)) + $held = [IO.File]::Open($runningExe, 'Open', 'Read', 'Read') + try { + $r = Invoke-Exe -File $node.Source -Arguments @($swapJs, 'swap', $installJs, $tmp, $runningExe) -TimeoutSec 120 -Label 'npm swap over a held file' + } finally { $held.Close() } + Check ($r.ExitCode -ne 0) 'the npm swap over a held file exits non-zero' (Show-Output $r) + Check ($r.Stderr -match 'in use' -and $r.Stderr -match 'autter bg shutdown') 'it names the file in use and the fix' $r.Stderr + Check ((Get-FileHash -LiteralPath $runningExe -Algorithm SHA256).Hash -eq $before) 'the previous file is left in place' + Check (-not (Test-Path -LiteralPath $tmp)) 'the download is removed' + + # 4. install.ps1 over the running service, from the local build. + $r = Invoke-Autter -Arguments @('bg', 'start') -TimeoutSec 90 + $pidBefore = Get-ServicePid + Check ($null -ne $pidBefore) 'the service runs before install.ps1' + $env:AUTTER_LOCAL_BINARY = $Autter + $env:AUTTER_SKIP_PATH_UPDATE = '1' + try { + $psExe = Join-Path $PSHOME 'powershell.exe' + $r = Invoke-Exe -File $psExe -Arguments @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', (Join-Path $RepoRoot 'install.ps1')) -TimeoutSec 300 -Label 'install.ps1 over the running service' + } finally { + Remove-Item Env:\AUTTER_LOCAL_BINARY -ErrorAction SilentlyContinue + Remove-Item Env:\AUTTER_SKIP_PATH_UPDATE -ErrorAction SilentlyContinue + } + Check ($r.ExitCode -eq 0) 'install.ps1 over the running service exits 0' (Show-Output $r) + Check ($r.Stdout.Contains($script:AutterExe)) 'install.ps1 installed into the test profile' (Show-Output $r) + Check ($r.Stdout -match 'Stopped the autter background service') 'install.ps1 stopped the service first' (Show-Output $r) + $restarted = Wait-Until -TimeoutSec 60 -Condition { + $p = Get-ServicePid + return ($null -ne $p -and $p -ne $pidBefore) + } + Check $restarted 'install.ps1 started the service again as a new process' + $r = Invoke-Autter -Arguments @('--version') + Check ($r.ExitCode -eq 0) 'autter.exe runs after install.ps1' (Show-Output $r) + } +} catch { Fail ("section error: " + $_.Exception.Message) } + +$env:Path = $script:BasePath + # --- vscode ----------------------------------------------------------------- Begin-Section 'vscode' diff --git a/src/commands/binary_swap.rs b/src/commands/binary_swap.rs new file mode 100644 index 0000000..9db728c --- /dev/null +++ b/src/commands/binary_swap.rs @@ -0,0 +1,641 @@ +//! Replacing the installed autter binary while a process may be running it. +//! +//! Windows does not replace or delete an `.exe` that a running process has +//! open: the background service runs `~/.autter/bin/autter.exe`, and so does +//! `autter upgrade` itself. It does let such a file be renamed. So before the +//! installer runs, the background service is stopped (as `autter bg shutdown` +//! does) and the installed binary is renamed aside to `autter.exe.old`; the +//! installer then writes a fresh file. If the installer leaves no binary +//! behind, the old one is renamed back, so a failed upgrade never leaves a +//! half-replaced install. The aside file is deleted as soon as nothing runs +//! it: by the next upgrade, or when the background service next starts. The +//! service is started again, on whatever binary is in place, if it was +//! running before. +//! +//! The file system and service calls go through [`SwapHost`], so the logic is +//! tested on every platform; only Windows uses it. + +#![cfg_attr(not(windows), allow(dead_code))] + +use std::io; +use std::path::{Path, PathBuf}; + +/// Suffix of a binary renamed aside: `autter.exe.old`, or +/// `autter.exe..old` when an older one is still running. +pub(crate) const ASIDE_SUFFIX: &str = ".old"; + +/// The binaries the installers put in `~/.autter/bin`. +pub(crate) const INSTALLED_BINARY_NAMES: &[&str] = &["autter.exe", "git.exe"]; + +/// The file system and background-service calls a swap makes. +pub(crate) trait SwapHost { + fn exists(&self, path: &Path) -> bool; + fn rename(&self, from: &Path, to: &Path) -> io::Result<()>; + fn remove(&self, path: &Path) -> io::Result<()>; + fn list_dir(&self, dir: &Path) -> Vec; + /// Whether the background service is running or starting. + fn service_running(&self) -> bool; + /// Stop the background service and wait for it to exit. + fn stop_service(&self) -> Result<(), String>; + /// Start the background service from `exe`. + fn start_service(&self, exe: &Path) -> Result<(), String>; +} + +/// Leftovers of earlier swaps in `dir`: `.old` and `..old` +/// for each of `names`. +pub(crate) fn leftover_binaries(host: &impl SwapHost, dir: &Path, names: &[&str]) -> Vec { + let mut found: Vec = host + .list_dir(dir) + .into_iter() + .filter(|path| { + let Some(file) = path.file_name().and_then(|f| f.to_str()) else { + return false; + }; + names.iter().any(|name| { + file.len() > name.len() + 1 + && file + .get(..name.len() + 1) + .is_some_and(|prefix| prefix.eq_ignore_ascii_case(&format!("{name}."))) + && file.to_ascii_lowercase().ends_with(ASIDE_SUFFIX) + }) + }) + .collect(); + found.sort(); + found +} + +/// Delete the leftovers no process runs any more. One still running stays +/// until a later call. +pub(crate) fn remove_leftover_binaries(host: &impl SwapHost, dir: &Path, names: &[&str]) { + for path in leftover_binaries(host, dir, names) { + let _ = host.remove(&path); + } +} + +fn aside_path(host: &impl SwapHost, exe: &Path, pid: u32) -> PathBuf { + let plain = PathBuf::from(format!("{}{ASIDE_SUFFIX}", exe.display())); + if !host.exists(&plain) { + return plain; + } + PathBuf::from(format!("{}.{pid}{ASIDE_SUFFIX}", exe.display())) +} + +/// Whether `err` is Windows refusing a file another process has open. +pub(crate) fn is_in_use(err: &io::Error) -> bool { + if err.kind() == io::ErrorKind::PermissionDenied { + return true; + } + // ERROR_SHARING_VIOLATION and ERROR_LOCK_VIOLATION. + cfg!(windows) && matches!(err.raw_os_error(), Some(32 | 33)) +} + +/// What `autter upgrade` says when the installed binary cannot be moved. +pub(crate) fn swap_failure_message(exe: &Path, err: &io::Error) -> String { + if is_in_use(err) { + format!( + "Could not replace {}: the file is in use ({err}).\n\ + The autter background service or another program still has it open, and Windows does not replace a running program.\n\ + The current autter is still installed and works.\n\ + Fix: run `autter bg shutdown`, then `autter upgrade` again.", + exe.display() + ) + } else { + format!( + "Could not move {} aside to replace it ({err}).\nThe current autter is still installed and works.", + exe.display() + ) + } +} + +/// Run `run_installer`, which writes a new binary at `exe`, with the +/// background service stopped and the binary there now renamed aside. +/// +/// Returns the installer's error, or an error when `exe` could not be moved; +/// in both cases the binary that was at `exe` is there again. The service is +/// started again, on whatever binary is at `exe`, if it was running. +pub(crate) fn run_installer_with_binary_aside( + host: &impl SwapHost, + exe: &Path, + pid: u32, + run_installer: impl FnOnce() -> Result<(), String>, +) -> Result<(), String> { + if let Some(dir) = exe.parent() { + remove_leftover_binaries(host, dir, INSTALLED_BINARY_NAMES); + } + + let service_was_running = host.service_running(); + if service_was_running && let Err(err) = host.stop_service() { + // Not fatal: renaming aside works on a running file. + eprintln!("Could not stop the autter background service ({err}); continuing."); + } + let restart = || { + if service_was_running + && host.exists(exe) + && let Err(err) = host.start_service(exe) + { + eprintln!( + "Could not start the autter background service again ({err}). Run `autter bg restart`." + ); + } + }; + + let aside = if host.exists(exe) { + let aside = aside_path(host, exe, pid); + if let Err(err) = host.rename(exe, &aside) { + restart(); + return Err(swap_failure_message(exe, &err)); + } + Some(aside) + } else { + None + }; + + let mut result = run_installer(); + + if let Some(aside) = aside { + if host.exists(exe) { + // Fails while this process (or the old service) still runs it. + let _ = host.remove(&aside); + } else { + match host.rename(&aside, exe) { + Ok(()) => { + let reason = result.err().unwrap_or_else(|| { + "The installer left no autter binary behind.".to_string() + }); + result = Err(format!( + "{reason}\nThe previous autter is still installed ({}).", + exe.display() + )); + } + Err(err) => { + let reason = result.err().unwrap_or_default(); + return Err(format!( + "{reason}\nThe previous autter could not be put back ({err}): rename {} to {}.", + aside.display(), + exe.display() + )); + } + } + } + } + + restart(); + result +} + +/// The real file system and background service. +#[cfg(windows)] +pub(crate) struct SystemHost { + daemon: Option, +} + +#[cfg(windows)] +impl SystemHost { + pub(crate) fn new() -> Self { + Self { + daemon: crate::daemon::DaemonConfig::from_env_or_default_paths().ok(), + } + } +} + +#[cfg(windows)] +impl SwapHost for SystemHost { + fn exists(&self, path: &Path) -> bool { + std::fs::symlink_metadata(path).is_ok() + } + + fn rename(&self, from: &Path, to: &Path) -> io::Result<()> { + std::fs::rename(from, to) + } + + fn remove(&self, path: &Path) -> io::Result<()> { + std::fs::remove_file(path) + } + + fn list_dir(&self, dir: &Path) -> Vec { + std::fs::read_dir(dir) + .map(|entries| entries.filter_map(|e| e.ok().map(|e| e.path())).collect()) + .unwrap_or_default() + } + + fn service_running(&self) -> bool { + self.daemon + .as_ref() + .is_some_and(crate::commands::daemon::daemon_is_running_or_starting) + } + + fn stop_service(&self) -> Result<(), String> { + match &self.daemon { + Some(config) => { + crate::commands::daemon::stop_daemon(config, std::time::Duration::from_secs(10)) + } + None => Ok(()), + } + } + + fn start_service(&self, exe: &Path) -> Result<(), String> { + let status = std::process::Command::new(exe) + .args(["bg", "start"]) + // Debug builds read AUTTER=git as "run as git". + .env_remove("AUTTER") + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .status() + .map_err(|e| e.to_string())?; + if status.success() { + Ok(()) + } else { + Err(format!("starting it exited with {status}")) + } + } +} + +/// Delete the aside files of earlier upgrades in `~/.autter/bin` that nothing +/// runs any more. Called when the background service starts. +#[cfg(windows)] +pub(crate) fn remove_leftover_installed_binaries() { + let Some(home) = dirs::home_dir() else { + return; + }; + let host = SystemHost { daemon: None }; + remove_leftover_binaries( + &host, + &home.join(".autter").join("bin"), + INSTALLED_BINARY_NAMES, + ); +} + +#[cfg(test)] +mod tests { + use super::*; + use std::cell::RefCell; + use std::collections::{BTreeMap, BTreeSet}; + + /// A stand-in for the Windows file system: a file in `running` is the + /// image of a live process (it can be renamed, not replaced or deleted); + /// a file in `locked` is open without delete sharing (cannot be renamed). + #[derive(Default)] + struct FakeHost { + files: RefCell>, + running: RefCell>, + locked: BTreeSet, + service_up: bool, + events: RefCell>, + } + + fn name(path: &Path) -> String { + path.file_name().unwrap().to_string_lossy().into_owned() + } + + impl FakeHost { + fn with(files: &[(&Path, &'static str)]) -> Self { + let host = FakeHost::default(); + for (path, content) in files { + host.files.borrow_mut().insert(path.to_path_buf(), content); + } + host + } + + fn log(&self, event: String) { + self.events.borrow_mut().push(event); + } + + fn events(&self) -> Vec { + self.events.borrow().clone() + } + + fn listing(&self) -> Vec<(String, &'static str)> { + self.files + .borrow() + .iter() + .map(|(p, c)| (name(p), *c)) + .collect() + } + } + + fn denied(what: &str) -> io::Error { + io::Error::new(io::ErrorKind::PermissionDenied, format!("{what} is in use")) + } + + impl SwapHost for FakeHost { + fn exists(&self, path: &Path) -> bool { + self.files.borrow().contains_key(path) + } + + fn rename(&self, from: &Path, to: &Path) -> io::Result<()> { + self.log(format!("rename {} -> {}", name(from), name(to))); + if self.locked.contains(from) { + return Err(denied(&name(from))); + } + if self.exists(to) && self.running.borrow().contains(to) { + return Err(denied(&name(to))); + } + let content = self + .files + .borrow_mut() + .remove(from) + .ok_or_else(|| io::Error::from(io::ErrorKind::NotFound))?; + self.files.borrow_mut().insert(to.to_path_buf(), content); + if self.running.borrow_mut().remove(from) { + self.running.borrow_mut().insert(to.to_path_buf()); + } + Ok(()) + } + + fn remove(&self, path: &Path) -> io::Result<()> { + self.log(format!("remove {}", name(path))); + if self.running.borrow().contains(path) || self.locked.contains(path) { + return Err(denied(&name(path))); + } + self.files.borrow_mut().remove(path); + Ok(()) + } + + fn list_dir(&self, dir: &Path) -> Vec { + self.files + .borrow() + .keys() + .filter(|p| p.parent() == Some(dir)) + .cloned() + .collect() + } + + fn service_running(&self) -> bool { + self.service_up + } + + fn stop_service(&self) -> Result<(), String> { + self.log("stop service".to_string()); + Ok(()) + } + + fn start_service(&self, exe: &Path) -> Result<(), String> { + let content = self.files.borrow().get(exe).copied().unwrap_or("none"); + self.log(format!("start service on {} ({content})", name(exe))); + Ok(()) + } + } + + fn dir() -> PathBuf { + PathBuf::from("home").join(".autter").join("bin") + } + + fn exe() -> PathBuf { + dir().join("autter.exe") + } + + fn aside() -> PathBuf { + dir().join("autter.exe.old") + } + + /// What install.ps1 does once the old binary is out of the way. + fn installer_writes_new<'a>(host: &'a FakeHost) -> impl FnOnce() -> Result<(), String> + 'a { + move || { + host.log("installer".to_string()); + if host.exists(&exe()) { + return Err("the installer found autter.exe still in place".to_string()); + } + host.files.borrow_mut().insert(exe(), "new"); + Ok(()) + } + } + + #[test] + fn running_binary_is_renamed_aside_and_the_service_restarts_on_the_new_one() { + let mut host = FakeHost::with(&[(&exe(), "old")]); + host.service_up = true; + // `autter upgrade` itself runs the installed binary. + host.running.borrow_mut().insert(exe()); + + let result = run_installer_with_binary_aside(&host, &exe(), 7, installer_writes_new(&host)); + assert_eq!(result, Ok(())); + assert_eq!( + host.events(), + vec![ + "stop service", + "rename autter.exe -> autter.exe.old", + "installer", + // Still running this process: deleted by a later run. + "remove autter.exe.old", + "start service on autter.exe (new)", + ] + ); + assert_eq!( + host.listing(), + vec![ + ("autter.exe".to_string(), "new"), + ("autter.exe.old".to_string(), "old") + ] + ); + + // The next upgrade (or service start), with the old process gone, + // deletes the leftover. + host.running.borrow_mut().clear(); + remove_leftover_binaries(&host, &dir(), INSTALLED_BINARY_NAMES); + assert_eq!(host.listing(), vec![("autter.exe".to_string(), "new")]); + } + + #[test] + fn service_that_was_not_running_is_not_started() { + let host = FakeHost::with(&[(&exe(), "old")]); + let result = run_installer_with_binary_aside(&host, &exe(), 7, installer_writes_new(&host)); + assert_eq!(result, Ok(())); + let events = host.events(); + assert!(!events.iter().any(|e| e.contains("service")), "{events:?}"); + // Nothing ran the aside file, so it is gone already. + assert_eq!(host.listing(), vec![("autter.exe".to_string(), "new")]); + } + + #[test] + fn file_in_use_fails_loudly_and_leaves_the_old_binary() { + let mut host = FakeHost::with(&[(&exe(), "old")]); + host.service_up = true; + host.locked.insert(exe()); + + let mut installer_ran = false; + let result = run_installer_with_binary_aside(&host, &exe(), 7, || { + installer_ran = true; + Ok(()) + }); + let message = result.unwrap_err(); + assert!(!installer_ran); + assert!(message.contains("the file is in use"), "{message}"); + assert!(message.contains("still installed and works"), "{message}"); + assert!( + message.contains("Fix: run `autter bg shutdown`, then `autter upgrade` again."), + "{message}" + ); + assert_eq!(host.listing(), vec![("autter.exe".to_string(), "old")]); + // The service that was stopped runs again, on the old binary. + assert_eq!( + host.events(), + vec![ + "stop service", + "rename autter.exe -> autter.exe.old", + "start service on autter.exe (old)", + ] + ); + } + + #[test] + fn failed_installer_puts_the_old_binary_back() { + let mut host = FakeHost::with(&[(&exe(), "old")]); + host.service_up = true; + host.running.borrow_mut().insert(exe()); + + let result = run_installer_with_binary_aside(&host, &exe(), 7, || { + Err("Installation script failed with exit code: Some(1)".to_string()) + }); + let message = result.unwrap_err(); + assert!( + message.starts_with("Installation script failed"), + "{message}" + ); + assert!( + message.contains("previous autter is still installed"), + "{message}" + ); + assert_eq!(host.listing(), vec![("autter.exe".to_string(), "old")]); + assert_eq!( + host.events().last().map(String::as_str), + Some("start service on autter.exe (old)") + ); + } + + #[test] + fn installer_that_succeeds_without_a_binary_is_a_failure() { + let host = FakeHost::with(&[(&exe(), "old")]); + let result = run_installer_with_binary_aside(&host, &exe(), 7, || Ok(())); + let message = result.unwrap_err(); + assert!(message.contains("left no autter binary"), "{message}"); + assert_eq!(host.listing(), vec![("autter.exe".to_string(), "old")]); + } + + #[test] + fn installer_failing_after_the_new_binary_is_in_place_keeps_the_new_one() { + // e.g. git.exe could not be refreshed: autter.exe is already new. + let host = FakeHost::with(&[(&exe(), "old")]); + let result = run_installer_with_binary_aside(&host, &exe(), 7, || { + host.files.borrow_mut().insert(exe(), "new"); + Err("Installation script failed with exit code: Some(1)".to_string()) + }); + assert_eq!( + result, + Err("Installation script failed with exit code: Some(1)".to_string()) + ); + assert_eq!(host.listing(), vec![("autter.exe".to_string(), "new")]); + } + + #[test] + fn an_old_aside_file_still_running_gets_a_numbered_neighbour() { + let host = FakeHost::with(&[(&exe(), "old"), (&aside(), "older")]); + host.running.borrow_mut().insert(aside()); + let result = run_installer_with_binary_aside(&host, &exe(), 7, installer_writes_new(&host)); + assert_eq!(result, Ok(())); + assert_eq!( + host.listing(), + vec![ + ("autter.exe".to_string(), "new"), + ("autter.exe.old".to_string(), "older"), + ] + ); + assert!( + host.events() + .contains(&"rename autter.exe -> autter.exe.7.old".to_string()), + "{:?}", + host.events() + ); + } + + #[test] + fn no_installed_binary_runs_the_installer_as_is() { + let host = FakeHost::default(); + let result = run_installer_with_binary_aside(&host, &exe(), 7, installer_writes_new(&host)); + assert_eq!(result, Ok(())); + assert_eq!(host.events(), vec!["installer"]); + } + + #[test] + fn leftovers_are_recognised_by_name() { + let host = FakeHost::with(&[ + (&exe(), "a"), + (&dir().join("autter.exe.old"), "b"), + (&dir().join("autter.exe.123.old"), "c"), + (&dir().join("GIT.EXE.old"), "d"), + (&dir().join("git.exe"), "e"), + (&dir().join("autter.exe.bak"), "f"), + (&dir().join("other.exe.old"), "g"), + ]); + let names: Vec = leftover_binaries(&host, &dir(), INSTALLED_BINARY_NAMES) + .iter() + .map(|p| name(p)) + .collect(); + assert_eq!( + names, + vec!["GIT.EXE.old", "autter.exe.123.old", "autter.exe.old"] + ); + } + + #[test] + fn in_use_errors_name_the_cause_and_the_fix() { + let message = swap_failure_message(&exe(), &denied("autter.exe")); + assert!(message.contains("in use"), "{message}"); + assert!(message.contains("autter bg shutdown"), "{message}"); + let other = swap_failure_message(&exe(), &io::Error::from(io::ErrorKind::NotFound)); + assert!(!other.contains("in use"), "{other}"); + assert!(other.contains("still installed"), "{other}"); + } + + /// The real Windows file system: a running .exe can be renamed aside and + /// replaced, and one held open without delete sharing fails with the + /// in-use message and stays in place. + #[cfg(windows)] + #[test] + fn system_host_swaps_a_running_exe_and_reports_a_held_one() { + use std::os::windows::fs::OpenOptionsExt; + + let dir = tempfile::tempdir().unwrap(); + let exe = dir.path().join("autter.exe"); + let windir = std::env::var_os("SystemRoot").unwrap_or_else(|| "C:\\Windows".into()); + let ping = PathBuf::from(windir).join("System32").join("PING.EXE"); + std::fs::copy(&ping, &exe).unwrap(); + let mut child = std::process::Command::new(&exe) + .args(["-n", "60", "127.0.0.1"]) + .stdout(std::process::Stdio::null()) + .spawn() + .unwrap(); + let host = SystemHost { daemon: None }; + + // Renaming the running file aside works, and the new one goes in. + let new = dir.path().join("new.bin"); + std::fs::write(&new, b"new").unwrap(); + let result = run_installer_with_binary_aside(&host, &exe, 7, || { + std::fs::rename(&new, &exe).map_err(|e| e.to_string()) + }); + assert_eq!(result, Ok(())); + assert_eq!(std::fs::read(&exe).unwrap(), b"new"); + let aside = dir.path().join("autter.exe.old"); + assert!(aside.exists(), "still running, so kept for later"); + + let _ = child.kill(); + let _ = child.wait(); + remove_leftover_binaries(&host, dir.path(), INSTALLED_BINARY_NAMES); + assert!(!aside.exists()); + + // Held open without FILE_SHARE_DELETE: cannot even be renamed. + let held = std::fs::OpenOptions::new() + .read(true) + .share_mode(1) // FILE_SHARE_READ + .open(&exe) + .unwrap(); + let result = run_installer_with_binary_aside(&host, &exe, 7, || { + panic!("the installer must not run when the binary cannot be moved") + }); + drop(held); + let message = result.unwrap_err(); + assert!(message.contains("the file is in use"), "{message}"); + assert!(message.contains("autter bg shutdown"), "{message}"); + assert_eq!(std::fs::read(&exe).unwrap(), b"new"); + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index c0471a2..f177dbb 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -2,6 +2,7 @@ pub mod arg_parser; pub mod ask; pub mod autter_handlers; pub mod badge; +pub mod binary_swap; pub mod blame; pub mod checkpoint_agent; pub mod ci_handlers; diff --git a/src/commands/upgrade.rs b/src/commands/upgrade.rs index 1fd382a..65be7b5 100644 --- a/src/commands/upgrade.rs +++ b/src/commands/upgrade.rs @@ -530,109 +530,145 @@ fn try_mock_releases(base: &str, channel: UpdateChannel) -> Option Result<(), String> { - #[cfg(windows)] - { - if let Ok(daemon_config) = crate::daemon::DaemonConfig::from_env_or_default_paths() { - // Best effort: stop the daemon before we hand off to the detached installer. - // The install script also has a fallback kill path so old released binaries - // can still recover, but stopping here makes upgrades complete sooner. - let _ = crate::commands::daemon::stop_daemon(&daemon_config, Duration::from_secs(10)); - } - - // On Windows, we need to run the installer detached because the current autter - // binary and shims are in use and need to be replaced. The installer will wait - // for the files to be released before proceeding. - let pid = std::process::id(); - let log_dir = dirs::home_dir() - .ok_or_else(|| "Could not determine home directory".to_string())? - .join(".autter") - .join("upgrade-logs"); - - // Ensure the log directory exists - fs::create_dir_all(&log_dir) - .map_err(|e| format!("Failed to create log directory: {}", e))?; - - let log_file = log_dir.join(format!("upgrade-{}.log", pid)); - let log_path_str = log_file.to_string_lossy().to_string(); - - // Write the install script to a temp file - let script_path = log_dir.join(format!("install-{}.ps1", pid)); - fs::write(&script_path, script_content) - .map_err(|e| format!("Failed to write install script: {}", e))?; - let script_path_str = script_path.to_string_lossy().to_string(); - - // Create log file with initial message - fs::write(&log_file, format!("Starting upgrade at PID {}\n", pid)) - .map_err(|e| format!("Failed to create log file: {}", e))?; - - // PowerShell wrapper that executes the script file with logging - let ps_wrapper = format!( - "$logFile = '{}'; \ - Start-Transcript -Path $logFile -Append -Force | Out-Null; \ - Write-Host 'Running verified install script...'; \ - try {{ \ - $ErrorActionPreference = 'Continue'; \ - & '{}'; \ - Write-Host 'Install script completed'; \ - }} catch {{ \ - Write-Host \"Error: $_\"; \ - Write-Host \"Stack trace: $($_.ScriptStackTrace)\"; \ - }} finally {{ \ - if ($env:{} -eq '1') {{ \ - $daemonExe = Join-Path $HOME '.autter\\bin\\autter.exe'; \ - if (Test-Path $daemonExe) {{ try {{ & $daemonExe bg start *> $null }} catch {{ }} }} \ - }}; \ - Stop-Transcript | Out-Null; \ - Remove-Item -Path '{}' -Force -ErrorAction SilentlyContinue; \ - }}", - log_path_str, script_path_str, AUTTER_RESTART_DAEMON_AFTER_INSTALL_ENV, script_path_str - ); - - let spawn_powershell = |exe: &str| -> std::io::Result { - let mut cmd = Command::new(exe); - cmd.arg("-NoProfile") +/// `autter upgrade` on Windows: run install.ps1 and wait for it, so its +/// output is the user's and its exit code decides ours. The binary this +/// process runs is in the installer's way (Windows does not replace a running +/// program), so the background service is stopped and the binary renamed +/// aside first, and put back if the installer leaves none (see +/// `binary_swap`). The service is started again if it was running. +#[cfg(windows)] +fn run_install_script_attached(script_content: &str, tag: &str) -> Result<(), String> { + use crate::commands::binary_swap; + + let pid = std::process::id(); + let autter_dir = dirs::home_dir() + .ok_or_else(|| "Could not determine home directory".to_string())? + .join(".autter"); + let log_dir = autter_dir.join("upgrade-logs"); + fs::create_dir_all(&log_dir).map_err(|e| format!("Failed to create log directory: {}", e))?; + let script_path = log_dir.join(format!("install-{}.ps1", pid)); + fs::write(&script_path, script_content) + .map_err(|e| format!("Failed to write install script: {}", e))?; + let exe = autter_dir.join("bin").join("autter.exe"); + + let host = binary_swap::SystemHost::new(); + let result = binary_swap::run_installer_with_binary_aside(&host, &exe, pid, || { + let run = |shell: &str| { + Command::new(shell) + .arg("-NoProfile") .arg("-ExecutionPolicy") .arg("Bypass") - .arg("-Command") - .arg(&ps_wrapper) - .env(AUTTER_RELEASE_ENV, tag); + .arg("-File") + .arg(&script_path) + .env(AUTTER_RELEASE_ENV, tag) + // Debug builds read AUTTER=git as "run as git". + .env_remove("AUTTER") + .status() + }; + let status = run("pwsh") + .or_else(|_| run("powershell")) + .map_err(|e| format!("Failed to run installation script: {}", e))?; + if status.success() { + Ok(()) + } else { + Err(format!( + "Installation script failed with exit code: {}", + status + .code() + .map(|c| c.to_string()) + .unwrap_or_else(|| "unknown".to_string()) + )) + } + }); + let _ = fs::remove_file(&script_path); + result +} - // Hide the spawned console to prevent any host/UI bleed-through - cmd.creation_flags(CREATE_NO_WINDOW); +/// The background service's own self-upgrade on Windows: the service process +/// is exiting, so the installer runs detached, logs to +/// ~/.autter/upgrade-logs, and starts the service again when it is done. +#[cfg(windows)] +fn run_install_script_detached(script_content: &str, tag: &str) -> Result<(), String> { + if let Ok(daemon_config) = crate::daemon::DaemonConfig::from_env_or_default_paths() { + // Best effort: stop the daemon before we hand off to the detached installer. + let _ = crate::commands::daemon::stop_daemon(&daemon_config, Duration::from_secs(10)); + } - if silent { - cmd.env(AUTTER_RESTART_DAEMON_AFTER_INSTALL_ENV, "1"); - cmd.env(AUTTER_DAEMON_UPGRADE_ENV, "1"); - cmd.stdout(Stdio::null()).stderr(Stdio::null()); - } + let pid = std::process::id(); + let log_dir = dirs::home_dir() + .ok_or_else(|| "Could not determine home directory".to_string())? + .join(".autter") + .join("upgrade-logs"); + fs::create_dir_all(&log_dir).map_err(|e| format!("Failed to create log directory: {}", e))?; + + let log_file = log_dir.join(format!("upgrade-{}.log", pid)); + let log_path_str = log_file.to_string_lossy().to_string(); + + let script_path = log_dir.join(format!("install-{}.ps1", pid)); + fs::write(&script_path, script_content) + .map_err(|e| format!("Failed to write install script: {}", e))?; + let script_path_str = script_path.to_string_lossy().to_string(); + + fs::write(&log_file, format!("Starting upgrade at PID {}\n", pid)) + .map_err(|e| format!("Failed to create log file: {}", e))?; + + // Runs the script with a transcript. A failure is logged and also ends + // the wrapper with exit code 1 (it used to be caught and exit 0). + let ps_wrapper = format!( + "$logFile = '{}'; \ + $failed = $false; \ + Start-Transcript -Path $logFile -Append -Force | Out-Null; \ + Write-Host 'Running verified install script...'; \ + try {{ \ + $ErrorActionPreference = 'Continue'; \ + & '{}'; \ + Write-Host 'Install script completed'; \ + }} catch {{ \ + $failed = $true; \ + Write-Host \"Error: $_\"; \ + Write-Host \"Stack trace: $($_.ScriptStackTrace)\"; \ + }} finally {{ \ + if ($env:{} -eq '1') {{ \ + $daemonExe = Join-Path $HOME '.autter\\bin\\autter.exe'; \ + if (Test-Path $daemonExe) {{ try {{ & $daemonExe bg start *> $null }} catch {{ }} }} \ + }}; \ + Stop-Transcript | Out-Null; \ + Remove-Item -Path '{}' -Force -ErrorAction SilentlyContinue; \ + }}; \ + if ($failed) {{ exit 1 }}", + log_path_str, script_path_str, AUTTER_RESTART_DAEMON_AFTER_INSTALL_ENV, script_path_str + ); - cmd.spawn() - }; + let spawn_powershell = |exe: &str| -> std::io::Result { + let mut cmd = Command::new(exe); + cmd.arg("-NoProfile") + .arg("-ExecutionPolicy") + .arg("Bypass") + .arg("-Command") + .arg(&ps_wrapper) + .env(AUTTER_RELEASE_ENV, tag) + .env(AUTTER_RESTART_DAEMON_AFTER_INSTALL_ENV, "1") + .env(AUTTER_DAEMON_UPGRADE_ENV, "1") + .stdout(Stdio::null()) + .stderr(Stdio::null()); + // Hide the spawned console to prevent any host/UI bleed-through + cmd.creation_flags(CREATE_NO_WINDOW); + cmd.spawn() + }; - let spawn_result = spawn_powershell("pwsh").or_else(|_| spawn_powershell("powershell")); - - match spawn_result { - Ok(_) => { - if !silent { - println!( - "{}", - paint( - "1;33", - "Note: The installation is running in the background on Windows." - ) - ); - println!( - "This allows the current autter process to exit and release file locks." - ); - println!("Check the log file for progress: {}", log_path_str); - println!( - "The installer will stop lingering autter background processes if needed, but active git commands can still delay completion." - ); - } - Ok(()) - } - Err(e) => Err(format!("Failed to run installation script: {}", e)), + spawn_powershell("pwsh") + .or_else(|_| spawn_powershell("powershell")) + .map(|_| ()) + .map_err(|e| format!("Failed to run installation script: {}", e)) +} + +fn run_install_script(script_content: &str, tag: &str, silent: bool) -> Result<(), String> { + #[cfg(windows)] + { + if silent { + run_install_script_detached(script_content, tag) + } else { + run_install_script_attached(script_content, tag) } } @@ -850,19 +886,14 @@ fn run_impl_with_url( match run_install_script(&script_content, &release.tag, false) { Ok(()) => { - // On Windows, we spawn the installer in the background and can't verify success - #[cfg(not(windows))] - { - println!( - "{} Successfully installed {}!", - paint("1;32", "✓"), - release.tag - ); - // Rewrite the hooks of agents already set up so they call the - // new binary. Windows installs finish in the background; there - // the first command after the install does it instead. - crate::mdm::hook_refresh::refresh_with_installed_binary(); - } + println!( + "{} Successfully installed {}!", + paint("1;32", "✓"), + release.tag + ); + // Rewrite the hooks of agents already set up so they call the + // new binary. + crate::mdm::hook_refresh::refresh_with_installed_binary(); log_message( "upgraded", @@ -876,6 +907,12 @@ fn run_impl_with_url( ); } Err(err) => { + // On Windows the error already says what is installed now and + // what to do (the installer may have got as far as replacing + // the binary). + #[cfg(windows)] + eprintln!("{err}"); + #[cfg(not(windows))] eprintln!("{}", upgrade_failure_message(&err)); std::process::exit(1); } diff --git a/src/daemon.rs b/src/daemon.rs index b3fd3c1..1ad5cbf 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -10882,8 +10882,8 @@ fn daemon_update_check_loop(coordinator: Arc, started_at /// After the daemon has fully shut down, attempt to install any pending update. /// /// On Unix the installer atomically replaces the binary via `mv`; on Windows -/// the installer is spawned as a detached process that polls until the exe is -/// unlocked. +/// the installer is spawned as a detached process that renames the running +/// exe aside and puts the new one in its place. pub(crate) fn daemon_run_pending_self_update() -> DaemonSelfUpdateOutcome { use crate::commands::upgrade::{ DaemonUpdateCheckResult, check_and_install_update_if_available, @@ -10918,6 +10918,10 @@ pub(crate) async fn run_daemon(config: DaemonConfig) -> Result