Title: PSUseConsistentIndentation double-indents attribute bodies that open a scriptblock (`[Attr({ … })]`) · Issue #2216 · PowerShell/PSScriptAnalyzer · GitHub
Open Graph Title: PSUseConsistentIndentation double-indents attribute bodies that open a scriptblock (`[Attr({ … })]`) · Issue #2216 · PowerShell/PSScriptAnalyzer
X Title: PSUseConsistentIndentation double-indents attribute bodies that open a scriptblock (`[Attr({ … })]`) · Issue #2216 · PowerShell/PSScriptAnalyzer
Description: Prerequisites I have read the documentation and my issue is not covered there. I have searched the existing issues and my issue is not already reported. Summary PSUseConsistentIndentation (and therefore Invoke-Formatter) double-indents t...
Open Graph Description: Prerequisites I have read the documentation and my issue is not covered there. I have searched the existing issues and my issue is not already reported. Summary PSUseConsistentIndentation (and ther...
X Description: Prerequisites I have read the documentation and my issue is not covered there. I have searched the existing issues and my issue is not already reported. Summary PSUseConsistentIndentation (and ther...
Opengraph URL: https://github.com/PowerShell/PSScriptAnalyzer/issues/2216
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PSUseConsistentIndentation double-indents attribute bodies that open a scriptblock (`[Attr({ … })]`)","articleBody":"### Prerequisites\n\n- [x] I have read the [documentation](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/overview) and my issue is not covered there.\n- [x] I have searched the [existing issues](https://github.com/PowerShell/PSScriptAnalyzer/issues) and my issue is not already reported.\n\n### Summary\n\n`PSUseConsistentIndentation` (and therefore `Invoke-Formatter`) double-indents the body of an **attribute that opens a scriptblock**, e.g. `[ArgumentCompleter({...})]`. The body gets `IndentationSize * 2` and the closing `})]` gets `IndentationSize * 1`.\n\nThis looks like the same root cause as #2159 (hashtable inside a method call: the `LParen` and the `AtCurly` each add an indentation level). Here the two adjacent openers are `(` and `{` from a **type/attribute literal** instead of `@{`, so `[AttributeName({` hits it too. #2159 was fixed on `main` by #2173 (\"only the last unclosed opener on a line affects indentation\"), but neither that fix nor #2159 covers the attribute form, and it is still broken in the latest release **1.25.0**.\n\n### Steps to reproduce\n\n```powershell\n$code = @'\n[ArgumentCompleter({\n\tParam($commandName, $parameterName)\n\t$validKeys = @('a', 'b')\n\t$validKeys | Where-Object { $_ } | ForEach-Object { \"$_=\" }\n})]\n'@\n\n$settings = @{\n\tIncludeRules = @('PSUseConsistentIndentation')\n\tRules = @{\n\t\tPSUseConsistentIndentation = @{\n\t\t\tEnable = $true\n\t\t\tIndentationSize = 4\n\t\t\tKind = 'tab'\n\t\t}\n\t}\n}\n\nInvoke-Formatter -ScriptDefinition $code -Settings $settings\n```\n\n### Expected behavior\n\nOne level of indentation for the body, matching the opener line, and the closing `})]` at the opener's level:\n\n```powershell\n[ArgumentCompleter({\n\tParam($commandName, $parameterName)\n\t$validKeys = @('a', 'b')\n\t$validKeys | Where-Object { $_ } | ForEach-Object { \"$_=\" }\n})]\n```\n\n### Actual behavior\n\nTwo levels for the body and one level for the closing `})]`:\n\n```powershell\n[ArgumentCompleter({\n\t\tParam($commandName, $parameterName)\n\t\t$validKeys = @('a', 'b')\n\t\t$validKeys | Where-Object { $_ } | ForEach-Object { \"$_=\" }\n\t})]\n```\n\nThe same happens with spaces (`Kind = 'space'`, `IndentationSize = 4`) and with `NewLineAfterOpenBrace`/`PSPlaceOpenBrace` not involved at all — `PSUseConsistentIndentation` alone is enough.\n\nIt also affects other attribute forms that open a scriptblock, e.g.\n\n```powershell\n[ValidateScript({\n\t$_ -gt 0\n})]\n```\n\nand the buggy output is **not idempotent in a harmless way**: because the opener line itself is not re-indented, re-running the formatter keeps the body at the wrong level, and any tool that re-applies formatting sees a permanent diff.\n\n### Environment\n\n```\nPSVersion: 7.6.6\nPSEdition: Core\nOS: Windows 11 (26100)\nPSScriptAnalyzer: 1.25.0\n```\n\nAlso reproduced with PSScriptAnalyzer 1.24.0 (bundled with ms-vscode.powershell 2025.4.0) and with Windows PowerShell 5.1 / 26100 (Desktop edition), so it is not host- or edition-specific.\n\n### Related\n\n- #2159 — `PSUseConsistentIndentation`: Hashtable inside method call gets double-indented (same `LParen`+`XCurly` double count; fixed by #2173)\n- #2173 — the fix that pops the level when an opener is not the last unclosed opener on the line; it does not catch the attribute opening `({`\n- #1168 — \"Formatting `.where` and `.foreach` methods is incorrect\" (adjacent-opener family)\n","author":{"url":"https://github.com/steve02081504","@type":"Person","name":"steve02081504"},"datePublished":"2026-09-18T04:10:30Z","url":"https://github.com/PowerShell/PSScriptAnalyzer/issues/2216"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6b810492-a09f-499b-50be-09d2acbd6de3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D942:13EF97:1EA5B6:2A847F:6AFCE209 |
| html-safe-nonce | 311deccb15b8d802f84df73ecbfa35414d99e1158d091e0106fce4674856b530 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOTQyOjEzRUY5NzoxRUE1QjY6MkE4NDdGOjZBRkNFMjA5IiwidmlzaXRvcl9pZCI6IjU2ODY0NDgxMjY1ODg0MTI0MjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f7b3d8fdd29de3cd2bba3e9f876c76ad10bfe0833d29a321263d381cb6ff6ecd |
| hovercard-subject-tag | issue:5495552467 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PSScriptAnalyzer/2216/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4569e930dc015e7be965fb76b9625f0ecdb6c99c4073da01657d2b956290d8cb/PowerShell/PSScriptAnalyzer/issues/2216 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4569e930dc015e7be965fb76b9625f0ecdb6c99c4073da01657d2b956290d8cb/PowerShell/PSScriptAnalyzer/issues/2216 |
| og:image:alt | Prerequisites I have read the documentation and my issue is not covered there. I have searched the existing issues and my issue is not already reported. Summary PSUseConsistentIndentation (and ther... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | steve02081504 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0c05b7eb5b95e29725d01bb1f560a020e3fafd0f033c6b9ed3918fa3161254dd |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PSScriptAnalyzer git https://github.com/PowerShell/PSScriptAnalyzer.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 33149177 |
| octolytics-dimension-repository_nwo | PowerShell/PSScriptAnalyzer |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 33149177 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PSScriptAnalyzer |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 79d9154e55368884c8c93766ba4fee076343d37f |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width