Debugging init.ps1 and install.ps1 during package installation within Visual Studio (using PowerShell VSX) or Powershell scripting IDE

Topics: General
Nov 23, 2012 at 5:40 AM

My goal is to debug within Visual Studio 2012 Nuget powershell scripts and modules that are installed using Nuget powershell console. Ideally I would like to intercept running these .ps1 scripts under <solution path> folder:
      init.ps1   << Run only once on Solution level
        install.ps1 << Run for each Project during installation

More info:

When installing a Nuget package: Install-Package MvcMembership for example I believe the first thing is to download a .zip Nuget package (e.x. MvcMembership.nupkg) and extract it into <solution path>\packages\ folder and then execute init.ps1 conditionally (if MvcMembership is already installed it is not run) and then run install.ps1 for a currently selected project.

So how to intercept running init.ps1 and install.ps1, load it into PowerGui Visual Studio editor and break on first line (or Powershell scripting IDE)?
Would I need to add something to these .ps1 files like: Set-PSBreakpoint(which would require me to extract .nupkg edit .ps1 script files and re-zip them again


Nov 25, 2012 at 3:32 PM

Unfortunately this is not currently possible. The NuGet Powershell host implementation lacks the necessary plubming in order to make debugging work.  

Nov 25, 2012 at 3:35 PM

Unfortunately this is not currently possible. The NuGet Powershell host implementation lacks the necessary plubming in order to make debugging work.  

Nov 25, 2012 at 3:39 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Feb 8, 2014 at 1:24 AM
Edited Feb 8, 2014 at 1:26 AM
Perhaps I am late to the party but here is a solution for debugging NuGet specific scripts, the NuGet package NuGetDebugTools. Its script Add-Debugger.ps1 adds a simple and yet effective debugger to the NuGet package manager console.
Feb 8, 2014 at 6:09 AM
Edited Feb 8, 2014 at 6:17 AM
It is never late since we still need another solution.
I was able to execute Nuget installs using PowerShell VSX debugger from visual studio.
I explained it as a comment (signed with Rad) in this blog post
So I could use the goodies of PowerShell VSX watch, locals, call stack and everything was visual (with some problems).
Basically I had to tweak some of Nuget packages to loose the restriction of running only in Nuget powershell console.

Can you demonstrate one "Hello World" example of debugging a package with init.ps1 and install.ps1 with a dependent package that has the same scripts. Preferably a blog post will do or a screen-cast on youtube.
I suppose you can debug PS modules as well.
I would like to visually create the breakpoints so I can jump around the source.

I was talking about solving some of the problems with Adam and hopefully his PowerShell Tools for Visual Studio will address some of the issues like these.

Feb 8, 2014 at 8:10 AM
Edited Feb 8, 2014 at 3:21 PM
The script Add-Debugger.ps1 is the minimalistic debugger without GUI (except the input box), very similar to the built-in debugger in the Microsoft console host. Thus, you cannot do anything "visually" in it, you have to type commands and see their output. I do not have plans to evolve it in this direction because I hope NuGet console will have its own debugger eventually. Nevertheless, it is a script debugger with necessary features.

"Hello World" scenario:

-- start Visual Studio

-- open NuGet console and type commands:

PM> Add-Debugger.ps1
PM> Set-PSBreakpoint -Command init
PM> Set-PSBreakpoint -Command install

(or set more specific breakpoints, see help Set-PSBreakpoint)

-- open a Visual Studio solution or invoke Install-Package XYZ for already opened

-- the debugger input dialog appears on any init.ps1 and install.ps1 invoked

-- type ? as debugger input and see what you can do:

s, StepInto Step to the next statement into functions, scripts, etc.
v, StepOver Step to the next statement over functions, scripts, etc.
o, StepOut Step out of the current function, script, etc.
c, Continue Continue operation (also on Cancel or empty).
q, Quit Stop operation and exit the debugger.
?, h Display this help message.
r Display PowerShell command history.
k Display call stack (Get-PSCallStack).
<number> Show debug location in context of <number> lines.
<command> Invoke any PowerShell <command> and write its output.

-- type other debugger and PowerShell commands and watch the output in the NuGet console

I tried this scenario and was able to step through two scripts init.ps1 and install.ps1 using this simple debugger.

P.S. Perhaps I should have mentioned that Add-Debugger.ps1 is a standalone tool. It is for any PowerShell host/runspace with no own debugger. In particular it is not related to PowerShell VSX.
Feb 8, 2014 at 6:30 PM
Great. Thank you very much