PowerShell / PowerShell/PSScriptAnalyzer

Suppression with `SuppressMessageAttribute` is completely broken for variables

Open
#2,040 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

Steps to reproduce

Consider this code:

$foo = "never used" # Should generate PSUseDeclaredVarsMoreThanAssignments

$global:var1 = $null # Should generate PSAvoidGlobalVars

Now let's try to add these suppressions:

[Diagnostics.CodeAnalysis.SuppressMessage('NonexistentRule', '')]
$foo = "never used" # Should still generate PSUseDeclaredVarsMoreThanAssignments

[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidGlobalVars', '')]
$global:var1 = $null # Should NOT generate PSAvoidGlobalVars

Expected behavior

I should still see the PSUseDeclaredVarsMoreThanAssignments warning, but not the PSAvoidGlobalVars one.

Actual behavior

The opposite happens, the first message is suppressed, but the second is not:

RuleName                             Severity     ScriptName Line  Message
--------                             --------     ---------- ----  -------
PSAvoidGlobalVars                    Warning      Test.ps1      5     Found global variable 'global:var1'.

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.5
PSEdition                      Core
GitCommitId                    7.4.5
OS                             Microsoft Windows 10.0.22631
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.23.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the PowerShell reproduction against PSScriptAnalyzer 1.23.0 and trace how SuppressMessageAttribute is handled for variables. Compare rule-name matching for NonexistentRule and PSAvoidGlobalVars; done means the nonexistent rule does not suppress PSUseDeclaredVarsMoreThanAssignments, while the valid rule suppresses PSAvoidGlobalVars.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, powershell
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.