How To: Debug a custom MSBuild task using Visual Studio
Yesterday I was complaining to Kieran about how hard it was to debug a custom task. When working on my AssemblyInfo task I was debugging all my regular expressions by running them in a separate command-line app, and with Log.LogMessage() calls in the task.
Kieran listened politely to my complaining, but clearly thought I was an idiot. He then proceeded to show me how to debug a custom task within Visual Studio. Here’s how to do it:
- Create a test project to run your task in
- Edit the .csproj/.vbproj file and add in the necessary MSBuild XML to make use of your task. Here is what I added:
<UsingTask AssemblyFile="D:\tasks\assemblyinfotask.dll" TaskName="AssemblyInfo"/>
<Target Name="BeforeBuild" Outputs="@(AssemblyInfo)">
<AssemblyInfo AssemblyInfoFile="@(AssemblyInfo)"/>
</Target>
- Close the test project
- Open your custom task project
- Go to Project > projectname Properties…
- Go to the Debug tab
- Change the start action to “Start external program” and put in the full path to MSBuild as the program. In my case the path is “E:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe”
- Change the “Command line arguments” to the full path to your test project
- Close the property window
Voila! Now you can set a breakpoint on the Execute() method in your custom task and single-step through all your code. Thanks Kieran!
[ Author: Neil Enns ]