-
Notifications
You must be signed in to change notification settings - Fork 384
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
Enable suppression of custom rules when used together with -IncludeDefaultRules #1245
Enable suppression of custom rules when used together with -IncludeDefaultRules #1245
Conversation
&& (ScriptAnalyzer.Instance.ExternalRules != null | ||
&& ScriptAnalyzer.Instance.ExternalRules.Count(item => String.Equals(item.GetFullName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0) | ||
&& (ScriptAnalyzer.Instance.DSCResourceRules != null | ||
&& ScriptAnalyzer.Instance.DSCResourceRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As one can see in the combination of all those statements is that we do not know whether the rule name to be suppressed is of type custom rule (where the rule name cannot be determined at design time) or not. Therefore it is not possible to make a statement whether the given rule name will be in one of the returned DiagnosticRecords, hence why the whole check is being removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks good to me, did @Jaykul have an opportunity to comment on the tests to determine whether they captured his issue?
I just sent @Jaykul and @ChrisLGardner a message on slack with a local build of |
PR Summary
Fixes #1237
cc @Jaykul
In PSSA 1.17.1 and 1.18.1 I went through 3 test cases of using the following expressions as a custom rule name returned in the
DiagnosticRecord
of a custom rule:$MyInvocation.MyCommand.Name
(the name of the function name that returns theDiagnosticRecord
)'$MyInvocation.InvocationName
(same as above but fully qualified, i.e. module name pre-pended to itAll those scenarios are possible in both versions but when the
-IncludeDefaultRules
switch was used, then in PSSA 1.17.1 the 2nd test case and in PSSA 1.18.0 the 1st and 3rd test case were not working because PSSA throws a red-herring error. This PR makes PSSA not throw an error any more and therefore enabling all 3 scenarios.The check that used to throw the error tried to check the rule name in the suppression against the available rule names. Unfortunately the conditional logic itself is a bit broken (this is why the errors only surfaced when the
-IncludeDefaultRules
was used due to properties likeScriptAnalyzer.Instance.ScriptRules
not being null any more). The problem with the check is that only at runtime will we know the returnedRuleName
of theDiagnosticRecord
. People are encouraged to use expressions that mirror the function name that returns theDiagnosticRecord
so that there is a match with whatGet-ScriptAnalyzer
returns but the reality is that people can return what they want and actively want to do so, see here. Therefore the check is removed as we cannot determine at this point in time if there is a match or not.Comparing the functionality with C#: the compiler similarly cannot throw an error either when a suppression attribute does not match an existing code style rule violation, therefore we are removing a feature that was never meant to be complete.
PR Checklist
.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
to the beginning of the title and remove the prefix when the PR is ready.