Formatting Issue, example included
Issue Type: Bug
This is an example of the formatting issue I have been struggling with. It appears that that using a multi-line piping statement, along with the 'Out-Null' is the cause in this situation. Formatting the document adds an additional tab each time this code happens. I have much more involved code then this example that becomes really confusing when this indent issue occurs.
function Test-Format([int]$num) {
if($num -eq 1){
Get-ChildItem -Path 'c:\*.*' | Where-Object {$_.name -like 'test1.*'} |
ForEach-Object{Remove-Item -Path $_ | Out-Null}
}
else{
Get-ChildItem -Path 'c:\*.*' | Where-Object {$_.name -like 'test2.*'} |
ForEach-Object{Remove-Item -Path $_ | Out-Null}
}
}
Test-format -num 1
Extension version: 2021.2.2 VS Code version: Code 1.54.1 (f30a9b73e8ffc278e71575118b6bf568f04587c8, 2021-03-04T22:38:31.419Z) OS version: Windows_NT x64 10.0.19042
System Info
| Item | Value |
|---|---|
| CPUs | Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz (8 x 3700) |
| GPU Status | 2d_canvas: enabled gpu_compositing: enabled multiple_raster_threads: enabled_on oop_rasterization: enabled opengl: enabled_on protected_video_decode: unavailable_off rasterization: enabled skia_renderer: enabled_on video_decode: enabled vulkan: disabled_off webgl: enabled webgl2: enabled |
| Load (avg) | undefined |
| Memory (System) | 31.92GB (22.25GB free) |
| Process Argv | --crash-reporter-id 73837bc4-9684-4f62-8d48-bb5a4bc03bd8 |
| Screen Reader | no |
| VM | 0% |
A/B Experiments
vsliv368:30146709
vsreu685:30147344
python383:30185418
vspor879:30202332
vspor708:30202333
vspor363:30204092
vstry244cf:30256637
pythonvsdeb440:30248342
pythonvsded773:30248341
pythonvspyt875:30259475
dockersubset:30265998
pythontbcf:30265426
vspre833:30267464
$null = Remove-Item -Path $_ is faster
I was not looking for a code review... For the purpose of code formatting, It's legal syntax.
Can anybody copy this code into their editor and verify there is an issue for the "Format Document"? I've had an issue once before and it turned out to be a local problem within my install.
Same code, without a line break after the second pipe, system formatted:
function Test-Format([int]$num) {
if($num -eq 1){
Get-ChildItem -Path 'c:\*.*' | Where-Object {$_.name -like 'test1.*'} | ForEach-Object{Remove-Item -Path $_ | Out-Null}
}
else{
Get-ChildItem -Path 'c:\*.*' | Where-Object {$_.name -like 'test2.*'} | ForEach-Object{Remove-Item -Path $_ | Out-Null}
}
}
Test-format -num 1
I just tested for you @moymike and with the latest stable extension, on macOS, in VS Code 1.54.1 with PowerShell 7.1.2, it formatted just fine:
function Test-Format([int]$num) {
if ($num -eq 1) {
Get-ChildItem -Path 'c:\*.*' | Where-Object { $_.name -like 'test1.*' } |
ForEach-Object { Remove-Item -Path $_ | Out-Null }
}
else {
Get-ChildItem -Path 'c:\*.*' | Where-Object { $_.name -like 'test2.*' } |
ForEach-Object { Remove-Item -Path $_ | Out-Null }
}
}
Test-format -num 1
I'm on VS Code 1.54.1 Powershell 5.1.19041.610 PowerShell Extension 2021.2.2 Windows 10 pro / Version 20H2 (build 9042.867) PSScriptAnalyzer Version 1.19.1
Another test...
$script = {function Test-Format([int]$num) {
if($num -eq 1){Get-ChildItem -Path 'c:\*.*' | Where-Object {$_.name -like 'test1.*'} |
ForEach-Object{Remove-Item -Path $_ | Out-Null}}
else{Get-ChildItem -Path 'c:\*.*' | Where-Object {$_.name -like 'test2.*'} |
ForEach-Object{Remove-Item -Path $_ | Out-Null}}}
Test-format -num 1
}
Invoke-Formatter -ScriptDefinition $script
This runs PSScriptAnalyzer, Invoke-Formatter directly from a call. This is the result I get: (still getting the added indents)
function Test-Format([int]$num) {
if ($num -eq 1) {
Get-ChildItem -Path 'c:\*.*' | Where-Object { $_.name -like 'test1.*' } |
ForEach-Object { Remove-Item -Path $_ | Out-Null }
}
else {
Get-ChildItem -Path 'c:\*.*' | Where-Object { $_.name -like 'test2.*' } |
ForEach-Object { Remove-Item -Path $_ | Out-Null }
}
}
Test-format -num 1
@moymike with your last example, do you get the same behaviour in the powershell.exe console?
Okay, had to install the module for ISE/Powershell via ISEScriptAnalyzerAddOn 1.0
Ran the
Invoke-Formatter -ScriptDefinition $script
and it has the issue.
So this is a problem with PSScriptAnalyzer and the Invoke-Formatter. It happens in the version I just ran against (1.0 2/29/2016) and in the version the VSCode uses (1.19.1 Jul 30, 2020)
I then removed the ISE installed version of PSScriptAnalyzer version 1.19.1 from the Github repo and got the same result.
I guess this thread needs to be moved to PowerShell/PSScriptAnalyzer, not sure how to do that, so I created a new issue in that project.
Found a work around in VSCode, kinda...
Settings / Extensions / Powershell Configuration / Code Formatting: Pipeline Indentation Style = NoIndentation
Mine was set to 'IncreaseIndentationForFirstPipeline'.
I can live with the settings change, but there is a bug in using the 'IncreaseIndentationForFirstPipeline', as it does not drop the extra indentation in the example I submitted.
Also, has the same formatting issue when "IncreaseIndentationAfterEveryPipeline" is set.
Just FYI you can install PSScriptAnalyzer directly on your machine with Install-Module PSScriptAnalyzer and you can then run it from the powershell.exe console.
Just FYI you can install PSScriptAnalyzer directly on your machine with
Install-Module PSScriptAnalyzerand you can then run it from the powershell.exe console.
Did that, same results.