Skip to content

Can't suppress message for a particular script #1240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rkeithhill opened this issue May 11, 2019 · 7 comments
Closed

Can't suppress message for a particular script #1240

rkeithhill opened this issue May 11, 2019 · 7 comments

Comments

@rkeithhill
Copy link
Contributor

Before submitting a bug report:

  • Make sure you are able to repro it on the latest released version
  • Perform a quick search for existing issues to check if this bug has already been reported

Steps to reproduce

Create a script with just variable definitions. This is a script you will dot source into a more complex script. Now when you analyze the script you will get a PSUseDeclaredVarsMoreThanAssignments warning. Suppressing these warnings one-by-one in such a file is a PITA and really makes this file that is supposed to be simple for my users to update, much more intimidating.

Expected behavior

I expected a single suppress message declaration at the top of the file to work:

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '', Scope="Script", Target="*")]

Actual behavior

This had no impact. The only way I can get this to work is to put this attribute (modified to remove the last two params) before every single variable declaration. And no, I don't want to disable this rule for all files, just this one file.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.2.0
PSEdition                      Core
GitCommitId                    6.2.0
OS                             Microsoft Windows 10.0.17763
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.0
1.17.1
@rkeithhill
Copy link
Contributor Author

As an aside, if it isn't easy to have an attribute at the global script level, then maybe PSSA could use a comment convention e.g.:

#pragma PSScriptAnalyzer disable PSUseDeclaredVarsMoreThanAssignments
... for the rest of the script, this warning is disabled

and

#pragma PSScriptAnalyzer disable PSUseDeclaredVarsMoreThanAssignments
... for script between these pragmas, the warning is disabled
#pragma PSScriptAnalyzer enable PSUseDeclaredVarsMoreThanAssignments

The problem, I think, is that for this to work it would require a change to PowerShell (maybe v7) to recognize #pragma directives and put them in the AST. This syntax could be generalized for use by any module willing to look for such pragma's in a script's AST e.g.:

#pragma <pragma-name> <pragma-arguments>
# For PowerShell modules, you'd use this format:
#pragma <module-name> <module-specific-directive> <arg>[,<arg>]

See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-pragma and https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-pragma-warning

Now, these are not "compiler directives" so perhaps pragma isn't the best name but I still think the concept would be easier to work with and more flexible (disabling and re-enabling around sections of the script) than using a SuppressMessage attribute.

@Jaykul
Copy link
Contributor

Jaykul commented May 14, 2019

Are you putting that attribute on a param() @rkeithhill ?

@rkeithhill
Copy link
Contributor Author

No. Just at the top of the file. I tried putting it on an empty param () at the beginning of the file but that didn't work either.

@Jaykul
Copy link
Contributor

Jaykul commented May 14, 2019

Yeah, actually, just tested ... it looks like the suppression only works on named functions.

@bergmeister
Copy link
Collaborator

bergmeister commented Jun 18, 2019

PSSA is looking for a FunctionAst, therefore if you put a param() at the top of that file (the attribute goes on top), then that does work:

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
param()

$unused1 = Get-Foo
$unused2 = Get-Bar

Issue #849 tracks more sophisticated suppression syntax

@oising
Copy link

oising commented Jul 23, 2019

Oh, and also: #1295

@SydneyhSmith
Copy link
Collaborator

Going to mark this a duplicate and track this issue with #849 and #1295

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants