Repository navigation
Unable to format document in special circumstances #1994
Copy link
Copy link
Closed
Labels
Description
Prerequisites
- I have written a descriptive issue title.
- I have searched all open and closed issues to ensure it has not already been reported.
- I have read the troubleshooting guide.
- I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
- I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
- If this is a security issue, I have read the security issue reporting guidance.
Summary
Extension works perfectly most of the time in recent months. Today I've noticed weird issue
Where a simple code like this format mostly everything with the exception of lines with Get-Item
$Files = Get-ChildItem -Path $Directory -File -Recurse -Include "*$ExtensionFrom"
foreach ($File in $Files) {
if ($File.Extension -eq $ExtensionFrom) {
if ($OnlyNewerThan -and $File.LastWriteTime -lt (Get-Date).AddDays(-$OnlyNewerThan)) {
#Write-Color -Text '[i]', "[TheDashboard] ", "Skipping $($File.FullName) as it's older than $OnlyNewerThan days" -Color Yellow
continue
}
Write-Color -Text '[i]', "[TheDashboard] ", "Processing fixes $($File.FullName) / $($File.LastWriteTime)" -Color Yellow
# Store original dates
$originalCreationTime = $File.CreationTime
$originalLastWriteTime = $File.LastWriteTime
$Encoding = Get-FileEncoding -Path $File.FullName
$FileContent = Get-Content -Raw -Path $File.FullName -Encoding $Encoding
if ($FileContent -match $SearchString) {
Write-Color -Text '[i]', "[TheDashboard] ", "Processing fixes $($File.FullName) for ($SearchString)" -Color Green
$FileContent -replace $SearchString, $ReplaceString | Set-Content -Path $File.FullName -Encoding $Encoding
# Restore original dates
(Get-Item $File.FullName).CreationTime = $originalCreationTime
(Get-Item $File.FullName).LastWriteTime = $originalLastWriteTime
}
}
}Trying to format it, leaves it as is, at the same time it formats everything else around it. So it seems there's something special about it.
PowerShell Version
Name Value
---- -----
PSVersion 7.4.1
PSEdition Core
GitCommitId 7.4.1
OS Microsoft Windows 10.0.22631
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0.}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0Visual Studio Code Version
1.89.0-insider
x64Extension Version
ms-vscode.powershell@2024.3.2Steps to Reproduce
- Add provided code to code
- Make sure lines with Get-Item are not formatted
- Try to format them
- No formatting applies
Visuals
No response
Logs
No response
Reactions are currently unavailable

Activity
ninmonkey commentedon Apr 10, 2024
I've tried a bunch of variations. You can reproduce it with almost any expression that starts with a
(You can reproduce it with this
formats as
contrary, working case
formats correctly
Another fail case
andyleejordan commentedon Apr 10, 2024
Oh weird, thanks for the report. Did you try it under PSSA directly too?
PrzemyslawKlys commentedon Apr 10, 2024
No I didn't, sorry.
andyleejordan commentedon Apr 10, 2024
The latest version of the extension is using PSSA 1.22, which just came out. It would be helpful to know if this repros directly with
Invoke-Formatterand if so we can move the bug over there and get it fixed.PrzemyslawKlys commentedon Apr 10, 2024
I would say it's PSScriptAnalyzer issue. Will you move it?
andyleejordan commentedon Apr 10, 2024
Thanks so much Przemysław Kłys (@PrzemyslawKlys)!
liamjpeters commentedon Apr 12, 2024
Stepping through
Formatter.Format()this behaviour arises from thePSUseConsistentIndentationrule.Specifically from:
PSScriptAnalyzer/Rules/UseConsistentIndentation.cs
Lines 165 to 177 in a754b95
The issue seems to be that, if the left paren
(is:OR
AND
THEN that line's indentation is not checked against what we expect it to be. (Other necessary things still happen however)
Coming at it for the first time, I find the code-flow of the rule to be difficult to follow. The actual evaluation of whether the indentation is correct happens within the
AddViolation()function, and only ifonNewLineis true. It reads as though at each token parse, we're adding a rule violation - when in reality, checks are being performed and conditionally a violation is being added.I believe, in the short-term, a change should be made so that in the situation outlined above, we still check the lines indentation is at the level we expect (but we don't increment the indentation level). Longer-term perhaps this rule should be revisited to make it a bit easier to follow and update it's styling slightly.
Christoph Bergmeister (@bergmeister) - this looks to be a rule you've put a lot of time and effort into, and you put together the PR that introduced this LParen check (#1469) - What do you think?