PowerShell / vscode-powershell

Provides PowerShell language and debugging support for Visual Studio Code
https://marketplace.visualstudio.com/items/ms-vscode.PowerShell
MIT License
1.71k stars 491 forks source link

Formatting an It block with unbalanced spacing causes issues #3947

Open corbob opened 2 years ago

corbob commented 2 years ago

Prerequisites

Summary

With the code (note two spaces between ) and { on line 7):

It "should create applications in Intune (<_>)" -ForEach @(
    $Packages
    @{
        Name  = 'installpackage'
        Version = '1.0.0'
    }
)  {
    $Output.Lines | Should -Contain "Application created in Intune: $_ ($" -Because $Output.String
}

When selecting to Format Document, results in:

It
$Output.Lines | Should -Contain "Application created in Intune: $_ ($" -Because $Output.String
}

When updating the code to include two spaces between -ForEach and @(:

It "should create applications in Intune (<_>)" -ForEach  @(
    $Packages
    @{
        Name  = 'installpackage'
        Version = '1.0.0'
    }
)  {
    $Output.Lines | Should -Contain "Application created in Intune: $_ ($" -Because $Output.String
}

It correctly formats:

It "should create applications in Intune (<_>)" -ForEach @(
    $Packages
    @{
        Name    = 'installpackage'
        Version = '1.0.0'
    }
) {
    $Output.Lines | Should -Contain "Application created in Intune: $_ ($" -Because $Output.String
}

And here's where it gets weird... When changing to three or more spaces on line 7:

It "should create applications in Intune (<_>)" -ForEach @(
    $Packages
    @{
        Name  = 'installpackage'
        Version = '1.0.0'
    }
)   {
    $Output.Lines | Should -Contain "Application created in Intune: $_ ($" -Because $Output.String
}

It does not format at all. Until... there are 2 or more spaces between -ForEach and @{, then it formats correctly.

Compounding the issue, if you remove the It, the formatting works correctly.

"should create applications in Intune (<_>)" -ForEach @(
    $Packages
    @{
        Name  = 'installpackage'
        Version = '1.0.0'
    }
)  {
    $Output.Lines | Should -Contain "Application created in Intune: $_ ($" -Because $Output.String
}

Formats to:

"should create applications in Intune (<_>)" -ForEach @(
    $Packages
    @{
        Name    = 'installpackage'
        Version = '1.0.0'
    }
) {
    $Output.Lines | Should -Contain "Application created in Intune: $_ ($" -Because $Output.String
}

Finally: When run through Invoke-Formatter in the integrated terminal, formatting occurs as expected:

PS /Users/coryknox/git/chocolatey/ctk/16_engtasks_1510> Invoke-Formatter @'
>> It "should create applications in Intune (<_>)" -ForEach @(
>>     $Packages
>>     @{
>>         Name  = 'installpackage'
>>         Version = '1.0.0'
>>     }
>> )  {
>>     $Output.Lines | Should -Contain "Application created in Intune: $_ ($" -Because $Output.String
>> }
>> '@
It "should create applications in Intune (<_>)" -ForEach @(
    $Packages
    @{
        Name    = 'installpackage'
        Version = '1.0.0'
    }
) {
    $Output.Lines | Should -Contain "Application created in Intune: $_ ($" -Because $Output.String
}

PowerShell Version

Name                           Value
----                           -----
PSVersion                      7.2.3
PSEdition                      Core
GitCommitId                    7.2.3
OS                             Darwin 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visual Studio Code Version

1.67.0-insider
ed2619e11b68bec43b788486230b0c2afeb4672d
x64

Extension Version

ms-vscode.powershell@2021.12.0
ms-vscode.powershell-preview@2022.4.3
TylerLeonhardt.vscode-inline-values-powershell@0.0.5

Steps to Reproduce

See Summary above

Visuals

https://user-images.githubusercontent.com/30301021/166330615-e23be282-6aae-4f80-9384-76535ac081f1.mp4

Logs

No response

andyleejordan commented 2 years ago

That's strange, I'm not reproing with the Preview and pwsh 7.2.3 on macOS...

andyleejordan commented 2 years ago

What's the output of Get-Module Pester in the PSIC for you? I have version 5.2.2.

corbob commented 2 years ago

I have version 5.3.1... Interestingly it's still reproducing for me even with pester not imported into PSIC.

It also reproduces on both stable and preview extensions.

andyleejordan commented 2 years ago

It also reproduces on both stable and preview extensions.

Something weird is going on for sure.

andyleejordan commented 2 years ago

Try disabling TylerLeonhardt.vscode-inline-values-powershell? IDK.

andyleejordan commented 2 years ago

Well FWIW I updated to 5.3.3 and it still doesn't repro for me. Trying on Windows now, who knows.

andyleejordan commented 2 years ago

Nope, not there either.

andyleejordan commented 2 years ago

Hey! It repro'd with the installed PowerShell Preview, but not when I was loading under a dev environment. Interesting.

corbob commented 2 years ago

I'm not able to repro it on Windows with either version of Code, and on my Mac it's not reproducing on stable Code. It's looking like it's a stray setting somewhere because I just installed all the same extensions onto my Windows system and the issue doesn't repro like that either.

Given that it's only one of the 4 code environments on my system, and you can't reproduce it, I'll go ahead and close this issue. If I happen to find what's causing it I'll reopen it.

I was going to close it since I can't repro it outside of my one install, but if you're able to repro then maybe it's worth keeping open?

andyleejordan commented 2 years ago

Yeah leave it open...can you list all your extensions for me? Big difference with dev environment is that all other extensions are disabled. I don't believe that my settings are changed though, and I tried a dev build of an older version of the extension so as to rule that out too.

andyleejordan commented 2 years ago
> code --list-extensions
alefragnani.Bookmarks
bungcip.better-toml
DavidAnson.vscode-markdownlint
eamodio.gitlens
EditorConfig.EditorConfig
fabiospampinato.vscode-highlight
GitHub.copilot
GitHub.vscode-pull-request-github
josefpihrt-vscode.roslynator
ms-azure-devops.azure-pipelines
ms-dotnettools.csharp
ms-python.python
ms-python.vscode-pylance
ms-vscode.azure-account
ms-vscode.hexeditor
ms-vscode.powershell-preview
ms-vscode.vscode-typescript-tslint-plugin
pspester.pester-test
qcz.text-power-tools
redhat.vscode-xml
richie5um2.vscode-sort-json
rusnason
corbob commented 2 years ago
74th.monokai-charcoal-high-contrast@3.4.0
bungcip.better-toml@0.3.2
cake-build.cake-vscode@2.0.0
DavidAnson.vscode-markdownlint@0.47.0
eamodio.gitlens@12.0.6
EditorConfig.EditorConfig@0.16.4
esbenp.prettier-vscode@9.5.0
felipecaputo.git-project-manager@1.8.2
GitHub.github-vscode-theme@6.0.0
GitHub.vscode-pull-request-github@0.40.0
GrapeCity.gc-excelviewer@4.2.54
johnpapa.vscode-peacock@4.0.1
mhutchie.git-graph@1.30.0
ms-dotnettools.csharp@1.24.4
ms-dotnettools.dotnet-interactive-vscode@1.0.3227011
ms-toolsai.jupyter@2022.3.1000901801
ms-toolsai.jupyter-keymap@1.0.0
ms-toolsai.jupyter-renderers@1.0.6
ms-vscode-remote.remote-containers@0.233.0
ms-vscode.hexeditor@1.9.6
ms-vscode.powershell@2021.12.0
ms-vscode.powershell-preview@2022.4.3
ms-vscode.vscode-typescript-tslint-plugin@1.3.4
rebornix.ruby@0.28.1
redhat.vscode-yaml@1.7.0
shardulm94.trailing-spaces@0.3.1
shd101wyy.markdown-preview-enhanced@0.6.2
sinedied.vscode-windows-xp-theme@0.1.0
tomphilbin.gruvbox-themes@1.0.0
TylerLeonhardt.vscode-inline-values-powershell@0.0.5
vsls-contrib.gistfs@0.4.1
wingrunr21.vscode-ruby@0.28.0
yzhang.markdown-all-in-one@3.4.3

I installed them all with the PowerShell:


@('74th.monokai-charcoal-high-contrast@3.4.0','bungcip.better-toml@0.3.2','cake-build.cake-vscode@2.0.0','DavidAnson.vscode-markdownlint@0.47.0','eamodio.gitlens@12.0.6','EditorConfig.EditorConfig@0.16.4','esbenp.prettier-vscode@9.5.0','felipecaputo.git-project-manager@1.8.2','GitHub.github-vscode-theme@6.0.0','GitHub.vscode-pull-request-github@0.40.0','GrapeCity.gc-excelviewer@4.2.54','johnpapa.vscode-peacock@4.0.1','mhutchie.git-graph@1.30.0','ms-dotnettools.csharp@1.24.4','ms-dotnettools.dotnet-interactive-vscode@1.0.3227011','ms-toolsai.jupyter@2022.3.1000901801','ms-toolsai.jupyter-keymap@1.0.0','ms-toolsai.jupyter-renderers@1.0.6','ms-vscode-remote.remote-containers@0.233.0','ms-vscode.hexeditor@1.9.6','ms-vscode.powershell@2021.12.0','ms-vscode.powershell-preview@2022.4.3','ms-vscode.vscode-typescript-tslint-plugin@1.3.4','rebornix.ruby@0.28.1','redhat.vscode-yaml@1.7.0','shardulm94.trailing-spaces@0.3.1','shd101wyy.markdown-preview-enhanced@0.6.2','sinedied.vscode-windows-xp-theme@0.1.0','tomphilbin.gruvbox-themes@1.0.0','TylerLeonhardt.vscode-inline-values-powershell@0.0.5','vsls-contrib.gistfs@0.4.1','wingrunr21.vscode-ruby@0.28.0','yzhang.markdown-all-in-one@3.4.3') | % {
    code-insiders --install-extension ($_.Split('@')[0])
}
andyleejordan commented 2 years ago

Here are our common extensions:

bungcip.better-toml@0.3.2
DavidAnson.vscode-markdownlint@0.47.0
eamodio.gitlens@12.0.6
EditorConfig.EditorConfig@0.16.4
GitHub.vscode-pull-request-github@0.40.0
ms-dotnettools.csharp@1.24.4
ms-vscode.hexeditor@1.9.6
ms-vscode.powershell-preview@2022.4.3
ms-vscode.vscode-typescript-tslint-plugin@1.3.4
andyleejordan commented 2 years ago

Still repros with all those disabled...

ninmonkey commented 2 years ago

@andschwa and @corbob If you have a working case and test case -- check the log file, it prints the parameters that are sent -- ( In the past, there was a case where it didn't match user's settings in the UI/Config file )

I wrote a script to detect the correct log and grep it

Mine was reproducing it on ms-vscode.powershell-2022.5.1 with code.cmd . I was trying to determine which setting was the cause by manually toggling them on. It seemed almost like it wasn't always updating. I'd hit save in the json, then format. Unless the double-quotes to single quotes interacts with it?

This config breaks

> Find-VSCodeScriptAnalyzerConfig ms-vscode.powershell-2022.5.1
{
  "IncludeRules": [
    "PSPlaceCloseBrace",
    "PSPlaceOpenBrace",
    "PSUseConsistentWhitespace",
    "PSUseConsistentIndentation",
    "PSAlignAssignmentStatement",
    "PSAvoidUsingDoubleQuotesForConstantString"
  ],
  "Rules": {
    "PSAvoidUsingCmdletAliases": {},
    "PSUseConsistentWhitespace": {
      "CheckInnerBrace": true,
      "CheckPipe": true,
      "CheckOpenParen": true,
      "CheckSeparator": true,
      "CheckParameter": true,
      "Enable": true,
      "CheckOpenBrace": true,
      "CheckPipeForRedundantWhitespace": true,
      "CheckOperator": true
    },
    "PSAlignAssignmentStatement": {
      "Enable": true,
      "CheckHashtable": true
    },
    "PSPlaceCloseBrace": {
      "NewLineAfter": false,
      "IgnoreOneLineBlock": false,
      "Enable": true
    },
    "PSUseConsistentIndentation": {
      "IndentationSize": 4,
      "PipelineIndentation": 2,
      "Kind": "space",
      "Enable": true
    },
    "PSAvoidUsingDoubleQuotesForConstantString": {
      "Enable": true
    },
    "PSUseCorrectCasing": {
      "Enable": true
    },
    "PSPlaceOpenBrace": {
      "NewLineAfter": true,
      "Enable": true,
      "IgnoreOneLineBlock": false,
      "OnSameLine": true
    }
  }
}