Skip to content

False positive for PSUseConsistentIndentation inside Visual Studio Code #1202

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

Closed
mrboring opened this issue Mar 30, 2019 · 9 comments
Closed

Comments

@mrboring
Copy link

Steps to reproduce

  • Open Visual Studio Code
  • Save the following code to a ps1 file. Note this is sample code cut down to the minimun to show the issue
function Test {
    param(
        [object[]] $Objects
    )

    $items = $Objects | Select-Object -First 1

    foreach ($item in $items) {
        # Test comment
        # Test comment line two
        # Test comment line three
        if (1 -eq 1) {
            $dummy2 = 'dummyValue'
        }
        # Test comment two
        else {
            $dummy2 = ($item |
                Where-Object field1 -like 'dummyValue' |
                Select-Object field1 |
                Sort-Object field1)[0].field2
        }
    }
}

Expected behavior

No green swigglies.

Actual behavior

Indentation issue 1

Indentation issue 3

From the PowerShell command prompt it works fine, and only reports the unused variable.

> Invoke-ScriptAnalyzer -Path '.\Indentation issue.ps1' | ft -a

RuleName                             Severity ScriptName            Line Message
--------                             -------- ----------            ---- -------
PSUseDeclaredVarsMoreThanAssignments Warning  Indentation issue.ps1 14   The variable 'dummy2' is assigned but never used.

If I move the Select-Object statement, on line 7, to a new line:

Indentation issue 2

image

It introduces a new false positive for lines 20-22.

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.2.0
PSEdition                      Core
GitCommitId                    6.2.0
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.18.0
### VSCode version: 1.32.3 a3db5be9b5c6ba46bb7555ec5d60178ecc2eaae4 x64

### VSCode extensions:
alefragnani.Bookmarks@10.4.1
eamodio.gitlens@9.5.1
Ionide.Ionide-fsharp@3.34.0
Ionide.Ionide-Paket@1.12.0
ms-vscode.csharp@1.18.0
ms-vscode.powershell@1.12.0
ms-vscode.powershell-preview@2.0.1
streetsidesoftware.code-spell-checker@1.6.10
vscode-icons-team.vscode-icons@8.4.0


### PSES version: 2.0.0.0

### PowerShell version:

Name                           Value
----                           -----
PSVersion                      6.2.0
PSEdition                      Core
GitCommitId                    6.2.0
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

I'm using the following VS Code PSScriptAnalyzerSettings.ps1 file:
PSScriptAnalyzerSettings.zip

From the terminal inside VS Code:

> get-module -ListAvailable PSScriptAnalyzer


    Directory: C:\Users\<REMOVED>\Documents\PowerShell\Modules

ModuleType Version    Name                                PSEdition ExportedCommands
---------- -------    ----                                --------- ----------------
Script     1.18.0     PSScriptAnalyzer                    Desk      {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer, Invoke-Formatter}

    Directory: C:\Users\<REMOVED>\.vscode\extensions\ms-vscode.powershell-preview-2.0.1\modules

ModuleType Version    Name                                PSEdition ExportedCommands
---------- -------    ----                                --------- ----------------
Script     1.18.0     PSScriptAnalyzer                    Desk      {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer, Invoke-Formatter}

From the PowerShell command prompt:

> get-module -ListAvailable PSScriptAnalyzer


    Directory: C:\Users\<REMOVED>\Documents\PowerShell\Modules

ModuleType Version    Name                PSEdition ExportedCommands
---------- -------    ----                --------- ----------------
Script     1.18.0     PSScriptAnalyzer    Desk      {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer, Invoke-Formatter}
@bergmeister
Copy link
Collaborator

bergmeister commented Mar 31, 2019

Hi, thanks for the very detailed info (that was definitely necessary due to the recent releases of the extension and PSSA).
I cannot repro your first issue with the squiggles in the foreach block or the squiggle that you get on line 8 when you add a newline in line 7. But in both cases I get the squiggles around lines 20-22 like you. If you format those lines as follows, then it goes away:

            $dummy2 = ($item |
                    Where-Object field1 -like 'dummyValue' |
                    Select-Object field1 |
                    Sort-Object field1)[0].field2

Not related to your problem specifically, but be aware that PSSA 1.18.0 added the new PSUseConsistentIndentation configuration setting PipelineIndentation that unfortunately introduced bug #1187 when using its default settings. The new update of the VS-Code extension (which you have) works around this bug by setting the default of this new setting to NoIndentation. Since you use a PSSA Settings file, I suggest you add this option to your settings file as well and the vscode setting powershell.codeFormatting.pipelineIndentationStyle as well.

@mrboring
Copy link
Author

@bergmeister Thanks for the quick response.

As you suggested, I set:

PSScriptAnalyzerSettings.psd1:

'PSUseConsistentIndentation' = @{
	'Enable'              = $true
	'IndentationSize'     = 4
	'PipelineIndentation' = 'IncreaseIndentationForFirstPipeline'
	'Kind'                = 'space'
}

I also commented out all other rules in the PSScriptAnalyzerSettings.psd1 file.

Settings:

Powershell › Code Formatting: Pipeline Indentation Style = IncreaseIndentationForFirstPipeline

Then I split line 7 after the vertical bar, and formatted the document. It formats without squigglies.

image

So, it seems like this PSSA rule (in VS Code) requires pipelined commands to be split over multiple lines. Do you think this could be the case?

@mrboring
Copy link
Author

@bergmeister Based on your information, I've figured out what is happening.

In my command prompt test I did not point to the PSScriptAnalyzerSettings.psd1 file, and that's why I got different results. When I repeated the test using the PSScriptAnalyzerSettings.psd1 file, I got the same results as VS Code.

As you stated, it's all about the PipelineIndentation indentation setting.

@mrboring mrboring reopened this Mar 31, 2019
@mrboring
Copy link
Author

@bergmeister I've re-opened the issue as I find that to stop swigglies I have to use multiple lines for all pipelined statements. This does not seem right. In my sample code, line 7 does not need splitting. I looked at other code I have and found the following line $_ | Format-List * -Force. Again, this does not need splitting over multiple lines.

Can you confirm if the PSUseConsistentIndentation should force pipelined statements to be split.

@bergmeister
Copy link
Collaborator

@mrboring

  • If you name your settings file PSScriptAnalyzerSettings.psd1, then Invoke-ScriptAnalyzer will pick it up implicitly if you invoke the command from the same working directory so that you don't have to specify it explicitly via the -Settings parameter
  • Yes, it is all about the PipelineIndentation setting that I recommend to set to NoIndentation due to the bug that I referenced that is present in 1.18.0 that will only be fixed in the next version. With this setting, single-line pipeline statements should not be affected.

@mrboring
Copy link
Author

mrboring commented Apr 1, 2019

@bergmeister Thanks. I've set NoIndentation.

@mrboring mrboring closed this as completed Apr 1, 2019
@mrboring
Copy link
Author

@bergmeister A new version of vscode-powershell (v2019.5.0-preview) has been released. Is this issue fixed in this release?

@bergmeister
Copy link
Collaborator

No, PSSA has not released 1.18.1 yet but hopefully in the coming weeks. From the time we release you will be able to just install the newer version, which will override the one shipped with the vscode extension until the vscode extension ships a newer version.
For the moment you can just build the development branch yourself and install it locally. If you want I can give you pre-built binaries, they just won't be signed.

@mrboring
Copy link
Author

@bergmeister Thanks for the quick response. I'll wait for the release of the new version of PSSA.

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

No branches or pull requests

2 participants