From 046ae54485b95ac5e242ce48892191ae997ff1f8 Mon Sep 17 00:00:00 2001 From: Lucas Trzesniewski Date: Mon, 2 Feb 2026 21:25:00 +0100 Subject: fix(powershell): preserve `$LASTEXITCODE` (#3120) `$LASTEXITCODE` is an [automatic variable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.5#lastexitcode) in PowerShell which holds the exit code of the last *native* program which has run. It's a close equivalent to `$?` in bash (PowerShell also has [its own `$?` variable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.5#section-1), but it's a boolean). Atuin is a native program, and the `PSConsoleHostReadLine` function calls `atuin history start`, which resets this variable to 0 (or to something else if it fails). This PR resets this variable to its previous value, as it is that one which the user will expect. Before: image After: image Default behavior (without a profile file or Atuin): image ## Checks - [x] I am happy for maintainers to push small adjustments to this PR, to speed up the review cycle - [x] I have checked that there are no existing pull requests for the same thing --- crates/atuin/src/shell/atuin.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/atuin/src/shell/atuin.ps1 b/crates/atuin/src/shell/atuin.ps1 index 3ff4824a..33c2ba5f 100644 --- a/crates/atuin/src/shell/atuin.ps1 +++ b/crates/atuin/src/shell/atuin.ps1 @@ -63,7 +63,8 @@ New-Module -Name Atuin -ScriptBlock { $lastRunStatus = $? # Exit statuses are maintained separately for native and PowerShell commands, this needs to be taken into account. - $exitCode = if ($lastRunStatus) { 0 } elseif ($global:LASTEXITCODE) { $global:LASTEXITCODE } else { 1 } + $lastNativeExitCode = $global:LASTEXITCODE + $exitCode = if ($lastRunStatus) { 0 } elseif ($lastNativeExitCode) { $lastNativeExitCode } else { 1 } ## 2. Report the status of the previous command to Atuin (atuin history end). @@ -125,6 +126,7 @@ New-Module -Name Atuin -ScriptBlock { $env:ATUIN_COMMAND_LINE = $null } + $global:LASTEXITCODE = $lastNativeExitCode return $line } -- cgit v1.3.1