GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add new MissingTryBlock rule by iRon7 · Pull Request #2179 · PowerShell/PSScriptAnalyzer

Conversation

@iRon7

@iRon7 iRon7 commented Apr 22, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

PR Summary

Although #2098 didn't get an up-for-graps label, I did create the suggested Add new MissingTryBlock rule to warn when catch or finally blocks are not preceded by a try block because:

  • I think that a catch block that misses a try block is less valid than a try block that misses a catch block which causes an parse error by the engine
    • and therefore even deserves the severity Error
  • A missing try block might easily get unnoticed during design time but will very likely fail during runtime
  • It happened twice now that someone in our company tried to publish a script that was missing a try block

Closes #1700 and Fixes #2098

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Change is not breaking
  • Make sure all .cs, .ps1 and .psm1 files have the correct copyright header
  • Make sure you've added a new test if existing tests do not effectively test the code changed and/or updated documentation
  • This PR is ready to merge and is not Work in Progress.
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@iRon7
PowerShell#2098 Add new MissingTryBlock rule
1da47c4
@iRon7
iRon7 requested review from a team, Christoph Bergmeister (bergmeister), Mikey Lombardi (He/Him) (michaeltlombardi) and Sean Wheeler (sdwheeler) as code owners April 22, 2026 16:52
@iRon7 iRon7 changed the title #2098 Add new MissingTryBlock rule Add new MissingTryBlock rule Apr 22, 2026
@liamjpeters

Liam Peters (liamjpeters) commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

👋

This rule has a severity of Warning but is emitting a diagnostic of Error severity. Where this is the only diagnostic that your rule emits, these should match. I would suggest Warning - just because it's not the 99% use-case, doesn't mean I can't define a function called catch and have it accept a ScriptBlock if I wanted to.

This has the same copy-pasta as your other PR.

iRon7 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iRon7 iRon7 changed the title Add new MissingTryBlock rule WIP: Add new MissingTryBlock rule Apr 28, 2026
@iRon7
Changed severity level of MissingTryBlock rule from Error to Warning …
e9a2365
…in the rule definition and resolved copy-pasta in the rule description.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings April 29, 2026 14:49
Copilot started reviewing on behalf of iRon7 April 29, 2026 14:51
View session
@iRon7
Updated tests for MissingTryBlock rule to reflect severity change fro…
50f751f
…m Error to Warning.
@iRon7 iRon7 changed the title WIP: Add new MissingTryBlock rule Add new MissingTryBlock rule Apr 29, 2026
Copilot AI reviewed Apr 29, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

Pull request overview

Adds a new built-in PSScriptAnalyzer rule to detect catch/finally blocks used without a preceding try, with accompanying documentation and Pester tests to validate behavior.

Changes:

  • Introduces the new MissingTryBlock built-in rule implementation and localized strings.
  • Adds rule documentation and registers it in the rules documentation index.
  • Adds a dedicated Pester test suite for violation, compliant, and suppression scenarios.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
docs/Rules/README.md Adds MissingTryBlock to the published rules index table.
docs/Rules/MissingTryBlock.md New rule documentation page describing intent, guidance, and examples.
Tests/Rules/MissingTryBlock.tests.ps1 New Pester coverage for violations, compliant cases, and suppression behavior.
Rules/Strings.resx Adds name/common-name/description/error message resources for the new rule.
Rules/MissingTryBlock.cs Implements AST-based detection and emits diagnostics for missing try before catch/finally.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Tests/Rules/MissingTryBlock.tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread docs/Rules/MissingTryBlock.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/Strings.resx Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/MissingTryBlock.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/MissingTryBlock.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/MissingTryBlock.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

iRon7 and others added 6 commits April 29, 2026 17:37
@iRon7
Update Rules/MissingTryBlock.cs
79e80aa
Co-authored-by: Copilot <[email protected]>
@iRon7
Grammar: “which is likely a mistake and result in … error” is ungramm…
47507bb
…atical.
@iRon7
Merge branch '#2098MissingTryBlock' of https://github.com/iRon7/PSScr…
449fdd1
…iptAnalyzer into #2098MissingTryBlock
@iRon7
Update Rules/Strings.resx
e1a8d3f
Co-authored-by: Copilot <[email protected]>
@iRon7
Update Rules/MissingTryBlock.cs
a420820
Co-authored-by: Copilot <[email protected]>
@iRon7
minor but worthwhile
216645d
andyleejordan
Andy Jordan (andyleejordan) suggested changes May 5, 2026
View reviewed changes

@andyleejordan Andy Jordan (andyleejordan) left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

Thanks again iRon7 for working through this with Liam Peters (@liamjpeters). The severity mismatch is fixed and the implementation is tight (using StringConstantExpressionAst as the bareword detection is the right approach for this scenario).

One context note that may be useful: this rule was explicitly endorsed upstream in PowerShell/PowerShell#25491 by the engine working group as something that belongs in PSSA rather than the parser. Good signal that there's appetite for landing it.

Three things before I'm comfortable merging:

The // Find all FunctionDefinitionAst in the Ast comment. GitHub's review bot flagged this and you replied "Outdated?" — but the comment is still in Rules/MissingTryBlock.cs on the current head and it's misleading: the code is finding StringConstantExpressionAst, not FunctionDefinitionAst. Could you replace it with something like // Find bareword "catch" or "finally" tokens that are not part of a TryStatementAst?

Opt-in by default. Same convention point as the other rules: please derive from ConfigurableRule with Enable = false so users can opt in, and update docs/Rules/README.md.

Doc the false-positive. This rule will fire on user code like:

function catch { param([scriptblock]$body) & $body }
catch { Write-Host 'hi' }

which is legal PowerShell (catch is not a reserved word at the command position). Worth a short note in docs/Rules/MissingTryBlock.md so users know they may need to suppress in unusual cases. Doesn't change the implementation.

Once those are sorted, I'd like Liam Peters (@liamjpeters) to take a quick second look since he did the bulk of the original review.

Drafted by Copilot (Claude Opus 4.7)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
sdwheeler
Sean Wheeler (sdwheeler) previously requested changes May 6, 2026
View reviewed changes
Comment thread docs/Rules/MissingTryBlock.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

@iRon7 @sdwheeler
Update docs/Rules/MissingTryBlock.md
f53cc38
Co-authored-by: Sean Wheeler <[email protected]>
bergmeister
Christoph Bergmeister (bergmeister) approved these changes May 7, 2026
View reviewed changes

@bergmeister Christoph Bergmeister (bergmeister) left a comment

Copy link
Copy Markdown
Collaborator

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.

Looks good to me but agree to fix things from previous comment

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
iRon7 added 2 commits May 12, 2026 13:52
@iRon7
Merge branch '#2098MissingTryBlock' of https://github.com/iRon7/PSScr…
7c22919
…iptAnalyzer into #2098MissingTryBlock
@iRon7
Applied feedback from @andrewconnell to add a note about the rule not…
4b94c24
… being enabled by default, and to add a note about potential false positives with functions named "catch" or "finally". Also added a test context for when the rule is disabled. Updated the rule implementation to inherit from ConfigurableRule and set Enable to false in the constructor. Updated the AnalyzeScript method to be an override, and added overrides for GetCommonName, GetDescription, GetName, GetSeverity, and GetSourceName.
liamjpeters
Liam Peters (liamjpeters) reviewed May 15, 2026
View reviewed changes

@liamjpeters Liam Peters (liamjpeters) left a comment

Copy link
Copy Markdown
Contributor

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.

Just some small cosmetic/hygiene changes. The actual rule and test coverage is looking really good 👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread docs/Rules/MissingTryBlock.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Tests/Rules/MissingTryBlock.tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/MissingTryBlock.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/MissingTryBlock.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@liamjpeters

Liam Peters (liamjpeters) commented May 15, 2026

Copy link
Copy Markdown
Contributor

iRon7 - You could update your PR description to include the issues it resolves so they're auto-resolved when your PR is merged.

You can make links using comments in the PR description such as:

Closes #1700 and Fixes #2098

iRon7 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iRon7 and others added 2 commits May 15, 2026 19:05
@iRon7 @liamjpeters
Update docs/Rules/MissingTryBlock.md
52598a1
Co-authored-by: Liam Peters <[email protected]>
@iRon7 @liamjpeters
Update Tests/Rules/MissingTryBlock.tests.ps1
9dc55e2
Co-authored-by: Liam Peters <[email protected]>
iRon7 and others added 2 commits May 15, 2026 19:06
@iRon7 @liamjpeters
Update Rules/MissingTryBlock.cs
cee9c87
Co-authored-by: Liam Peters <[email protected]>
@iRon7 @liamjpeters
Update Rules/MissingTryBlock.cs
48ab58a
Co-authored-by: Liam Peters <[email protected]>
@iRon7

iRon7 commented May 15, 2026

Copy link
Copy Markdown
Contributor Author

Liam Peters (@liamjpeters),

Thanks again for your comments, I have tested and committed them.

Closes #1700 and Fixes #2098

Added to the PR description (I wasn't even aware of the duplicate 1700)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

andyleejordan
Andy Jordan (andyleejordan) approved these changes May 15, 2026
View reviewed changes

@andyleejordan Andy Jordan (andyleejordan) left a comment

Copy link
Copy Markdown
Member

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.

Thanks iRon7 — and thanks Liam Peters (@liamjpeters) for the careful second pass. All five of Liam's 2026-05-15 line-level asks landed at the new head: the PSMissingTryBlock = @{ settings example, the "Doesn't emit a violation" rename, the duplicate <summary> block above the class, the dead helper, and Closes #1700 and Fixes #2098 in the PR body.

CI is green on Linux, macOS, and Windows. Approving.

Drafted by Copilot (Claude Opus 4.7)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@andyleejordan
Andy Jordan (andyleejordan) dismissed Sean Wheeler (sdwheeler)’s stale review May 15, 2026 18:05

I believe they've been addressed.

@andyleejordan
Andy Jordan (andyleejordan) merged commit 97f30d6 into PowerShell:main May 15, 2026
4 checks passed

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
@bergmeister Christoph Bergmeister (bergmeister) bergmeister approved these changes
@michaeltlombardi Mikey Lombardi (He/Him) (michaeltlombardi) Awaiting requested review from michaeltlombardi michaeltlombardi is a code owner
+3 more reviewers
@liamjpeters Liam Peters (liamjpeters) liamjpeters left review comments
@sdwheeler Sean Wheeler (sdwheeler) sdwheeler left review comments
@andyleejordan Andy Jordan (andyleejordan) andyleejordan approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Rule request: The Catch (or Finally) statement is missing its Try block. Missing try{} block not reported as a problem

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@iRon7 @liamjpeters @andyleejordan @bergmeister @sdwheeler
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.