Vscode-powershell: [Preview-2020-5.0] Formatting broken when using default PipelineIndentationStyle setting value of None

Created on 13 May 2020  路  41Comments  路  Source: PowerShell/vscode-powershell

Issue Description

I am experiencing a problem with... formatting. Pressing shortcuts or choosing from menu doesn't work. F8 / F5 works fine. Intellisense works as well.

1589385610-e162f223-5ab6-4d60-bdbb-aa205fe17f2f1589385348230.zip

Attached Logs

Follow the instructions in the README about
capturing and sending logs.

Environment Information

Visual Studio Code

| Name | Version |
| --- | --- |
| Operating System | Windows_NT x64 10.0.18363 |
| VSCode | 1.45.0|
| PowerShell Extension Version | 2020.5.0 |

PowerShell Information

|Name|Value|
|---|---|
|PSVersion|5.1.18362.752|
|PSEdition|Desktop|
|PSCompatibleVersions|1.0 2.0 3.0 4.0 5.0 5.1.18362.752|
|BuildVersion|10.0.18362.752|
|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|
|---|---|---|
|beautify|HookyQR|1.5.0|
|bracket-pair-colorizer-2|CoenraadS|0.1.3|
|code-settings-sync|Shan|3.4.3|
|excel-to-markdown-table|csholmq|1.2.2|
|gistfs|vsls-contrib|0.0.67|
|gitdoc|vsls-contrib|0.0.6|
|github-linker|gimenete|0.2.3|
|gitlens|eamodio|10.2.1|
|html-preview-vscode|george-alisson|0.2.5|
|markdown-all-in-one|yzhang|2.8.0|
|markdown-preview-enhanced|shd101wyy|0.5.3|
|markdown-shortcuts|mdickin|0.12.0|
|material-icon-theme|PKief|4.1.0|
|pdf|tomoki1207|0.6.0|
|powershell-preview|ms-vscode|2020.5.0|
|project-manager|alefragnani|10.12.0|
|rainbow-csv|mechatroner|1.7.0|
|reg|ionutvmi|1.0.1|
|vscode-html-css|ecmel|0.2.3|
|vscode-markdownlint|DavidAnson|0.35.1|
|vscode-pull-request-github|GitHub|0.16.0|
|vscode-toggle-quotes|BriteSnow|0.3.2|
|vscode-versionlens|pflannery|0.27.0|
|vscode-wakatime|WakaTime|4.0.0|
|vscode-yaml|redhat|0.8.0|
|xml|DotJoshJohnson|2.5.0|

Most helpful comment

Done

All 41 comments

hmm @bergmeister looks like these are the settings used:

{
    "IncludeRules": [
        "PSPlaceCloseBrace",
        "PSPlaceOpenBrace",
        "PSUseConsistentWhitespace",
        "PSUseConsistentIndentation",
        "PSAlignAssignmentStatement"
    ],
    "Rules": {
        "PSUseConsistentIndentation": {
            "IndentationSize": 4,
            "Enable": true,
            "PipelineIndentation": 3,
            "Kind": "space"
        },
        "PSAvoidUsingCmdletAliases": {},
        "PSPlaceOpenBrace": {
            "Enable": true,
            "NewLineAfter": true,
            "OnSameLine": true,
            "IgnoreOneLineBlock": true
        },
        "PSAlignAssignmentStatement": {
            "Enable": true,
            "CheckHashtable": true
        },
        "PSUseCorrectCasing": {
            "Enable": true
        },
        "PSPlaceCloseBrace": {
            "Enable": true,
            "NewLineAfter": false,
            "IgnoreOneLineBlock": true
        },
        "PSUseConsistentWhitespace": {
            "CheckPipe": true,
            "CheckSeparator": true,
            "CheckOpenBrace": true,
            "Enable": true,
            "CheckParameter": false,
            "CheckOpenParen": true,
            "CheckPipeForRedundantWhitespace": false,
            "CheckInnerBrace": true,
            "CheckOperator": true
        }
    }
}

Does that look alright to you?

I updated the preview extension in my insider version of code and cannot repro. @PrzemyslawKlys Have you tried using the > Format Document command? Because I know some other extensions can override the shortcut for formatting a selection

No change - i tried shorcut, right click format, > format document. I'll uninstall extension and try fresh

Uninstall/install didn't help

I tried switching to PowerShell 5.1 locally to be closer to your environment but it still works... Also tried VS Code stable

@TylerLeonhardt the settings look to me.

Another option is to run PSScriptAnalyzer directly to see if anything changes:

$jsonRules = @'
{
    "IncludeRules": [
        "PSPlaceCloseBrace",
        "PSPlaceOpenBrace",
        "PSUseConsistentWhitespace",
        "PSUseConsistentIndentation",
        "PSAlignAssignmentStatement"
    ],
    "Rules": {
        "PSUseConsistentIndentation": {
            "IndentationSize": 4,
            "Enable": true,
            "PipelineIndentation": 3,
            "Kind": "space"
        },
        "PSAvoidUsingCmdletAliases": {},
        "PSPlaceOpenBrace": {
            "Enable": true,
            "NewLineAfter": true,
            "OnSameLine": true,
            "IgnoreOneLineBlock": true
        },
        "PSAlignAssignmentStatement": {
            "Enable": true,
            "CheckHashtable": true
        },
        "PSUseCorrectCasing": {
            "Enable": true
        },
        "PSPlaceCloseBrace": {
            "Enable": true,
            "NewLineAfter": false,
            "IgnoreOneLineBlock": true
        },
        "PSUseConsistentWhitespace": {
            "CheckPipe": true,
            "CheckSeparator": true,
            "CheckOpenBrace": true,
            "Enable": true,
            "CheckParameter": false,
            "CheckOpenParen": true,
            "CheckPipeForRedundantWhitespace": false,
            "CheckInnerBrace": true,
            "CheckOperator": true
        }
    }
}
'@

$scriptContent = Get-Content -Raw ./path/to/file.ps1
Invoke-Formatter -ScriptDefinition $scriptContent -Settings ($jsonRules | ConvertFrom-Json -AsHashtable)

This actually gets me Invoke-Formatter: Cannot parse settings. Will abort the invocation. So either the settings aren't correct or (more likely) I did it wrong.

Ok, i've it reproduced - only part of formatting doesn't work...

Formatting

It doesn't seem to touch things on the left side.. notice the Action on 11th line

@TylerLeonhardt Running this exact repro script of you, would also throw a settings parser error in 1.18.3, so does not seem to be a regression

@bergmeister sorry I was side-chaining 馃槄 that was more of a "Chris pls help fix my script to do what I want" than "PSSA is broken"

@TylerLeonhardt No worries, something is wrong in the format of how you convert the JSON because the following (manually converted) works fine:

Invoke-Formatter -ScriptDefinition 'gci' -Settings @{
    "IncludeRules" = @(
        "PSPlaceCloseBrace",
        "PSPlaceOpenBrace",
        "PSUseConsistentWhitespace",
        "PSUseConsistentIndentation",
        "PSAlignAssignmentStatement"
    );
    "Rules"        = @{
        "PSAvoidUsingCmdletAliases"  = @{}
        "PSUseConsistentIndentation" = @{
            "IndentationSize"     = 4;
            "Enable"              = $true;
            "PipelineIndentation" = 3;
            "Kind"                = "space"
        };
        "PSPlaceOpenBrace"           = @{
            "Enable"             = $true;
            "NewLineAfter"       = $true;
            "OnSameLine"         = $true;
            "IgnoreOneLineBlock" = $true
        };
        "PSAlignAssignmentStatement" = @{
            "Enable"         = $true;
            "CheckHashtable" = $true
        };
        "PSUseCorrectCasing"         = @{
            "Enable" = $true
        };
        "PSPlaceCloseBrace"          = @{
            "Enable"             = $true;
            "NewLineAfter"       = $false;
            "IgnoreOneLineBlock" = $true
        };
        "PSUseConsistentWhitespace"  = @{
            "CheckPipe"                       = $true;
            "CheckSeparator"                  = $true;
            "CheckOpenBrace"                  = $true;
            "Enable"                          = $true;
            "CheckParameter"                  = $false;
            "CheckOpenParen"                  = $true;
            "CheckPipeForRedundantWhitespace" = $false;
            "CheckInnerBrace"                 = $true;
            "CheckOperator"                   = $true
        }
    }
}

@PrzemyslawKlys Formatting therefore seems to generally work. Please supply the script where the Action hashtable entry does not get corrected. Also: If you revert to the previous extension version and reload, does it still repro? Have you got PSSA 1.19.0 installed locally or do you just use the version that comes out of the box with the extension (both are the same so, no worries)

@bergmeister

Yes, I jumped the gun a bit because I just was working on a different file that didn't have hash. Generally last preview didn't have an issue and i have installed 1.19.0 before newest preview so not sure if it's related to PSScriptAnalyzer code, more like some configuration on VsCode part.

PS C:\Support\GitHub\GpoZaurr> get-module -ListAvailable PSScriptAnalyzer



    Directory: C:\Users\przemyslaw.klys\Documents\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.19.0     PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer, Invoke-Formatter}


    Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.18.3     PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer, Invoke-Formatter}
Script     1.18.2     PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer, Invoke-Formatter}
Script     1.18.1     PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer, Invoke-Formatter}


    Directory: C:\Users\przemyslaw.klys\.vscode\extensions\ms-vscode.powershell-preview-2020.5.0\modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.19.0     PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer, Invoke-Formatter}

Here's the code I am testing it with:

@{
Test  = 'dfsf'
    dffdf = 'dddff'
}

foreach ($Test in $Test1) {
foreach ($Test2 in TEst3) {

}
}

Code above replicates it. On PowerShell (not preview) this is translated to

@{
    Test  = 'dfsf'
    dffdf = 'dddff'
}

foreach ($Test in $Test1) {
    foreach ($Test2 in TEst3) {

    }
}

image

On preview

@{
Test      = 'dfsf'
    dffdf = 'dddff'
}

foreach ($Test in $Test1) {
foreach ($Test2 in TEst3) {

}
}

image

@PrzemyslawKlys Your example formats fine for me with the PowerShell preview. Please try just commenting out all your settings once as a test (Ctrl+K+C) to see if it is caused by a setting and if so, work your way through the list of settings that causes it. If you don't have any success, please post a list of all user and workspace setting you have.

@bergmeister The only problem is... I don't use custom settings except those available within VSCode. There's nothing to comment out. The settings that @TylerLeonhardt showed were not set by me.

{
    "workbench.colorTheme": "Monokai",
    "workbench.iconTheme": "material-icon-theme",
    "powershell.codeFormatting.preset": "OTBS",
    "editor.fontSize": 12,
    "search.showLineNumbers": true,
    "terminal.integrated.scrollback": 30000,
    "debug.inlineValues": true,
    "editor.minimap.enabled": false,
    "workbench.useExperimentalGridLayout": true,
    "breadcrumbs.enabled": true,
    "workbench.editor.highlightModifiedTabs": true,
    "window.title": "${folderName}${separator}${activeEditorShort}",
    "debug.openDebug": "neverOpen",
    "typescript.implementationsCodeLens.enabled": true,
    "powershell.developer.editorServicesLogLevel": "Diagnostic",
    "editor.snippetSuggestions": "none",
    "files.autoSave": "off",
    "files.autoSaveDelay": 5000,
    "explorer.confirmDragAndDrop": false,
    "files.trimTrailingWhitespace": true,
    "editor.renderWhitespace": "all",
    "gitlens.blame.ignoreWhitespace": true,
    "powershell.codeFormatting.useCorrectCasing": true,
    "[powershell]": {
        "editor.defaultFormatter": "ms-vscode.powershell-preview",
        /* "editor.wordSeparators": "`~!@#%^&*()=+[{]}\\|;:'\",.<>/?" */
    },
    "[markdown]": {
        "editor.defaultFormatter": "yzhang.markdown-all-in-one"
    },
    "files.encoding": "utf8bom",
    "extensions.autoUpdate": true,
    "window.zoomLevel": 0,
    "editor.suggest.localityBonus": true,
    "editor.suggest.maxVisibleSuggestions": 15,
    "search.maintainFileSearchCache": true,
    "extensions.showRecommendationsOnlyOnDemand": true,
    "terminal.integrated.experimentalRefreshOnResume": true,
    "html.format.indentInnerHtml": true,
    "[html]": {
        "editor.defaultFormatter": "HookyQR.beautify"
    },
    "[json]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    },
    "[jsonc]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    },
    "[javascript]": {
        "editor.defaultFormatter": "vscode.typescript-language-features"
    },
    "cSpell.enabledLanguageIds": [
        "asciidoc",
        "c",
        "cpp",
        "csharp",
        "css",
        "git-commit",
        "go",
        "handlebars",
        "html",
        "jade",
        "java",
        "javascript",
        "javascriptreact",
        "json",
        "jsonc",
        "latex",
        "less",
        "markdown",
        "php",
        "plaintext",
        "pug",
        "python",
        "restructuredtext",
        "rust",
        "scala",
        "scss",
        "text",
        "typescript",
        "typescriptreact",
        "yaml",
        "yml"
    ],
    "cSpell.userWords": [
        "K艂ys",
        "Przemys艂aw",
        "dashimo",
        "documentimo"
    ],
    "sqltools.telemetry": false,
    "mssql.copyIncludeHeaders": true,
    "peacock.favoriteColors": [
        {
            "name": "Angular Red",
            "value": "#b52e31"
        },
        {
            "name": "Auth0 Orange",
            "value": "#eb5424"
        },
        {
            "name": "Azure Blue",
            "value": "#007fff"
        },
        {
            "name": "C# Purple",
            "value": "#68217A"
        },
        {
            "name": "Gatsby Purple",
            "value": "#639"
        },
        {
            "name": "Go Cyan",
            "value": "#5dc9e2"
        },
        {
            "name": "Java Blue-Gray",
            "value": "#557c9b"
        },
        {
            "name": "JavaScript Yellow",
            "value": "#f9e64f"
        },
        {
            "name": "Mandalorian Blue",
            "value": "#1857a4"
        },
        {
            "name": "Node Green",
            "value": "#215732"
        },
        {
            "name": "React Blue",
            "value": "#00b3e6"
        },
        {
            "name": "Something Different",
            "value": "#832561"
        },
        {
            "name": "Vue Green",
            "value": "#42b883"
        }
    ],
    "editor.smoothScrolling": true,
    "editor.tabCompletion": "on",
    "html.format.wrapLineLength": 0,
    "powershell.codeFormatting.autoCorrectAliases": true,
    "editor.detectIndentation": false,
    "editor.renderControlCharacters": false,
    "editor.suggest.snippetsPreventQuickSuggestions": false,
    "terminal.integrated.rendererType": "experimentalWebgl",
    "powershell.powerShellDefaultVersion": "Windows PowerShell (x64)",
    "editor.fontFamily": "Fira Code Retina, Consolas, Monaco, 'Courier New', monospace",
    //"editor.fontFamily": "Fira Code",
    "editor.fontLigatures": false,
    "workbench.colorCustomizations": {
        //https://code.visualstudio.com/api/references/theme-color
        //"titleBar.activeBackground": "#ff0000"
        //"statusBar.background": "#ff0000"
        "statusBar.debuggingBackground": "#FF7F00"
    },
    "powershell.integratedConsole.suppressStartupBanner": true,
    "explorer.compactFolders": false,
    "grammarly.userWords": [
        "ADEssentials"
    ],
    "editor.codeActionsOnSave": {
        "source.fixAll.markdownlint": true
    },
    "markdownlint.config": {
        "MD033": false
    },
    "timeline.showView": true,
    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
    "javascript.updateImportsOnFileMove.enabled": "always",
    "powershell.integratedConsole.forceClearScrollbackBuffer": true,
    "git.enableCommitSigning": true
}
    "powershell.codeFormatting.preset": "OTBS",
    "powershell.codeFormatting.useCorrectCasing": true,
    "powershell.codeFormatting.autoCorrectAliases": true,
    "[powershell]": {
        "editor.defaultFormatter": "ms-vscode.powershell-preview",
    },

Here are the PowerShell formatting settings @bergmeister (I'm pretty sure you can delete that last one... I don't think it really does anything for you?)

@PrzemyslawKlys I tried your settings and still cannot repro. One more suggestion would be to try to disable all extensions except the PowerShell extension. The settings that Tyler showed you are a low level detail, which is a result of the combination of powershell.codeFormatting.* settings.

I've disabled everything except PowerShell Preview. No change.

@TylerLeonhardt With your settings, I can reproduce that the formatter doesn't do anything, however, the powershell.* of @PrzemyslawKlys are (and for those it does not repro)

{
    "powershell.codeFormatting.preset": "OTBS",
    "powershell.developer.editorServicesLogLevel": "Diagnostic",
    "powershell.codeFormatting.useCorrectCasing": true,
    "[powershell]": {
        "editor.defaultFormatter": "ms-vscode.powershell-preview",
    },
    "powershell.codeFormatting.autoCorrectAliases": true,
    "powershell.powerShellDefaultVersion": "Windows PowerShell (x64)",
    "powershell.integratedConsole.suppressStartupBanner": true,
    "powershell.integratedConsole.forceClearScrollbackBuffer": true
}

@PrzemyslawKlys you don't have any workspace level settings do you?

That would be in a .vscode folder in the folder you have open in VS Code.

@TylerLeonhardt Not using any workspace settings. Since PowerShell doesn't really support multiple workspaces for formatting I don't use that.

Just to add, non-preview extension just works. So my settings are not impacting standard extension.

I'm happy to share screen (teamviewer) or teams or anything you wish to do.

@PrzemyslawKlys I can now sometimes repro, but behaviour is not consistent, give me a bit more time in the meantime. P.S. In your repro example, you are missing a dollar before TEst3, I guess that was just a typo and not in your real script?

ye, typo during typing ;) Doesn't change if it's dollar or not for formatting.

I can reproduce now, it was actually one of my settings that caused the bug to not occur. The culprit seems to be powershell.codeFormatting.pipelineIndentationStyle. When no value is used, the new default (None) applies. Setting it to any other setting makes the problem go away. I suggest to set it to the previous default NoIndentation for the moment. This new value and default was actually meant to have less issues by default...

Yep, setting it to NoIndentation works :-)

I found another bug - when you enable

image

Import-Module "$PSScriptRoot\..\GPoZaurr.psd1" -Force

$Type = 'Test'

'Onet','Evotec' | Where-Object  {
    if ($Type -eq 'Test') {
        #$_
    }
}

is converted to:

Import-Module "$PSScriptRoot\-Force

$Type = 'Test'

'Onet', 'Evotec' | Where-Object {
    if ($Type -eq 'Test') {
        #$_
    }
}

So basically the first line looses " and couple of other chars breaking the script. If you can't reproduce it I also set my new settings to this:

    "powershell.codeFormatting.pipelineIndentationStyle": "NoIndentation",
    "powershell.codeFormatting.trimWhitespaceAroundPipe": true

The moment I add "powershell.codeFormatting.whitespaceBetweenParameters": true things go sideways

A simple repro for PSSA is. Thanks for your patience @PrzemyslawKlys . @TylerLeonhardt I will investigate the cause but a simple quick fix would be revert the default of powershell.codeFormatting.pipelineIndentationStyle back to NoIndentation.

Invoke-Formatter -Settings @{ IncludeRules = @('PSUseConsistentIndentation'); Rules = @{ PSUseConsistentIndentation = @{ Enable = $true; PipelineIndentation = 'None' } }  } -ScriptDefinition @'         
@{
      foo      = "dfsf"
    bar = "dddff"
}

    foreach ($Test in $Test1) {
    foreach ($Test2 in $TEst3) {

    }
    }
'@

I opened this PSSA issue for the new issue around whitespaceBetweenParameters: https://github.com/PowerShell/PSScriptAnalyzer/issues/1495
Please don't post any new issues in this issue but rather open a new one.

Will do. Sorry,

No worries, thanks for bringing up the issue and being patient with us.
@TylerLeonhardt I opened an issue in PSSA here: https://github.com/PowerShell/PSScriptAnalyzer/issues/1496
Do you want to re-release with a changed default or let me investigate more?

@bergmeister I've also noticed something: If you have an older version of PSScriptAnalyzer (1.18.* in my case), this is taking precedence and the assemblies for this are loading in the integrated terminal, rather than the "bundled" vscode one. This prevented for instance parameter casing "format document" from working correctly until I deleted my "modules" version of PSSA and reloading, then it worked fine.

You may want to make sure the module load search order prefers the vscode bundled one.

This is on a "fresh" load:
image

That's weird, I have multiple versions and only new one loads?

image

image

@PrzemyslawKlys try removing your 1.19.0 in your modules folder, I'm saying if you only have the older version still installed, that one takes precedence.

Even in your example, your modules folder one took precedence, I'm saying that the one in the powershell extensions folder bundled with vscode should take precedence even if it is older.

Sorry. You're correct.

image

I am not sure I agree that it should take precedence tho? Newest one should be used - I often install PSScriptAnalyzer newer than in extension?

@PrzemyslawKlys well ideally it would use an AssemblyLoadContext like the other "builtin" modules so that this is a moot point, but regardless I'd be OK either way as long as it's "whatever the latest version is". Currently it's "Whatever is in Modules takes precedence regardless"

@JustinGrote This should not happen, can you open a new issue for that one please? Do you know why that would be @TylerLeonhardt
This also happens with the previous version of the preview extension.

@PrzemyslawKlys or @TylerLeonhardt Can you please rename this issue to something like:
[Preview-2020-5.0] Formatting broken when using default PipelineIndentationStyle setting value of None

Done

Was this page helpful?
0 / 5 - 0 ratings