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

Debug psm1 files. #133

Closed
elangelo opened this issue Apr 6, 2016 · 2 comments
Closed

Debug psm1 files. #133

elangelo opened this issue Apr 6, 2016 · 2 comments

Comments

@elangelo
Copy link

elangelo commented Apr 6, 2016

Hi,

I can't find a standard way to debug functions in psm1 files.
I've hacked together that more or less works for me now but it's incomplete:

  1. adjust launch.json:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "PowerShell",
            "type": "PowerShell",
            "request": "launch",
            "program": "Invoke-PSake",
            "args": ["${workspaceRoot}\\Debug.ps1", "Debug", "-parameters @{\"ModuleFile\"=\"${file}\"}"]
        }
    ]
}
  1. add Debug.ps1
`properties{
    $ModuleFile
}

Task Debug {
    echo "debugging"
    Import-Module $ModuleFile
    $moduleName = [io.path]::GetFileNameWithoutExtension($ModuleFile)
    $command = Get-Command -Module $moduleName

    if ($command.Length -gt 1) {
        Write-Host "More than one exported command in the module you want to debug."   
    } else {
    & $command
    }
}`

This works rather well if you are debugging modules with only one exported command and that doesn't require any parameters.

@daviwil
Copy link
Contributor

daviwil commented Apr 6, 2016

Hah! I'm surprised that putting Launch-PSake in the program field of launch.json actually works. I've never tried that before.

One thing that we need to do (which I can't find the bug for at the moment) is to call Import-Module if a .psm1 file is run in the debugger instead of just executing the file directly. However this still doesn't solve the problem of exercising the functionality. You'd still need some form of test script for that, so what you're doing right now makes sense to me.

Do you have any ideas on what else we might be able to do to enable better module debugging?

@rkeithhill
Copy link
Contributor

I don't think you need to Invoke-PSake to debug your PSM1 file. PSake is more of a build tool. I also don't think you need to modify launch.json. Just create a debug harness script like this:

# DebugModule.ps1
Import-Module $PSScriptRoot\YourModule.psm1
# Put in a call to a module method you want to step into and set a breakpoint here

Then just have this file open in the active editor window when you start debugging.

Another option is to use Pester to write tests for your module. This is what I recommend. This provides two benefits - 1) you now have tests you can run anytime to check for regressions and 2) the execution of those tests can help you debug your functions.

Just open a Pester test file (.ps1) and set a breakpoint on a call to one of your module functions. Then start debugging with the Pester file open. For an example of this, open the Examples folder in the PowerShell extensions install dir ($home/.vscode/extensions/ms-vscode.powershell-0.5.0/examples). Then open the Tests\PathProcessing.Tests.ps1 file. Note I believe the test file should end with .Tests.ps1 for Pester to "automatically" find it. Set a breakpoint on line 40, press F5 and you should be able to step into the module function Import-FileNoWildcard. After stopping the Debug session, press Ctrl+Shift+T - note that this will now run all the Pester tests in the workspace folder. This does require configuration of the .vscode\tasks.json file in your workspace folder.

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

No branches or pull requests

3 participants