Repository navigation
Fix a casting error when using $PSNativeCommandUsesErrorActionPreference
#15993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,19 @@ Describe 'Native command error handling tests' -Tags 'CI' { | |
| $stderr[1].Exception.Message | Should -BeExactly "Program `"$exeName`" ended with non-zero exit code: 1." | ||
| } | ||
|
|
||
| It "Non-boolean value should not cause type casting error when the native command exited with non-zero code" { | ||
| $ErrorActionPreference = 'Continue' | ||
|
|
||
| $PSNativeCommandUseErrorActionPreference = 'Yeah' | ||
| $PSNativeCommandUseErrorActionPreference | Should -BeExactly 'Yeah' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a good thing - that the original value is retained? It seems odd to me that
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This happens to all powershell action preference variable, and actually, all other automatic variables that don't have the "AllScope" option. For variables that are not 'AllScope', they are not copied to a new local scope and thus an assignment to the same variable in a nested scope actually create a new variable in that scope, which doesn't have the argument transformation attribute as the one declared in the global scope. BTW, we want to avoid 'AllScope' variables as much as possible because it's expensive.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But for
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But also, is there an actual reason for that behaviour to persist with this variable? Surely we want to eagerly convert it to a boolean, since the only thing that can happen is that it becomes a landmine later...
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So is this desirable behaviour though? Or is there no way around it and we just need to prevent the error that happens later?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me it looks like an accidental inconsistency that we should eliminate (but possibly can't because it's a breaking change...?)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Absolutely not desirable :)
Making the variable 'AllScope' will prevent it, because 'AllScope' means copy the same variable to every new scope you created. But that's expensive (cost is not necessarily worth the gain), and it may cause breaking changes when it comes to action preference variables considering the existing optimization on those variables.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There could be other ways to improve it, for example, by making those variables aware to PowerShell when creating a new one in a local scope, so the transformation attribute can be automatically added. This is out scope for this PR, but we surely can discuss more about it.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'm happy with the changes in here then and will merge |
||
|
|
||
| $stderr = testexe -returncode 1 2>&1 | ||
|
|
||
| $error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode' | ||
| $error[0].TargetObject | Should -BeExactly $exeName | ||
| $stderr[1].Exception.Message | Should -BeExactly "Program `"$exeName`" ended with non-zero exit code: 1." | ||
| } | ||
|
|
||
| It 'Non-zero exit code generates a non-teminating error for $ErrorActionPreference = ''SilentlyContinue''' { | ||
| $ErrorActionPreference = 'SilentlyContinue' | ||
|
|
||
|
|
@@ -174,5 +187,18 @@ Describe 'Native command error handling tests' -Tags 'CI' { | |
| $LASTEXITCODE | Should -Be 1 | ||
| $Error.Count | Should -Be 0 | ||
| } | ||
|
|
||
| It "Non-boolean value should not cause type casting error when the native command exited with non-zero code" { | ||
| $ErrorActionPreference = 'Continue' | ||
|
|
||
| $PSNativeCommandUseErrorActionPreference = 0 | ||
| $PSNativeCommandUseErrorActionPreference | Should -Be 0 | ||
| $PSNativeCommandUseErrorActionPreference | Should -BeOfType 'System.Int32' | ||
|
|
||
| testexe -returncode 1 > $null | ||
|
|
||
| $LASTEXITCODE | Should -Be 1 | ||
| $Error.Count | Should -Be 0 | ||
| } | ||
| } | ||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.