vscode-powershell: Powershell debugger hangs stepping over a script invocation

Issue Type: Bug

  1. Open a script with the following contents
Set-StrictMode -Version 5
$ErrorActionPreference = "Stop"
. $PSScriptRoot\test2.ps1
"hello 3"
  1. In test2.ps1, put literally anything (e.g. “hello from test2.ps1”, or even blank or non-existant).
  2. Put a breakpoint on line 3 (test2.ps1 invocation)
  3. Hit F5
  4. When it breaks hit F10

Result: debugger will never return. You have to stop it and restart the integrated powershell instance.

Note: If either of the first two lines are commented out, i.e. without either Set-StrictMode OR $EAP = “Stop”, it seems to work The combination causes fits.

Note 2: If there are no breakpoints, it seems to work then as well.

Extension version: 2020.4.0 VS Code version: Code 1.45.1 (5763d909d5f12fe19f215cbfdd29a91c0fa9208a, 2020-05-14T08:27:35.169Z) OS version: Windows_NT x64 10.0.19635

System Info
Item Value
CPUs Intel® Core™ i7-8700K CPU @ 3.70GHz (12 x 3696)
GPU Status 2d_canvas: enabled
flash_3d: enabled
flash_stage3d: enabled
flash_stage3d_baseline: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
oop_rasterization: disabled_off
protected_video_decode: enabled
rasterization: enabled
skia_renderer: disabled_off_ok
video_decode: enabled
viz_display_compositor: enabled_on
viz_hit_test_surface_layer: disabled_off_ok
webgl: enabled
webgl2: enabled
Load (avg) undefined
Memory (System) 31.78GB (15.85GB free)
Process Argv –folder-uri file:///c%3A/repos/devdir
Screen Reader no
VM 0%

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 27 (7 by maintainers)

Most upvoted comments

I have had this issue today as well.

After looking at the log, I realized what caused it - at least in my case.

In my case, I had a variable in the watch window, which wasn’t initialized.

For example, I can run the following script to produce the issue:

Set-StrictMode -Version 2.0
$ErrorActionPreference = "Stop"

Write-Host "Hello World"

With my watch set like this: image

I can set a breakpoint on the first line and step through the script. As soon as I’ve stepped over the first two lines and try to step to the Write-Host line, the debugger freezes.

StrictMode version 1.0 and onwards lists the following:

Prohibits references to uninitialized variables, except for uninitialized variables in strings.

So I guess setting any version of StrictMode combined with $ErrorActionPreference='Stop' will cause issues when setting a watch on a variable that doesn’t exist.

Here’s what I saw in my log that helped me spot the issue:

2020-12-15 08:31:32.157 +01:00 [ERR] Execution of the following command(s) completed with errors:

    $Name

Error #1:
The variable '$Name' cannot be retrieved because it has not been set.
ScriptStackTrace:
at <ScriptBlock>, C:\devops\coric-engage-blueprints\freeze.ps1: line 2
Exception:
   System.Management.Automation.RuntimeException: The variable '$Name' cannot be retrieved because it has not been set.
   at System.Management.Automation.VariableOps.GetVariableValue(VariablePath variablePath, ExecutionContext executionContext, VariableExpressionAst varAst)
   at System.Management.Automation.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
2020-12-15 08:31:32.159 +01:00 [VRB] Attempting to execute command(s):

    prompt

It would be nice to have a fix for this. It would be great for the watch to check if the variable exists first, and if not, just write something like $Name: uninitialized or something like that, rather than blindly trying to read it and getting locked up. 😄

I have had this issue today as well.

After looking at the log, I realized what caused it - at least in my case.

In my case, I had a variable in the watch window, which wasn’t initialized.

For example, I can run the following script to produce the issue:

Set-StrictMode -Version 2.0
$ErrorActionPreference = "Stop"

Write-Host "Hello World"

With my watch set like this: image

I can set a breakpoint on the first line and step through the script. As soon as I’ve stepped over the first two lines and try to step to the Write-Host line, the debugger freezes.

StrictMode version 1.0 and onwards lists the following:

Prohibits references to uninitialized variables, except for uninitialized variables in strings.

So I guess setting any version of StrictMode combined with $ErrorActionPreference='Stop' will cause issues when setting a watch on a variable that doesn’t exist.

Here’s what I saw in my log that helped me spot the issue:

2020-12-15 08:31:32.157 +01:00 [ERR] Execution of the following command(s) completed with errors:

    $Name

Error #1:
The variable '$Name' cannot be retrieved because it has not been set.
ScriptStackTrace:
at <ScriptBlock>, C:\devops\coric-engage-blueprints\freeze.ps1: line 2
Exception:
   System.Management.Automation.RuntimeException: The variable '$Name' cannot be retrieved because it has not been set.
   at System.Management.Automation.VariableOps.GetVariableValue(VariablePath variablePath, ExecutionContext executionContext, VariableExpressionAst varAst)
   at System.Management.Automation.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
2020-12-15 08:31:32.159 +01:00 [VRB] Attempting to execute command(s):

    prompt

It would be nice to have a fix for this. It would be great for the watch to check if the variable exists first, and if not, just write something like $Name: uninitialized or something like that, rather than blindly trying to read it and getting locked up. 😄

This is not a bad idea at all, I’ll see what I can do.

We’ll come back to this when the debugger implementation is revamped (in https://github.com/PowerShell/PowerShellEditorServices/issues/1295).

@johndog some next steps:

  • Try to see if you can reproduce this with the PowerShell Preview extension
  • If so, we need to work out what’s happening and why the code isn’t progressing. Logs will help with that.

It looks like the log snippet you posted might be involved. It may be that strict mode prevents running something behind the scenes in the debugger.

I did try to reproduce this just now on my Windows (10.0.19041) machine and wasn’t able to in PowerShell 7.1, 7 or 5.1, so definitely worth trying the preview extension. Also worth determining if this always happens, or only sometimes.

Does the powershell extension use pwsh or powershell?

It uses whichever of the PowerShells installed on your system you’ve got it configured to use. From your log snippet, this is currently PS 7.1 on your machine. You can change this by clicking on the little PowerShell icon/version in the lower right-hand corner of the VSCode window.

This might have something to do with it. From EditorServicesLog:

2020-06-05 09:57:44.897 -07:00 [INF] PowerShell runtime version: 7.1.0, edition: Core
2020-06-05 09:57:45.060 -07:00 [INF] highlight handler loaded
2020-06-05 09:57:46.348 -07:00 [INF] Unable to find PSSA settings file at 'c:\repos\devdir\PSScriptAnalyzerSettings.psd1'. Loading default rules.
2020-06-05 09:57:46.351 -07:00 [INF] PSScriptAnalyzer settings file not found. Falling back to default rules
2020-06-05 09:57:47.201 -07:00 [INF] Adding handlers
2020-06-05 09:57:47.201 -07:00 [INF] Handlers added
2020-06-05 09:57:47.478 -07:00 [ERR] Execution of the following command(s) completed with errors:

    $contextIndex


2020-06-05 09:57:47.486 -07:00 [WRN] Runtime exception occurred while executing command:

System.Management.Automation.RuntimeException: The variable '$contextIndex' cannot be retrieved because it has not been set.
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, IList`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, PSInvocationSettings settings)
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContextService.ExecuteCommandAsync[TResult](PSCommand psCommand, StringBuilder errorMessages, ExecutionOptions executionOptions) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\PowerShellContextService.cs:line 776
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContextService.ExecuteCommandAsync[TResult](PSCommand psCommand, StringBuilder errorMessages, ExecutionOptions executionOptions) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\PowerShellContextService.cs:line 825
2020-06-05 09:59:09.647 -07:00 [ERR] Tried to resume debugger with action StepOver but there was no debuggerStoppedTask.

Does the powershell extension use pwsh or powershell?