vscode-powershell icon indicating copy to clipboard operation
vscode-powershell copied to clipboard

Format on save hangs with PowerShell v7.5.0, runs fine with v7.4.7

Open o-l-a-v opened this issue 10 months ago • 14 comments

Prerequisites

  • [x] I have written a descriptive issue title.
  • [x] I have searched all open and closed issues to ensure it has not already been reported.
  • [x] I have read the troubleshooting guide.
  • [x] I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • [x] I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
  • [x] If this is a security issue, I have read the security issue reporting guidance.

Summary

Format on save hangs with PowerShell v7.5.0, it never completes. Works fine with PowerShell v7.4.7.

I've tried with v7.5.0 managed by mise (ref: https://github.com/PowerShell/vscode-powershell/issues/5123#issuecomment-2612568256), and downloaded and unzipped v7.5.0 ZIP myself.

  • Windows 11 x64 24H2
  • VSCode v1.96.4
  • vscode-powershell v2025.0.0
    • Also tried with preview version v2025.1.0

PowerShell Version

v7.5.0 x64 Windows

Name             : Visual Studio Code Host
Version          : 2025.1.0
InstanceId       : dd17a949-7d42-4f49-b509-b842f6923341
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : nb-NO
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

Visual Studio Code Version

1.96.4
cd4ee3b1c348a13bafd8f9ad8060705f6d4b9cba
x64

Extension Version

[email protected]

Steps to Reproduce

  • Use PowerShell v7.5.0
  • Configure format on save for PowerShell in VSCode
  • Open PS1 file, do changes, CTRL + S to save
  • VSCode hangs, formatting never happens

Visuals

Image

Logs

I've added following config:

{
  "powershell editor services.trace.server": "Verbose",
  "powershell.developer.editorServicesLogLevel": "Trace"
}

I see no relevant log events.

  • Is "powershell editor services.trace.server" relevant anymore?

o-l-a-v avatar Feb 02 '25 12:02 o-l-a-v

I can't reproduce on my work computer, so I don't know what is going on here.

If I find out where to get relevant logs I'll add more info.

o-l-a-v avatar Feb 03 '25 12:02 o-l-a-v

Thanks as alwyas @o-l-a-v, let me know if it crops up again.

andyleejordan avatar Feb 04 '25 01:02 andyleejordan

@o-l-a-v to answer your question, the first line is no longer relevant, but powershell.trace.server is, it's configurable in the options menu now, it's not a "hidden" setting, and the logging is much better in the latest version of the extension.

JustinGrote avatar Feb 04 '25 18:02 JustinGrote

Thanks @JustinGrote. I set that to verbose, I guess the output is added to output tab -> PowerShell: Trace LSP? It does not log anything when this issue is reproduced, and all it logs before it is:

2025-02-04 20:51:23.642 [info] No parameters provided.
2025-02-04 20:51:28.275 [info] No result returned.

Maybe this is a VSCode problem, or some other extension interfering.


Edit 1: Created a new temporary VSCode profile. Added PowerShell and EditorConfig extension only, with settings.json:

{
    "editor.formatOnSave": true,
    "powershell.developer.editorServicesLogLevel": "Trace",
    "powershell.powerShellAdditionalExePaths": {
        "7.5": "Z:\\Programs\\PowerShell\\7.5\\pwsh.exe"
    },
    "powershell.powerShellDefaultVersion": "7.5",
    "powershell.trace.server": "verbose"
}

And it worked. I'll nuke my default profile I guess.


Edit 2: Testing my default profile with just the config above: It worked. Must be some conflicting setting.

o-l-a-v avatar Feb 04 '25 19:02 o-l-a-v

The new logging has different log levels that unify with VScode. You need to set your log level to debug or trace in vscode for the ps extension (or globally).

I'm actually having a hard time finding a doc on this, guess I should contribute one to the vscode docs repo.

Two ways to do the same thing: Image Image Image

JustinGrote avatar Feb 04 '25 20:02 JustinGrote

I ended up uninstalling VSCode, deleted %APPDATA%\Code and %USERPROFILE%\.vscode, installed VSCode, loaded the exact same user settings.json, and now it works again.

o-l-a-v avatar Feb 05 '25 10:02 o-l-a-v

This now happens again.

Image
  • Only with PowerShell v7.5.
  • VSCode v1.97.2
  • vscode-powershell v2025.0.0 (stable) and v2025.1.0 (preview)

I've tried to figure out why, but I've not been able to find it on logs.

If you got more troubleshooting / debug options I'm willing to try. 😊

o-l-a-v avatar Mar 04 '25 12:03 o-l-a-v

Turned on trace logging. In the "Extension Host" output I eventually saw this:

2025-03-04 13:30:55.298 [trace] [ms-vscode.powershell] provider DONE after 300061ms

Nothing around that timestamp in the "PowerShell" output.

o-l-a-v avatar Mar 04 '25 12:03 o-l-a-v

The trace level stuff doesn't surface to the PowerShell level.

Format on save basically just calls Invoke-ScriptAnalyzer with the appropriate params. Is this a large file? Does Invoke-ScriptAnalyzer with the same fix settings also act slow?

JustinGrote avatar Mar 04 '25 16:03 JustinGrote

Not a large file, happens with any PowerShell file.

Will do some testing with PSSA tomorrow.

o-l-a-v avatar Mar 04 '25 16:03 o-l-a-v

Wow. I found that if I disabled my reordering of $env:PSModulePath in $profile, it does not happen anymore.

I do this because of:

Here's the logic:

# Get PSModulePath environment variables for machine and user
$([string[]]('Machine','User')).ForEach{
    New-Variable -Force -Name ('PSModulePath{0}' -f $_) -Value (
        [string[]](
            [System.Environment]::GetEnvironmentVariable(
                'PSModulePath',
                $_
            ).Split(
                [System.IO.Path]::PathSeparator
            ).Where{
                -not [string]::IsNullOrEmpty($_)
            }
        )
    )
}
# Create new value
$PSModulePathNewValue = [System.Collections.Generic.List[string]]::new()
$PSModulePathNewValue.AddRange($PSModulePathUser)
$PSModulePathNewValue.AddRange($PSModulePathMachine)
$PSModulePathNewValue.Add(('{0}\Modules' -f $PSHOME))
# Only keep unique paths that exists
$PSModulePathNewValue = [string[]](
    $PSModulePathNewValue | Select-Object -Unique | Where-Object -FilterScript {
        -not [string]::IsNullOrEmpty($_) -and
        [System.IO.Directory]::Exists($_)
    }
)
# Set new PSModulePath in process scope
[System.Environment]::SetEnvironmentVariable(
    'PSModulePath',
    (
        $PSModulePathNewValue -join [System.IO.Path]::PathSeparator
    ),
    'Process'
)

"Vanilla" $env:PSModulePath.Split(';') after starting VSCode:

C:\Users\olavb\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\users\olavb\scoop\apps\pwsh75\7.5.0\Modules
C:\Users\olavb\AppData\Local\Microsoft\PowerShell\Test\Modules
C:\Users\olavb\scoop\modules
C:\Users\olavb\AppData\Local\Microsoft\PowerShell\Modules

C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
c:\Users\olavb\.vscode\extensions\ms-vscode.powershell-2025.0.0\modules

$env:PSModulePath.Split(';') if having that in $profile:

C:\Users\olavb\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
C:\Users\olavb\AppData\Local\Microsoft\PowerShell\Test\Modules
C:\Users\olavb\scoop\modules
C:\Users\olavb\AppData\Local\Microsoft\PowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
C:\Users\olavb\scoop\apps\pwsh75\7.5.0\Modules

Something clearly touches $env:PSModulePath after $profile has run. The major difference I see is that c:\Users\olavb\.vscode\extensions\ms-vscode.powershell-2025.0.0\modules is missing. But readding it as the first path in $profile did not fix it either.

Ergo, it has something to do with $env:PSModulePath, but I'm not sure what exactly.

o-l-a-v avatar Mar 05 '25 09:03 o-l-a-v

This now happens again.

Image * Only with PowerShell v7.5. * VSCode v1.97.2 * vscode-powershell v2025.0.0 (stable) and v2025.1.0 (preview)

I've tried to figure out why, but I've not been able to find it on logs.

If you got more troubleshooting / debug options I'm willing to try. 😊

Just to comment that I've seen this exact same thing (and arrived here after searching for similar). Same PS and extension version, but VSCode v1.99.2.

robinmalik avatar Apr 14 '25 10:04 robinmalik

I've been noticing it more too lately frankly, but only in flaky situations

JustinGrote avatar Apr 14 '25 14:04 JustinGrote

I am experiencing the same issue when saving any powershell script, even it's just a basic script containing only Write-Host "Hello World!"

Vscode details:

Version: 1.100.1 (user setup)
Commit: 91fa95bccb027ece6a968589bb1d662fa9c8e170
Date: 2025-05-09T15:43:50.040Z
Electron: 34.5.1
ElectronBuildId: 11369351
Chromium: 132.0.6834.210
Node.js: 20.19.0
V8: 13.2.152.41-electron.0
OS: Windows_NT x64 10.0.26100

Powershell version 7.5.1

Powershell Extension version: v2025.0.0

Let me know if there is any useful debugging I can activate to find the root cause.

Sebast1aan avatar May 20 '25 07:05 Sebast1aan