How to debug install.ps1 script of NuGet package
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.
The sample scenario:
- start Visual Studio
open NuGet console and type commands
PM> Add-Debugger [-ReadHost]PM> Set-PSBreakpoint -Command initPM> 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 empty input).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.+<number> Set location context preference to <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
v1.4.0 - New switch ReadHost
tells to use Read-Host
for input instead of the default GUI input box.
This is how I was able to step-through install.ps1 using PowerShell ISE:
To be able to step through execution of the install script using PowerShell ISE follow these steps:Enable execution of assemblies built with .Net 4
Either
C:\Windows\System32\WindowsPowerShell\v1.0Or
C:\Windows\SysWOW64\WindowsPowerShell\v1.0
Depending on which version of PS you're usingIf files are not there create them
Either C:\Windows\System32\WindowsPowerShell\v1.0OrC:\Windows\SysWOW64\WindowsPowerShell\v1.0
Depending on which version of PS you're using
If config files are not there create them
powershell.exe.config:
<configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0.30319"/> <supportedRuntime version="v2.0.50727"/> </startup> </configuration>
powershell_ise.exe.config:
<?xml version="1.0" encoding="utf-8"?><configuration> <startup> <supportedRuntime version="v4.0.30319" /> </startup></configuration>
To be able to run PowerShell scripts included with a NuGet package the execution policy will need to be changed:
Set-ExecutionPolicy RemoteSigned -Scope Process
Copy install.ps1 that you want to debug and modify it's contents as follows:
delete the parameters block
param( [Parameter(Mandatory=$true)] [string] $installPath, [Parameter(Mandatory=$true)] [string] $toolsPath, [Parameter(Mandatory=$true)] $package, [Parameter(Mandatory=$true)] $project)
import a module which allows to use nuget cmdlets outside of the VS host process
Download http://community.sharpdevelop.net/blogs/mattward/NuGet/NuGetOutsideVisualStudio.zip Extract contents of the bin folder to some place and then import the PackageManagement.Cmdlets.dll
like so:
import-module "C:\dev\NuGetOutsideVisualStudio\bin\PackageManagement.Cmdlets.dll"
now you are able to set all the parameters manually like so:
$toolsPath="C:\dev\demo-solution\packages\X1.Registration.DbUpdate.0.4\tools"$installPath="C:\dev\demo-solution\packages\X1.Registration.DbUpdate.0.4"set-project DemoSolution.Logic C:\dev\demo-solution\DemoSolution.sln$project = Get-Project -name DemoSolution.Logic
That still leaves $package object unset but I found that script doesn't really refer to that parameter