Skip to content

Fix a casting error when using $PSNativeCommandUsesErrorActionPreference#15993

Merged
rjmholt merged 2 commits intoPowerShell:masterfrom
daxian-dbw:variable
Aug 26, 2021
Merged

Fix a casting error when using $PSNativeCommandUsesErrorActionPreference#15993
rjmholt merged 2 commits intoPowerShell:masterfrom
daxian-dbw:variable

Conversation

@daxian-dbw
Copy link
Copy Markdown
Member

@daxian-dbw daxian-dbw commented Aug 25, 2021

PR Summary

This PR includes the following changes:

  1. Move the declaration of the variable PSNativeArgumentPassing (from an experimental feature) to static InitialSessionState, to be consistent with PSNativeCommandUsesErrorActionPreference.
  2. Change back to GetBooleanPreference to get the value for PSNativeCommandUsesErrorActionPreference, 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.

image

  1. Instead of using the string "PSNativeCommandArgumentPassing", use the const variable defined in ExperimentalFeature.
  2. Instead of using s_nativeArgumentPassingVarPath, use the one defined in SpecialVariables.
  3. A few other simple cleanup changes.

PR Context

PR Checklist

@ghost ghost assigned TravisEz13 Aug 25, 2021
@daxian-dbw daxian-dbw assigned rjmholt and unassigned TravisEz13 Aug 25, 2021
@daxian-dbw daxian-dbw changed the title some clean-up changes Fix a casting error when using $PSNativeCommandUsesErrorActionPreference Aug 25, 2021
Comment thread src/System.Management.Automation/engine/ExecutionContext.cs
$ErrorActionPreference = 'Continue'

$PSNativeCommandUseErrorActionPreference = 'Yeah'
$PSNativeCommandUseErrorActionPreference | Should -BeExactly 'Yeah'
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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 $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.

Copy link
Copy Markdown
Member Author

@daxian-dbw daxian-dbw Aug 25, 2021

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But for $WhatIfPreference, it's missing the argument transformation attribute, so you can assign arbitrary value to it even in the global scope ... 🤷

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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.

I'm not seeing that:
image

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...

Copy link
Copy Markdown
Member Author

@daxian-dbw daxian-dbw Aug 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

This is from 7.2.0-preview.9. The key is a new local scope.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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...?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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

@rjmholt rjmholt merged commit 403767d into PowerShell:master Aug 26, 2021
@iSazonov iSazonov added this to the 7.2.0-rc.1 milestone Aug 26, 2021
@daxian-dbw daxian-dbw deleted the variable branch August 26, 2021 06:28
xtqqczze pushed a commit to xtqqczze/PowerShell-PowerShell that referenced this pull request Sep 2, 2021
@daxian-dbw daxian-dbw added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Sep 9, 2021
@ghost
Copy link
Copy Markdown

ghost commented Dec 16, 2021

🎉v7.3.0-preview.1 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants