PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.85k stars 373 forks source link

How can I preload custom rules #1864

Closed iRon7 closed 1 year ago

iRon7 commented 1 year ago

Loading custom rules appears quiet slow (3 PowerShell-based rules take more than 8 seconds) This occurs with a simple Invoke-ScriptAnalyzer .\Test.ps1 command where .\Test.ps1 only includes a single statement like: gci . and on every (re)invocation. Removing the PSScriptAnalyzerSettings.psd1 from the current folder "resolves" the problem (67 milliseconds)

PSScriptAnalyzerSettings.psd1 ```PowerShell # This setting is configured in the workspace's `.vscode\settings.json`. # # For more information on PSScriptAnalyzer settings see: # https://github.com/PowerShell/PSScriptAnalyzer/blob/master/README.md#settings-support-in-scriptanalyzer # # You can see the predefined PSScriptAnalyzer settings here: # https://github.com/PowerShell/PSScriptAnalyzer/tree/master/Engine/Settings @{ # Only diagnostic records of the specified severity will be generated. # Uncomment the following line if you only want Errors and Warnings but # not Information diagnostic records. #Severity = @('Error','Warning') # Analyze **only** the following rules. Use IncludeRules when you want # to invoke only a small subset of the default rules. # IncludeRules = @('PSAvoidDefaultValueSwitchParameter', # 'PSMisleadingBacktick', # 'PSMissingModuleManifestField', # 'PSReservedCmdletChar', # 'PSReservedParams', # 'PSShouldProcess', # 'PSUseApprovedVerbs', # 'PSAvoidUsingCmdletAliases', # 'PSUseDeclaredVarsMoreThanAssignments') # Do not analyze the following rules. Use ExcludeRules when you have # commented out the IncludeRules settings above and want to include all # the default rules except for those you exclude below. # Note: if a rule is in both IncludeRules and ExcludeRules, the rule # will be excluded. # ExcludeRules = @('PSAvoidUsingWriteHost', 'PSAvoidTrailingWhitespace') # You can use rule configuration to configure rules that support it: CustomRulePath = "C:\Program Files\WindowsPowerShell\Modules\SSO_PS_ScriptAnalyzer\1.0.3\SSO_PS_ScriptAnalyzer.psm1" # CustomRulePath = ".\SSO_PS_ScriptAnalyzer.psm1" IncludeDefaultRules = $True Rules = @{ PSUseCompatibleCommands = @{ # Turns the rule on Enable = $true # Lists the PowerShell platforms we want to check compatibility with TargetProfiles = @( 'win-8_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework', 'win-8_x64_10.0.14393.0_7.0.0_x64_3.1.2_core' ) } PSUseCompatibleSyntax = @{ # This turns the rule on (setting it to false will turn it off) Enable = $true # Simply list the targeted versions of PowerShell here TargetVersions = @( '5.1', '7.0' ) } } } ```

Is there a way to preload my custom rules as a standard rule for the whole environment?

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14393.5127
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.5127
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PS C:\> Get-Module -Name PSScriptAnalyzer

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.20.0     PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-Formatter, Invoke-ScriptAnalyzer}
iRon7 commented 1 year ago

The same slow loading of custom rules (every successive Invoke-ScriptAnalyzer after the first invocation -which takes about 35 seconds-) appears on version 1.21.0. And also on PowerShell 7 (which is even slower: every successive invocation takes more than 15 seconds)

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.0
PSEdition                      Core
GitCommitId                    7.3.0
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
bergmeister commented 1 year ago

Can you provide more details and a minimal repro of the custom rules that you are using or causing that?

iRon7 commented 1 year ago

While creating a mcve, I found the issue: I was checking the PSScriptAnalyzer version (Get-InstalledModule PSScriptAnalyzer -ErrorAction SilentlyContinue) in the .psm1 file which appeared to be very expensive.