Skip to content
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

Explicit creation of preference variables in descendant scopes should be appropriately type-constrained, as in the global scope / according to the definition of the preference variable #3483

Closed
mklement0 opened this issue Apr 4, 2017 · 1 comment
Labels
Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif Issue-Enhancement the issue is more of a feature request than a bug Resolution-No Activity Issue has had no activity for 6 months or more WG-Language parser, language semantics

Comments

@mklement0
Copy link
Contributor

mklement0 commented Apr 4, 2017

Currently, preference variables, such as $VerbosePreference, are only predefined in the global scope, where they are type-constrained, such as to [System.Management.Automation.ActionPreference], which ensures that only valid values can be assigned to them.
Some preference variables, such as $OFS, aren't even predefined in the global scope, although their type (and default value) is known, as described in Get-Help about_Preference_Variables.

By contrast, assigning to a preference variable in any descendant scope implicitly creates a new variable that is not type-constrained, which effectively allows assigning invalid values.

Therefore, assigning invalid values in scripts may go unnoticed.
(It seems that PowerShell quietly applies a given preference variable's default value in the event of encountering an invalid value.)

Steps to reproduce

$VerbosePreference = 'ThisIsAnInvalidValue'
& { $VerbosePreference = 'ThisIsAnInvalidValue' }

Expected behavior

Ideally, both commands would complain about the inability to convert string 'ThisIsAnInvalidValue' to type [System.Management.Automation.ActionPreference]

Actual behavior

  • The first command fails, as desired, because the top-level $VerbosePreference is type-constrained to [System.Management.Automation.ActionPreference], and converting string 'ThisIsAnInvalidValue' to that type fails.

  • The 2nd command is quietly accepted, because assigning to $VerbosePreference in the child scope created by & implicitly creates a new, local variable that isn't type-constrained, so it effectively becomes a [string] value that cannot be converted to a legal [System.Management.Automation.ActionPreference] value.

Environment data

PowerShell Core v6.0.0-alpha (v6.0.0-alpha.17) on Darwin Kernel Version 16.5.0: Fri Mar  3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64
@mklement0 mklement0 changed the title Suggesting: Implicit creation of preference variables in descendent scopes should use the same type as in the top-level scope Suggestion: Implicit creation of preference variables in descendent scopes should use the same type as in the top-level scope Apr 5, 2017
@iSazonov iSazonov added WG-Language parser, language semantics Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif labels Apr 6, 2017
@SteveL-MSFT SteveL-MSFT added the Issue-Enhancement the issue is more of a feature request than a bug label Apr 11, 2017
@SteveL-MSFT SteveL-MSFT added this to the 6.1.0 milestone Apr 11, 2017
@mklement0 mklement0 changed the title Suggestion: Implicit creation of preference variables in descendent scopes should use the same type as in the top-level scope Suggestion: Implicit creation of preference variables in descendant scopes should use the same type as in the top-level scope Aug 15, 2017
@mklement0 mklement0 changed the title Suggestion: Implicit creation of preference variables in descendant scopes should use the same type as in the top-level scope Implicit creation of preference variables in descendant scopes should use the same type as in the global scope Aug 15, 2017
@mklement0 mklement0 changed the title Implicit creation of preference variables in descendant scopes should use the same type as in the global scope Implicit creation of preference variables in descendant scopes should be appropriately type-constrained, as in the global scope / according to the definition of the preference variable Aug 15, 2017
@SteveL-MSFT SteveL-MSFT modified the milestones: 6.1.0-Consider, Future Jun 20, 2018
@mklement0 mklement0 changed the title Implicit creation of preference variables in descendant scopes should be appropriately type-constrained, as in the global scope / according to the definition of the preference variable Explicit creation of preference variables in descendant scopes should be appropriately type-constrained, as in the global scope / according to the definition of the preference variable Dec 14, 2020
@microsoft-github-policy-service microsoft-github-policy-service bot added Resolution-No Activity Issue has had no activity for 6 months or more labels Nov 16, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot removed this from the Future milestone Nov 23, 2023
@MatejKafka
Copy link

@kilasuit Still relevant, please reopen.

Got bitten by this today. Inside a binary cmdlet, I needed to get the value of a preference variable. I assumed that when I run SessionState.PSVariable.Get("ErrorActionPreference"), I'll always get a valid ActionPreference value. I'll update the cmdlet to be more defensive, but it would be nice to get this fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif Issue-Enhancement the issue is more of a feature request than a bug Resolution-No Activity Issue has had no activity for 6 months or more WG-Language parser, language semantics
Projects
None yet
Development

No branches or pull requests

4 participants