PowerShell / PowerShell/PSScriptAnalyzer

Powershell Code Formatter Removing Text From Lines

Open
#1,655 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area - Formatter
Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal .psm1 reproduction containing the Write-host line and compare the output from Invoke-Formatter with the result of Format Document in Visual Studio Code. Done means formatting changes Write-host to Write-Host without removing the closing quote or -ForegroundColor Red, and the two formatting paths produce consistent results.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell, vscode
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.