Unable to suppress PSAvoidLongLines warning in a method
See original GitHub issueI’m unable to suppress the PSAvoidLongLines warning in a method inside a class.
Steps to reproduce
Run Invoke-ScriptAnalyzer -Path . -Recurse
while the following PSScriptAnalyzerSettings.psd1
file is present in the folder:
@{
Severity = @('Error', 'Warning', 'Information')
ExcludeRules = @('PSUseBOMForUnicodeEncodedFile',
'PSAvoidUsingConvertToSecureStringWithPlainText',
'PSAvoidUsingPlainTextForPassword')
Rules = @{
PSAlignAssignmentStatement = @{
Enable = $true
CheckHashtable = $true
}
PSPlaceCloseBrace = @{
Enable = $true
NoEmptyLineBefore = $false
IgnoreOneLineBlock = $true
NewLineAfter = $true
}
PSPlaceOpenBrace = @{
Enable = $true
OnSameLine = $true
NewLineAfter = $true
IgnoreOneLineBlock = $true
}
PSAvoidLongLines = @{
Enable = $true
MaximumLineLength = 80
}
}
}
Class foo {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidLongLines", "")]
[String]output () {
$output = "a book is a book is a book. but this report on the activities of a company is"
return $output
}
}
Expected behavior
0 warnings
Actual behavior
RuleName Severity ScriptName Line Message
-------- -------- ---------- ---- -------
PSAvoidLongLines Warning test.ps1 4 Line exceeds the configured maximum length of 80 characters
1 rule violations found. Severity distribution: Error = 0, Warning = 1, Information = 0
If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 7.0.3
PSEdition Core
GitCommitId 7.0.3
OS Darwin 17.7.0 Darwin Kernel Version 17.7.0: Wed May 27 17:00:02 PDT 2020; root:xnu-4570.71.80.1~1/RELEASE_X86_64
Platform Unix
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.19.0
See also #1586 for initial report since this issue was closed i opened a new one and provided clear repo steps.
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (9 by maintainers)
Top Results From Across the Web
Unable to suppress PowerShell warning
I am working on a script and I've encountered a command that I could not suppress it's warning. I tried -WarningAction SilentlyContinue and ......
Read more >Suppress compiler warnings - Visual Studio
Right-click on the project node, and choose Properties on the context menu. Or, select the project node and press Alt+Enter.
Read more >Can't suppress messages in Error List
1.Click the column 'Suppression State' on error list->check 'Suppressed'. · 2. Select all the suppressed messages (click the first line, press ' ...
Read more >Suppressing Warnings in GCC and Clang
6 ways of suppressing compiler warnings in GCC and Clang, including writing different code, using qualifiers and specifiers, attributes, ...
Read more >14 Warning Messages
Warnings can be suppressed for an entire class by supplying a class name. For example: %warnfilter(501) Object; class Object { public: ... //...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@thomasrayner We’ll have to go to a level that is so low, the only docs is the PowerShell source code itself😅 What @rjmholt is saying is that the
ScriptPosition
class that this rules use to create anIScriptPosition
has the its readonly offset property always return zero, which we can see in the code here:https://github.com/PowerShell/PowerShell/blob/9b39f05557ef1c4fe51b14376a7c3abbcfaebff8/src/System.Management.Automation/engine/parser/Position.cs#L669-L672
However, there is also exist the
InternalScriptPosition
class that implementsIScriptPosition
, which is defined here:https://github.com/PowerShell/PowerShell/blob/9b39f05557ef1c4fe51b14376a7c3abbcfaebff8/src/System.Management.Automation/engine/parser/Position.cs#L430
As we can see, that class is not publicly exposed, therefore using reflection (something like this) one is still able to create this class, which allows passing in the offset via its constructor here: https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/parser/Position.cs#L434 An alternative to reflection (which is expensive from a performance point of view) would of course be create our own implementation of
InternalScriptPosition
but given this special case, using reflection is OK and means at least less work/code to write write 😃That would be great but there is also no pressure, PSSA release cycles are quite long anyway and I don’t think this issue is that much of a problem. My explanation only covers the Position part, you probably have to do some experiment to find what the right offset would be. The complex suppression logic that Rob linked above was created by someone else many years above and I luckily never had to touch it or look at it but looking at Rob’s comment, you might be able to just ignore it and assume it does its job correctly so that it would just be the case of finding the right offset programmatically.