Skip to content
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

Conversation

bergmeister
Copy link
Collaborator

@bergmeister bergmeister commented May 30, 2019

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 the DiagnosticRecord)
  • '$MyInvocation.InvocationName (same as above but fully qualified, i.e. module name pre-pended to it
  • A custom string

All 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 like ScriptAnalyzer.Instance.ScriptRules not being null any more). The problem with the check is that only at runtime will we know the returned RuleName of the DiagnosticRecord. People are encouraged to use expressions that mirror the function name that returns the DiagnosticRecord so that there is a match with what Get-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

&& (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))
Copy link
Collaborator Author

@bergmeister bergmeister May 30, 2019

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.

@bergmeister bergmeister marked this pull request as ready for review May 30, 2019 07:43
@bergmeister bergmeister added this to the 1.18.1 milestone May 30, 2019
Copy link
Member

@JamesWTruher JamesWTruher left a 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?

@bergmeister
Copy link
Collaborator Author

I just sent @Jaykul and @ChrisLGardner a message on slack with a local build of 1.18.1 from this branch

@bergmeister bergmeister merged commit f99e1f4 into PowerShell:development Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom Rule Consistency
3 participants