Skip to content

Set exit code of Invoke-ScriptAnalyzer -EnableExit to total number of diagnostics (#2054) #2055

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

Merged
merged 1 commit into from
Feb 10, 2025

Conversation

MatejKafka
Copy link
Contributor

@MatejKafka MatejKafka commented Feb 4, 2025

PR Summary

Fixes #2054 by setting the exit to the total number of emitted diagnostics instead of just the last processed file from the pipeline.

PR Checklist

@andyleejordan
Copy link
Member

Thanks @MatejKafka! I really like this. Could I ask you to do one small refactor? I checked and RunAnalysis() is returning an IEnumerable which is why you then called ToArray() (well, you could have also used LINQ's Count() extension method, but that's what we're trying to avoid). Instead, since the container of diagnostic records being returned is already a List could you change the return type of RunAnalysis to List? Then you can use the Count property instead of method, and continue to pass it to the next function that takes an IEnumerable (which List satisfies and won't cause any type changes) instead of changing it to take an array.

var diagnostics = new List<DiagnosticRecord>();

Copy link
Member

@andyleejordan andyleejordan left a comment

Choose a reason for hiding this comment

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

Ugh sorry GitHub UI see comment I just left in "Conversation" tab of PR. Thanks!

@MatejKafka
Copy link
Contributor Author

@andyleejordan Needed to make a few more changes in AnalyzeScriptDefinition, which returned an IEnumerable, which is why did not make this change in the first place, but agree it's the better option. :)

Btw, looking at the code a bit more, imo it would also make sense to move ReportSummary handling to EndProcessing – that way, it's consistent with the new EnableExit behavior, and I'd assume most users want the summary for the whole run, not for each piped file separately. Thoughts?

@andyleejordan
Copy link
Member

Btw, looking at the code a bit more, imo it would also make sense to move ReportSummary handling to EndProcessing – that way, it's consistent with the new EnableExit behavior, and I'd assume most users want the summary for the whole run, not for each piped file separately. Thoughts?

@bergmeister thoughts on this? I'm inclined to agree but it seems like a riskier change. Otherwise I'm good with this PR as-is but wanted to ping you before merging.

@andyleejordan
Copy link
Member

@MatejKafka I chatted with @SeeminglyScience and confirmed that wouldn't break things, so I'm all for adding that small refactor. It makes sense, then I can do a final review and merge this.

@MatejKafka
Copy link
Contributor Author

One more thing – currently, the number of violations reported by -ReportSummary does not include parse errors, which is imo a bit weird, especially when the exit code includes them. Would you agree to also including parse errors in the total number of violations and the "Error = ..." print to be consistent?

@MatejKafka
Copy link
Contributor Author

Updated with changes to -ReportSummary. I tend to make a lot of minor cleanup changes while editing code. If you're not happy about any of the changes, just ping me and I'll remove them from the PR. :)

@andyleejordan
Copy link
Member

Ah, yes, sorry. I misunderstood and that moving the report summary logic would be a change on the same order as the exit code change. Need to keep it very minimal.

@MatejKafka
Copy link
Contributor Author

Not sure I follow – are you referring to changing -ReportExit at all, or the extra changes on top of it?

@andyleejordan
Copy link
Member

All the extra changes on top of it. I'm good to merge just commit 04a0149. For the extra report summary changes proposed, let's do it another PR.

@MatejKafka
Copy link
Contributor Author

Makes sense, I'll move the follow-up commits to separate PRs, thanks. :)

Copy link
Member

@andyleejordan andyleejordan left a comment

Choose a reason for hiding this comment

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

Fantastic, thank you!

@andyleejordan andyleejordan enabled auto-merge (squash) February 10, 2025 23:36
@andyleejordan andyleejordan merged commit 0e46667 into PowerShell:main Feb 10, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invoke-ScriptAnalyzer -EnableExit sets exit code based on the last evaluated file with piped input
2 participants