Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to actually debug ? #467

Closed
ArieHein opened this issue Jan 29, 2020 · 17 comments · Fixed by #1282
Closed

Question: How to actually debug ? #467

ArieHein opened this issue Jan 29, 2020 · 17 comments · Fixed by #1282

Comments

@ArieHein
Copy link
Sponsor Contributor

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

@Badgerati
Copy link
Owner

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 😕

@ArieHein
Copy link
Sponsor Contributor Author

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 ?

@Badgerati
Copy link
Owner

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 🤔

@ArieHein
Copy link
Sponsor Contributor Author

ArieHein commented Jan 30, 2020

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.

@Badgerati
Copy link
Owner

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 🤔

@ArieHein
Copy link
Sponsor Contributor Author

ArieHein commented Feb 2, 2020

Hmm, what is out-PodeHost ?

@Badgerati
Copy link
Owner

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.

@fatherofinvention
Copy link
Contributor

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.

@ili101
Copy link
Contributor

ili101 commented Feb 8, 2023

@fatherofinvention I do this https://github.com/ili101/PWT/blob/main/DebugHelper.ps1
Basically:

  1. Add the debugger command where you want to.
  2. In VSCode add another PS terminal.
  3. Do $PID and execute your Pode code.
  4. If applicable hit the breakpoint by browsing to the URL or something similar.
  5. Go back to the main PS extension terminal.
  6. Use the 2 lines to connect to the process and connect the debugger.
  7. Wait a second for the PS VSCode extension to detect and enter debug mode (or if it fails just debug in the terminal)

@fatherofinvention
Copy link
Contributor

@ili101 Wow... this is brilliant. It works perfectly. You seriously just leveled up my productivity with this. Thank you!

Tagging @Badgerati for visibility.

@Badgerati
Copy link
Owner

@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 🤔

@Badgerati
Copy link
Owner

Re-opening this to update the docs with debugging notes.

@nightroman
Copy link

nightroman commented Mar 22, 2024

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.

@fatherofinvention
Copy link
Contributor

@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.

@fatherofinvention
Copy link
Contributor

@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 !

@fatherofinvention
Copy link
Contributor

fatherofinvention commented May 2, 2024

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.

@Badgerati
Copy link
Owner

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants