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

Need to be able to debug powershell inline script block jobs

Open alex4998 opened this issue 4 years ago • 7 comments

I already asked a question in this regards on StackOverflow. Here it is https://stackoverflow.com/questions/67708312/is-it-possible-to-debug-powershell-inline-script-block-jobs-in-visual-studio-cod but still have no response. It looks like it is a missing feature. What I need is an ability to debug inline script block jobs in powershell scripts.

alex4998 avatar Jun 07 '21 19:06 alex4998

Can you confirm that this scenario is supported in the PowerShell Debugger itself? We can only support it if it's already in the debugger, as we're just a front-end for that.

andyleejordan avatar Jun 11 '21 19:06 andyleejordan

I don't understand what you ask me to confirm. The first thing that I did, I added a question to the StackOverflow ( https://stackoverflow.com/questions/67708312/is-it-possible-to-debug-powershell-inline-script-block-jobs-in-visual-studio-cod). It contains all the details of what I tried and nobody could help me. Then I opened an issue with vscode (https://github.com/microsoft/vscode/issues/125682#issuecomment-856136539) and I've been recommended to open an issue here. If there is yet another party which should be involved I definitely can open another issue there but I don't understand where.

I found this line by the link you provided: "If you are debugging a job by running the Debug-Job cmdlet, the Exit command detaches the debugger, and allows the job to continue running.", so I assume the debugging of jobs is possible somehow but I cannot find a way how. If you look at my StackOverflow post I tried "Debug-Job" but that didn't help.

alex4998 avatar Jun 11 '21 20:06 alex4998

What I need is an ability to debug inline script block jobs in powershell scripts

Is this ability available in the PowerShell Debugger itself? If so, then this is the correct place to ask us to extend that support to the extension (as you filed this issue in the VS Code's extension repo), and if it's not available in the PowerShell Debugger, then we should transfer it to the the PowerShell repo as a feature request.

If you need help testing with the PowerShell debugger, I would suggest the Discord.

andyleejordan avatar Jun 11 '21 20:06 andyleejordan

I guess I already answered your question and I believe you know all this stuff better than me. There is Debug-Job cmdlet - https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/debug-job?view=powershell-7.1, so this feature should be available, right?

alex4998 avatar Jun 17 '21 04:06 alex4998

Using a slightly modified version of the code from the SO thread I'm able to repro.

$job = Start-Job -ScriptBlock {
    # For some reason if this no-op variable isn't here
    # the debugger won't stop even outside of VSCode.
    $something = 'nothing'

    # Won't actually see this since the host isn't
    # attached until Debug-Job fires.
    Write-Host before breakpoint


    Wait-Debugger
    Write-Host after breakpoint
}

while ($job.State -ne 'AtBreakpoint') {
    Start-Sleep -Milliseconds 200
}

Write-Host before debug

Debug-Job -Job $job

Write-Host after debug

In the PSIC we don't actually stop execution, though we are definitely getting the debugger stop event. If the above is saved to a file and invoked manually from the PSIC, you can see VSCode get the event to start debugging (pause/restart/etc menu comes up) and we start getting host output from the job. We just aren't pausing.

It's probably an issue in the debugger stop event logic for determining if we should continue.

SeeminglyScience avatar Jun 17 '21 12:06 SeeminglyScience

This problem has not been solved in 2024...

Andy-AO avatar Feb 08 '24 05:02 Andy-AO

Debugging jobs in PowerShell with vs code is really hard. See the multiple issues reported about this. There should be some easier way to do this. Following the guides from the ScriptGuy don't always work.

In our case we're trying to stop the debugger in vs code with PowerShell 7.4.1 at a specific line to allow us to step through the code. Adding the CmdLets Set-PSBreakPoint or Wait-Debugger in the script we would like to debug doesn't work.

The furthest we came was this:

$job = Start-Job -FilePath 'C:\Set permissions.ps1' -ArgumentList $params -InitializationScript {
    $host.Runspace.Debugger.SetDebugMode([System.Management.Automation.DebugModes]::RemoteScript)
    Set-PSBreakpoint 'C:\Set permissions.ps1' -line 931
}

Debug-Job $job

But even in this example it's just stating: image

So it seems like it stopped, but it's not visible in vs code itself. What commands to use to move through the code? Typing h for help shows this, which isn't even the content of the script file we're trying to debug:

image

What now?

It's very confusing and doesn't seem doable. Is there an easier/better way? Thank you for reconsidering this issue.

DarkLite1 avatar Feb 27 '24 15:02 DarkLite1