PSScriptAnalyzer
PSScriptAnalyzer copied to clipboard
$FormatEnumerationLimit triggers PSAvoidGlobalVars warning
Steps to reproduce
Run PSSA against the following code:
function Test-FormatLimitGlobal
{
# Change format enum limit Globally
$Old = $Global:FormatEnumerationLimit
$Global:FormatEnumerationLimit = 1
"After changing: [$Global:FormatEnumerationLimit]"
Get-Process | Select-Object -Property Name, Threads -First 4
# Change it back
$Global:FormatEnumerationLimit = $Old
}
Expected behavior
PSSA does not trigger any warning against FormatEnumerationLimit
Actual behavior
"Found global variable 'Global:FormatEnumerationLimit'." warning issued for every use of FormatEnumerationLimit
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.18362.1801
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.1801
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.20.0
Shouldn't FormatEnumerationLimit be in SpecialVars.cs with the rest of built-ins?
Thanks for bringing this to our attention @ssudnits! We were able to reproduce this issue but are unsure when we will get around to fixing it. Will mark up for grabs.
@ssudnits Please can you clarify why you need to use the global scope in this case here because that's kind of what the rule is about. I see that Invoke-ScriptAnalyzer -ScriptDefinition '$FormatEnumerationLimit = 2'
triggers a PSUseDeclaredVarsMoreThanAssignments
violation but Invoke-ScriptAnalyzer -ScriptDefinition '$ErrorActionPreference = 2'
doesn't so FormatEnumerationLimit
could be added to a special exclusion list for PSUseDeclaredVarsMoreThanAssignments
.
@bergmeister
Because $FormatEnumerationLimit
is a global and I want code to reflect that?
Also, why does Invoke-ScriptAnalyzer -ScriptDefinition '$global:ErrorActionPreference = 2'
not trigger any warning?
Ok, I think you are right that we should add it to the list of automatic variables here: https://github.com/PowerShell/PSScriptAnalyzer/blob/master/Engine/SpecialVars.cs