fix(pwsh): Use global:error[0] for most recent error in powershell (#3584)

* Use global:error[0] for most recent error

The current version use $error[0], which is for the module instead of the global status and is always $null, so the $lastExitCodeForPrompot is always $origLastExitCode (see https://github.com/starship/starship/issues/3485 for a related bug).
It should be replaced with $global:error[0] 
Comments are also updated

* fix(pwsh): make Semantic PR bot happy
This commit is contained in:
Shan Zhou 2022-02-11 03:07:50 -08:00 committed by GitHub
parent 7347d2c195
commit 465e6fc4be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 3 deletions

View File

@ -87,13 +87,13 @@ $null = New-Module starship {
"--jobs=$($jobs)" "--jobs=$($jobs)"
) )
# Whe start from the premise that the command executed correctly, which covers also the fresh console. # We start from the premise that the command executed correctly, which covers also the fresh console.
$lastExitCodeForPrompt = 0 $lastExitCodeForPrompt = 0
if ($lastCmd = Get-History -Count 1) { if ($lastCmd = Get-History -Count 1) {
# In case we have a False on the Dollar hook, we know there's an error. # In case we have a False on the Dollar hook, we know there's an error.
if (-not $origDollarQuestion) { if (-not $origDollarQuestion) {
# We retrieve the InvocationInfo from the most recent error using $error[0] # We retrieve the InvocationInfo from the most recent error using $global:error[0]
$lastCmdletError = try { $error[0] | Where-Object { $_ -ne $null } | Select-Object -ExpandProperty InvocationInfo } catch { $null } $lastCmdletError = try { $global:error[0] | Where-Object { $_ -ne $null } | Select-Object -ExpandProperty InvocationInfo } catch { $null }
# We check if the last command executed matches the line that caused the last error, in which case we know # We check if the last command executed matches the line that caused the last error, in which case we know
# it was an internal Powershell command, otherwise, there MUST be an error code. # it was an internal Powershell command, otherwise, there MUST be an error code.
$lastExitCodeForPrompt = if ($null -ne $lastCmdletError -and $lastCmd.CommandLine -eq $lastCmdletError.Line) { 1 } else { $origLastExitCode } $lastExitCodeForPrompt = if ($null -ne $lastCmdletError -and $lastCmd.CommandLine -eq $lastCmdletError.Line) { 1 } else { $origLastExitCode }