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

Implement -IncludeSuppressions parameter #1701

Merged
merged 20 commits into from
Aug 4, 2021

Conversation

rjmholt
Copy link
Contributor

@rjmholt rjmholt commented Jul 24, 2021

PR Summary

Implements the -IncludeSuppressions parameter, allowing a single Invoke-ScriptAnalyzer invocation to emit all diagnostics including suppressed ones, decorating the suppressed ones accordingly.

Also adds an IsSuppressed property on diagnostics.

/cc @t-lipingma

PR Checklist

@t-lipingma

This comment has been minimized.

@rjmholt
Copy link
Contributor Author

rjmholt commented Jul 24, 2021

Ok, I've spent ages trying to debug this but it doesn't make any sense. The new parameter sets aren't present, but the command is loaded from the asset location, and this only happens in WinPS 5.1...

image

@rjmholt
Copy link
Contributor Author

rjmholt commented Jul 25, 2021

I finally worked this out. We have a bizarre test that overrides Invoke-ScriptAnalyzer and then calls other tests... Naturally now we have to fix all of that. I've never seen anyone actually call into the theoretically exposed API...

@t-lipingma

This comment has been minimized.

WriteToOutput(diagnosticsList);
if (fix)
{
ShouldProcess(path, $"Analyzing and fixing path with Recurse={this.recurse}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a bug with the original code? If I read this correctly we will take the action no matter what, but ShouldProcess is supposed to notify the user and not take the action in the case of -WhatIf. this code will fix the script even with -WhatIf. I get that it's not as big a deal for just the diagnosis part, but the fix part seems wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I saw this and had the same thought. Definitely something we should review. If @bergmeister agrees, I can open an issue to track it so we can fix it beyond this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goes back to my very first PR in PSSA, where I added the -Fix switch and since this is an action that changes state, I implemented ShouldProcess for it and declared that on the cmdlet. Since the -Fix parameter is only on the -File parameter set, I did not implement it for the -ScriptDefinition parameter set. Original commit is here: bfa1c54
Therefore I still think it's not really important here, very rarely does one run PSSA where the command would take ages and performing a dry-run would save time. Still happy to have a tracking issue

@@ -24,6 +24,7 @@ public class SuppressedRecord : DiagnosticRecord
public SuppressedRecord(DiagnosticRecord record, IReadOnlyList<RuleSuppression> suppressions)
{
Suppression = new ReadOnlyCollection<RuleSuppression>(new List<RuleSuppression>(suppressions));
IsSuppressed = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this ever be called with a suppression list count of zero? In that case IsSuppressed really true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good question. I think the mere wrapping in the SuppressedRecord object classes the diagnostic as suppressed, but I agree that this opens up the possibility of an invalid object, which I dislike... I'm not sure I see a way around this without changing a few things around though, and I'm not sure that's worth it.

Copy link
Collaborator

@bergmeister bergmeister left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good but not sure whether doubling the parameter sets is the right way. This can make the experience more confusing for users and makes maintenance more complicated as well. How about an Enum parameter instead that has a default?
Some good improvements to tests that made some assumptions :-)

@rjmholt
Copy link
Contributor Author

rjmholt commented Jul 29, 2021

This can make the experience more confusing for users and makes maintenance more complicated as well. How about an Enum parameter instead that has a default?

So I would prefer an enum parameter, but that would be a breaking change; invocations that previously use -SuppressedOnly will no longer work.

In order to keep -SuppressedOnly while also including another parameter that affects suppression output, I think we're forced to split things into parameter sets in some way. The two mutually exclusive switches seemed like a simple approach. An alternative might be putting a more future-proof enum in the other parameter set.

@bergmeister
Copy link
Collaborator

Hmm, didn't realize -SuppressedOnly was an already existing parameter.
But I think we could still have the -IncludeSuppressions switch side by side with it but instead have some early validation logic to disallows the usage of both switches and throw an error (yes, this would be at runtime unfortunately). To me, multiple param sets for different switches not only make the code more complex but are also unintuitive as a user when I try to do tab completion and might not even be aware of other params that are excluded just because I chose to use one param and therefore don't know in most cases that this unintentional action is limiting what tab completion shows. Also, in case one person actually reads the docs and uses both switches, PowerShell's generic error is not helpful to users and although users could look at the cmdlet syntax block and try to understand what the param sets are, the reality is that most users don't understand it or just end up being frustrated by the time it takes them to figure out how to call the command.

Having said that, this topic is opinionated and I am sure the other side will argue for param sets with its early validation before actually running the command. Therefore I suggest we let a 3rd person like @JamesWTruher decide on this matter. I am happy with the rest of the PR.

@JamesWTruher
Copy link
Contributor

@rjmholt @bergmeister i'm generally in favor of adding parameter sets where they make sense and don't over complicate things. In this case we're going from 2 to only 4 so i don't believe it's a higher cognitive load. The benefit with parameter sets is that we can determine behavior via inspection rather than runtime, so tools like vscode provide a better experience.

@rjmholt rjmholt force-pushed the includesuppressions-parameter branch from c970e73 to 02ccd84 Compare August 3, 2021 22:15
@rjmholt
Copy link
Contributor Author

rjmholt commented Aug 4, 2021

@bergmeister @JamesWTruher need an approval to merge 🙂

@rjmholt rjmholt enabled auto-merge (squash) August 4, 2021 15:45
@rjmholt rjmholt merged commit d1942a1 into PowerShell:master Aug 4, 2021
@rjmholt rjmholt deleted the includesuppressions-parameter branch November 20, 2021 00:53
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.

4 participants