Pode
Pode copied to clipboard
Question: How to actually debug ?
Question
When I'm doing changes in my server.ps1 and i relaunch my server, it goes up correctly but some of the changes i do do not appear to work but i have no way of knowing what is going underneath. Is there like a best practice guide for the minimum components in server.ps1 that need to be up for this to happen ?
I know how to use logging to track requests, but I'm looking into the debugging of the inner Script blocks, its not like i can put breakpoints into server.ps1
Hey @ArieHein,
If you're using the internal Pode restarts, then this page might help - when the server restarts, the main scriptblock supplied to Start-PodeServer won't reflect any changes made; this is due to the restart merely re-invoking the scriptblock that was original supplied. To have those changes reflect, you'll need to fully stop-then-start your server. There's also this that might help.
For minimal components, it depends on the server type; but if we assume a web-server then mostly just an Add-PodeEndpoint and then the Routes (and even the routes are optional!).
I know what you mean about the debugging and route scriptblocks, runspaces are quite a pain for this. Normally when I need to debug something in these I go down the simple road of '<comment>' | Out-PodeHost 😂 . There's also Pode's inbuilt error logging, which you could setup to go to a file/terminal, and then use Write-PodeErrorLog to see what's going on.
It'd be nice if breakpointing for runspaces did work, but at the moment I'm unaware if there's a way to make it possible 😕
Yes i do use the restart via filewatcher, im more refering to debugging inside the script block. For example i use import-podemodule to load a module i created with some functions that are exported so inside the script block i just call one of the commands and pass it parameters that are part of the response. Can i not pass some variable content back to Pode to report it ?
I think I understand what you're after, but would you be able to give a rough code example as to what you need? For the variable content the closest I can think of is Write-PodeErrorLog, but perhaps there could be a better way that might be implemented into Pode to do what you're after 🤔
Assume this for server.ps1
Start-PodeServer {
Import-PodeModule -Path "./General.psm1"
Add-PodeEndpoint -Address . -Port 8080 -Protocol HTTP -Name "General"
Add-PodeRoute -Method GET -Path "/general" -EndpointName "General" -ContentType "application/json" -ScriptBlock {
param($Request)
Try {
$Output = Get-GeneralData -Param1 $Request.Data.Param1
# do something with $Output
}
Catch {
$Output = ($_.Exception.Message)
}
Finally {
Write-PodeJsonResponse -Value ("Get General Data")
}}
Assume this is General.psm1
Function Get-GeneralData {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)][string] $Param1
)
Try {
# take $param1 and get some data
}
}
Catch {
Return ( $_.Exception.Message)
}
}
Export-ModuleMember -Function Get-GeneralData
So basically, a request comes to /general and the script block runs the Get-GeneralData function from the module that returns something.
When i change the internal code of the module it has no effect on the server successfully coming up or not ,as it should but i loose the ability to debug the changes in the module and im looking for a way to push some values back to terminal or otherwise that will allow me some sort of looking at values while its in the module.
If the Catch inside the module throws the exception, its not that the call from pode failed so pode will see this as successful.
Hope that made it more visible.
I think I can see what you're getting at. If it's just outputting values back to the terminal wouldn't using Out-PodeHost work? Such as $Param1 | Out-PodeHost, or $_.Exception | Out-PodeHost, within the catch block. That'll output the values straight to the server's terminal.
It might also be worth investigating Debug-Runspace - I may try and have a play with it later/this weekend 🤔
Hmm, what is out-PodeHost ?
Out-PodeHost is basically just Out-Default - it allows you to write messages/objects to the terminal the server was started in, since cmdlets like Write-Host don't work in runspaces.
Hey @Badgerati! Have you had any success with Debug-Runspace? I tried it earlier but didn't get far. I'm hoping to be able to set a breakpoint or attach a debugger at a specific point in a scriptblock so I can check the state.
@fatherofinvention I do this https://github.com/ili101/PWT/blob/main/DebugHelper.ps1 Basically:
- Add the debugger command where you want to.
- In VSCode add another PS terminal.
- Do
$PIDand execute your Pode code. - If applicable hit the breakpoint by browsing to the URL or something similar.
- Go back to the main PS extension terminal.
- Use the 2 lines to connect to the process and connect the debugger.
- Wait a second for the PS VSCode extension to detect and enter debug mode (or if it fails just debug in the terminal)
@ili101 Wow... this is brilliant. It works perfectly. You seriously just leveled up my productivity with this. Thank you!
Tagging @Badgerati for visibility.
@ili101 @fatherofinvention awesome! I played with Enter-PSHostProcess and Get-Runspace a while back but couldn't get it to work; must've been missing Wait-Debugger!
I'll have a try myself too, and update the docs with a debugging section; also wondering if there's any helper functions I can put into Pode to for this as well 🤔
Re-opening this to update the docs with debugging notes.
Here is some peculiar but surprisingly robust bare runspace debugger: https://www.powershellgallery.com/packages/Add-Debugger
In your Pode route handler or similar code call Add-Debugger and then use Wait-Debugger as hardcoded breakpoints or set other breakpoints by Set-PSBreakpoint.
When breakpoints are hit, enter debugger commands as prompted (either in console or GUI depending on the host and Add-Debugger parameters).
Trivial example: add the debugger and break immediately after:
Add-Debugger
Wait-Debugger
Tip: Use d (Detach) in order to stop debugging instead of q (Quit) to avoid stopping Pode.
@nightroman - Fan of your work, been following you on Github for years. Thanks for sharing this here, I'll give it a try later today.
@nightroman just came back to say thank you once more for Add-Debugger. It's a game changer for debugging Pode. For anyone new to it, it installs as a Script and not a Module which means you'll need to make sure your scripts directory is in your PATH if it isn't already. You can add it on Ubuntu for example like this:
echo 'export PATH="/usr/local/share/powershell/Scripts:$PATH"' >> ~/.bashrc
source ~/.bashrc
You should see it there now when you run echo $PATH. Once that's done you can follow the steps @nightroman provided above to start debugging without having to enter a PSHostProcess and then Debug-Runspace. This is a very nice convenience!
@Badgerati wondering if you've given this a shot yet? I think it will really speed up your work on Pode too !
One last tip: if you run sudo pwsh that resets your PATH var to secure paths defined in the sudoers config, ignoring the your user's custom PATH settings. I believe there is a way to preserve or add custom paths even when running as sudo but I haven't tried that yet.
@fatherofinvention I have given it a shot, and it's really useful when any error aren't immediately obvious :) I've documented it use now as well in 2.9.0