fix: PowerShell init - replace Get-Error with $error[0] #2873
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Get-Error
does not exist in all versions of PowerShell, and attempting to use it on machines where it does not exist pollutes$error
. Someone may be tempted to use-ErrorAction Ignore
but since the command does not exist, it still ends up in$error
This is a dual bug fix because it actually gets errors on all machines now and it does not pollute
$error
.Motivation and Context
In looking for unrelated errors, I kept seeing an error about
Get-Error
not existing, and narrowed it down to starship.I also changed
-expand
to-ExpandProperty
while I was at it because it is best practice.Screenshots (if appropriate):
$error[0]
exists in all versions of PowerShell and will not cause issues even if there are no errorsHow Has This Been Tested?
I didn't test within starship, but I did at the console. First, I opened a fresh console with no errors and you can see that attempting to get
$lastCmdletError
did not cause errors. Next, I forced an error, then I ran the line again and you can see that it did get$lastCmdletError
Checklist:
There are no other instances of
Get-Error
, including tests, so this was likely the only place it was being used