Repository navigation
Closed
Description
Steps to reproduce
I have a bunch of custom rules written in PowerShell. For some of the rules I populate RuleSuppressionId in the diagnostic record, so that I can use that for rule suppression, but it is not working. For example, I have this violation returned for my custom rule, which I want to suppress across my psm1 file on a specific functions.
Line : 2107
Column : 13
Message : You should not use Get-FooBar,....
Extent : <some extent>
RuleName : ScriptName\Measure-PSAvoidFooBarCommand
Severity : Warning
ScriptName : FooBar.psm1
ScriptPath : C:\..\FooBar.psm1
RuleSuppressionID : Get-FooBar
SuggestedCorrections : {Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtentAnd I have this line suppression defined at the beginning of the psm1 file,
[SuppressMessageAttribute('ScriptName\Measure-PSAvoidFooBarCommand', CheckId = 'Get-FooBar', Scope = 'Function', Target = 'Get-SomeFunction*')]
param()
Expected behavior
It should suppress the violation but it does not.
Actual behavior
It suppress all the violations for the given rule without checking the RuleSuppressionId.
if I tweak the suppression to below, I get an error
```powershell
[SuppressMessageAttribute('ScriptName\Measure-PSAvoidFooBarCommand', 'Get-FooBar', Scope = 'Function', Target = 'Get-SomeFunction*')]
param()
Suppression Message Attribute error at line 12 in FooBar.psm1 : Cannot find any DiagnosticRecord with the Rule Suppression ID Get-FooBar....Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.18362.1593
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.1593
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.19.1Reacted by iRon7
Activity
rjmholt commentedon Jul 6, 2021
Manigandan Jegannathan (@jegannathanmaniganadan) are you able to supply a small custom rule with which we could try to reproduce this? Also how are you creating the extent for the diagnostic? I know from another issue that the extent has to have the correct offset in order to suppress correctly: #1591 (comment).
jegannathanmaniganadan commentedon Jul 30, 2021
Sorry about the delay, let me know if this helps. Can we reopen this issue if this sounds legit ? Rob Holt (@rjmholt)
Custom rule file
Filename : SampleCustomRule.psm1
Sample script with violation
Filename : SampleScript.ps1
Behavior
As you can see, it expected to suppress only one violation with rule matching
Get-FooBarbut it ends up suppressing the rule across the function.iRon7 commentedon Apr 29, 2024
I bounced into the same issue.
Is there any solution or workaround for this?
HeyItsGilbert commentedon Apr 2, 2025
This is also the same as #1344.
HeyItsGilbert commentedon Apr 2, 2025
I may be reading this wrong but I think what is happening is that the
SuppressRulemethod is throwing too early.AnalyzeSyntaxTreeparses the suppressions from the AST via Helper.Instance.GetRuleSuppression https://github.com/PowerShell/PSScriptAnalyzer/blob/main/Engine/ScriptAnalyzer.cs#L2098Helper.Instance.SuppressRuleis then called 3 different times.SuppressRuleSuppressRulemethod adds all the rules to theunappliedSuppressionsHashSet. https://github.com/PowerShell/PSScriptAnalyzer/blob/main/Engine/Helper.cs#L1346SuppressRules, the external rules aren't listed.I think the logic to detect and error on unapplied rules needs to happen after the rule suppressions.
iRon7 commentedon Sep 9, 2025
In our environment we bouncing into this bug quite often.
I am surprised that there aren't any more complains about this bug, I guess that scripters simply workaround the issue by using an empty
RuleSuppressionIDinstead and with that widening the target. But that behavior should actually be avoided.test: ✨ Add test for custom rule suppression with RuleSuppressionID
Fix SuppressMessage CustomRule (#2142)