Psscriptanalyzer: Multiline formatting does not indent correctly

Created on 12 Jul 2019  路  6Comments  路  Source: PowerShell/PSScriptAnalyzer

Issue Description

I am experiencing a problem with formatting of multiline commands. Particularly with comments in them. Given the following command, this is how Powershell formats it:

ls c:\ |
select -ExpandProperty name |
sort -Descending

If I put in backticks then the formatter notices:

ls c:\ | `
    select -ExpandProperty name | `
    sort -Descending `

which is what I would expect.
However, if I then put in comments I get:

ls c:\ | `
    #Then select the name
select -ExpandProperty name | `
    #Before sorting in descending order
sort -Descending `

Which doesn't look right to me.

Environment Information

Visual Studio Code

| Name | Version |
| --- | --- |
| Operating System | Windows_NT x64 6.1.7601 |
| VSCode | 1.36.1|
| PowerShell Extension Version | 2019.5.0 |

PowerShell Information

|Name|Value|
|---|---|
|PSVersion|5.1.14409.1018|
|PSEdition|Desktop|
|PSCompatibleVersions|1.0 2.0 3.0 4.0 5.0 5.1.14409.1018|
|BuildVersion|10.0.14409.1018|
|CLRVersion|4.0.30319.42000|
|WSManStackVersion|3.0|
|PSRemotingProtocolVersion|2.3|
|SerializationVersion|1.1.0.1|

Visual Studio Code Extensions

Visual Studio Code Extensions(Click to Expand)

|Extension|Author|Version|
|---|---|---|
|gitlens|eamodio|9.8.5|
|json-tools|eriklynd|1.0.2|
|powershell|ms-vscode|2019.5.0|
|printcode|nobuhito|3.0.0|
|qub-xml-vscode|qub|1.2.8|
|XMLLanguageSupport|IBM|0.0.1|
|zenburn|ryanolsonx|1.0.1|

Area - Formatter Issue - Bug

Most helpful comment

(This might be fixed in PSSA 1.18.1, which you can test with Install-Module -Scope CurrentUser PSScriptAnalyzer -- PSES should pick that up)

All 6 comments

(This might be fixed in PSSA 1.18.1, which you can test with Install-Module -Scope CurrentUser PSScriptAnalyzer -- PSES should pick that up)

I ran that in PowerShell and then restarted VSCode. Same issue.

I've attached a file with the code as formatted. (Renamed to .txt extension so that GitHub will allow upload)
FormattingTest.txt

Whilst I can confirm this still happens on the latest version, I'd argue it is not a valid use case because the backtick is not needed when using pipelines.
Your example can be minimized to this simple example that would cause incorrect formatting:

foo | `
# hello world
baz

However, not using backticks does not cause this malformatting:

foo |
# hello world
baz

What you observed is because ScriptAnalyzer increases the indentation per backtick and optionally for pipelines as well depending on the PipelineIndentation setting (which default to NoIndentation in VS-Code). Whilst backticks are controversial and usually recommended against, I would've closed this issue normally because backticks are not needed but whilst playing with it, I found one edge case where pipelines are not used (i.e. backticks are needed), where it only indents the comment but not the line below, which should be fixed.

foo `
#comment
-bar

Ah! The problem was that I wasn't aware of the PipelineIndentation setting! Now that I am, it does what I expect, other than your final case. (Which is the same issue as my final case, I think)

Thank you!

Any idea why pipelines don't default to "IncreaseIndentationForFirstPipeline", which is what I'd expect?

(i.e. a visual indication that the later lines are a continuation of the first one)

That's what the info at https://devblogs.microsoft.com/powershell/powershell-scriptanalyzer-version-1-18-0-released/ indicates it was going to do.

Edit: Never mind, found https://devblogs.microsoft.com/powershell/release-of-powershell-script-analyzer-1-18-1/

The new PipelineIndentation feature was released in 1.18.0 and the default setting of PSScriptAnalyzer itself is IncreaseIndentationForFirstPipeline. However, after the release, a few edge cases were reported where the behaviour is not correct yet. Therefore we changed the VS-Code PowerShell extension to default to NoIndentation to make the bugs in those edge cases not surface in the meantime. The majority of edge cases were fixed in 1.18.1 but we found another edge case after 1.18.1 (and this one), therefore we expect that only starting with 1.18.2 the feature will be mature enough to start making it a default for VS-Code users.

Was this page helpful?
0 / 5 - 0 ratings