Skip to content

Suppressing a custom rule works in VSCode, but not from the commandline #1344

Open
@mrboring

Description

@mrboring

Steps to reproduce

Unzip this: PSScriptAnalyzer - Custom rule suppression error.zip

The ZIP contains:

  • CustomRule.psm1
  • PSScriptAnalyzerSettings.psd1
  • Run Me.ps1
  • Suppress Testing.ps1
  • Errors.clixml
  • log.txt

Execute Run Me.ps1

Expected behavior

No errors.

Actual behavior

VSCode

Working OK. Custom rule suppressed, no squigglies:

image

Commandline

Got these errors:

Invoke-ScriptAnalyzer : Suppression Message Attribute error at line 4 in Suppress Testing.ps1 : Cannot find any DiagnosticRecord with the Rule Suppression ID undefinedVar.
At C:\Data\PowerShell\Miscellaneous\Issues\PSScriptAnalyzer - Custom rule suppression error\Run Me.ps1:3 char:1
+ Invoke-ScriptAnalyzer `
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (Microsoft.Windows.P…ric.RuleSuppression:RuleSuppression) [Invoke-ScriptAnalyzer], ArgumentException
+ FullyQualifiedErrorId : Suppression Message Attribute error at line 4 in Suppress Testing.ps1 : Cannot find any DiagnosticRecord with the Rule Suppression ID undefinedVar.,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand

Invoke-ScriptAnalyzer : Suppression Message Attribute error at line 2 in Suppress Testing.ps1 : Cannot find any DiagnosticRecord with the Rule Suppression ID Var1.
At C:\Data\PowerShell\Miscellaneous\Issues\PSScriptAnalyzer - Custom rule suppression error\Run Me.ps1:3 char:1
+ Invoke-ScriptAnalyzer `
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (Microsoft.Windows.P…ric.RuleSuppression:RuleSuppression) [Invoke-ScriptAnalyzer], ArgumentException
+ FullyQualifiedErrorId : Suppression Message Attribute error at line 2 in Suppress Testing.ps1 : Cannot find any DiagnosticRecord with the Rule Suppression ID Var1.,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand

I exported the errors to Errors.clixml. This is in the attached ZIP.

I set the custom rule to log the DiagnoticRecord. The log contained the following:

20190918 11:13:58:1358 - DiagnosticRecord: 
Message              : Avoid using non-local variables.
Extent               : $Var1
RuleName             : CustomRule\Measure-AvoidNonLocalVar
Severity             : Warning
ScriptName           : 
ScriptPath           : 
RuleSuppressionID    : Var1
SuggestedCorrections :
20190918 11:13:58:1358 - RuleSuppressionID check for leading/trailing spaces: >Var1<
20190918 11:13:58:1358 - RuleSuppressionID check property type              : System.String
--------------------------------------------------
20190918 11:13:58:1358 - DiagnosticRecord: 
Message              : Avoid using non-local variables.
Extent               : $undefinedVar
RuleName             : CustomRule\Measure-AvoidNonLocalVar
Severity             : Warning
ScriptName           : 
ScriptPath           : 
RuleSuppressionID    : undefinedVar
SuggestedCorrections :
20190918 11:13:58:1358 - RuleSuppressionID check for leading/trailing spaces: >undefinedVar<
20190918 11:13:58:1358 - RuleSuppressionID check property type              : System.String

The above shows that DiagnosticRecord with the required RuleSuppressionID were available.

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.3
PSEdition                      Core
GitCommitId                    7.0.0-preview.3
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }

1.18.3
1.18.2
1.18.1
1.18.0

Activity

mrboring

mrboring commented on Nov 21, 2019

@mrboring
Author

@bergmeister Have you made any progress on this?

SNikalaichyk

SNikalaichyk commented on Apr 29, 2020

@SNikalaichyk

Just ran into this issue as well.

robinmalik

robinmalik commented on Jan 10, 2023

@robinmalik

I'm getting these errors too, though I've noticed a difference in when the error is raised.

Example: I've a module with wrapper functions for VMware. I want to use the following suppression statement in order to ignore checks for $Global:DefaultVIServers (this is set when a connection to VMware is established, so the module functions check to see if we're already connected and if not, throw a message):

[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', 'Global:DefaultVIServers', Justification = 'false positive')]

Working example / passes checks ✅:

if($Global:DefaultVIServers.Count -eq 0) { }

Failing example ❌:

if($Null -eq $Global:DefaultVIServer) {}

Error:
Invoke-ScriptAnalyzer: Suppression Message Attribute error at line 4 in Get-ClusterStatistics.ps1 : Cannot find any DiagnosticRecord with the Rule Suppression ID Global:DefaultVIServers.

The workaround here (at least for me) is to change the checking condition. Hopefully it helps some people.

self-assigned this
on Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @mrboring@robinmalik@bergmeister@SNikalaichyk@SydneyhSmith

      Issue actions

        Suppressing a custom rule works in VSCode, but not from the commandline · Issue #1344 · PowerShell/PSScriptAnalyzer