Fix a casting error when using $PSNativeCommandUsesErrorActionPreference#15993
Fix a casting error when using $PSNativeCommandUsesErrorActionPreference#15993rjmholt merged 2 commits intoPowerShell:masterfrom
$PSNativeCommandUsesErrorActionPreference#15993Conversation
$PSNativeCommandUsesErrorActionPreference
| $ErrorActionPreference = 'Continue' | ||
|
|
||
| $PSNativeCommandUseErrorActionPreference = 'Yeah' | ||
| $PSNativeCommandUseErrorActionPreference | Should -BeExactly 'Yeah' |
There was a problem hiding this comment.
Is this a good thing - that the original value is retained? It seems odd to me that $WhatIfPreference accepts any value and does not coerce it to a boolean value during assignment. I'm sensing there is a PS design principle here for pref vars that I'm not aware of.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
But for $WhatIfPreference, it's missing the argument transformation attribute, so you can assign arbitrary value to it even in the global scope ... 🤷
There was a problem hiding this comment.
This happens to all powershell action preference variable, and actually, all other automatic variables that don't have the "AllScope" option.
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...
There was a problem hiding this comment.
So is this desirable behaviour though? Or is there no way around it and we just need to prevent the error that happens later?
There was a problem hiding this comment.
To me it looks like an accidental inconsistency that we should eliminate (but possibly can't because it's a breaking change...?)
There was a problem hiding this comment.
So is this desirable behaviour though?
Absolutely not desirable :)
is there no way around it and we just need to prevent the error that happens later?
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Ok, I'm happy with the changes in here then and will merge
|
🎉 Handy links: |


PR Summary
This PR includes the following changes:
PSNativeArgumentPassing(from an experimental feature) tostatic InitialSessionState, to be consistent withPSNativeCommandUsesErrorActionPreference.GetBooleanPreferenceto get the value forPSNativeCommandUsesErrorActionPreference, because when assigning a value to that variable in a nested scope, the variable won't have the argument transformation attribute and thus can be assigned with arbitrary values. Tests are also added."PSNativeCommandArgumentPassing", use the const variable defined inExperimentalFeature.s_nativeArgumentPassingVarPath, use the one defined inSpecialVariables.PR Context
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.