PSScriptAnalyzer icon indicating copy to clipboard operation
PSScriptAnalyzer copied to clipboard

Powershell Code Formatter Removing Text From Lines

Open gobenaus opened this issue 4 years ago • 2 comments

Hi,

I am trying to track down an issue where the Powershell code formatter is run against a psm1 file but does not seem to do anything at all. I started pasting individual functions of the psm1 file into a new file to see if perhaps it was confused by a certain piece of code. I have yet to establish why it does not appear to do anything on the original file at all. It did run on the new file but it messed up one function that contained the following line of code. I was able to replicate this issue by creating a new .psm1 file that only contains the string below and then running the Format Document against it

Code Before Running Formatter

Write-host "Error: File '$settingsfile' does not exist" -ForegroundColor Red

Code After Running Formatter

Write-Host "Error: File '$settingsfile'-ForegroundColor Red

What is odd is that if you run the line above through Invoke-Formatter, it does not mess up the result. But if you use SHIFT-ALT-F, it does. If you use Format Selection it does nothing in code. It was supposed to change Write-host to Write-Host.

Apart from the fact that it actually removes code it is not supposed to remove, what is the best way to debug this. The result returned by Invoke-Formatter does not seem to correspond what is put back in the psm1 file.

Visual Studio Code Version: 1.52.1 Powershell Extension: v2020.6.0 PSVersion 5.1.17763.1490 Windows Version: 10.0.17763.0 - Windows 2019

gobenaus avatar Jan 03 '21 23:01 gobenaus

Using this config, does it stop mutating your code? If not, my full config which does not mutate is here: https://gist.github.com/ninmonkey/1c1ab3fcaa6776e754679598231fc26b *[1]

{
    "powershell.codeFormatting.whitespaceBetweenParameters": false,
    "powershell.codeFormatting.useCorrectCasing": true,
    "powershell.codeFormatting.autoCorrectAliases": true,
    "powershell.codeFormatting.ignoreOneLineBlock": true,
    "powershell.codeFormatting.whitespaceAroundOperator": true,
    "powershell.codeFormatting.addWhitespaceAroundPipe": false,
}

What is odd is that if you run the line above through Invoke-Formatter, it dos not mess up the result.

I've filed a few issues where the formatter mutates code. It happens with certain powershell.codeFormatting.* values.

VS Code's format code command ignores your PSScriptAnalyzerSettings.psd1 config file. It's only for linting. Some of the args for formatting are hardcoded in the code.

{
    "powershell.scriptAnalysis.settingsPath": "C:\\foo\\PSScriptAnalyzerSettings.psd1",
}

[1]: except the process -> get-process part, for example when you are missing a comma in your param() block and save it. That case is technically not a bug.

ninmonkey avatar Jan 10 '21 17:01 ninmonkey

@gobenaus did the suggestion above help you in any way?

JamesWTruher avatar Sep 28 '21 22:09 JamesWTruher