Welcome to MSDN Blogs Sign in | Join | Help

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:

  1. Create a test project to run your task in
  2. 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>


  3. Close the test project
  4. Open your custom task project
  5. Go to Project > projectname Properties…
  6. Go to the Debug tab
  7. 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”
  8. Change the “Command line arguments” to the full path to your test project
  9. 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 ]

Published Wednesday, September 28, 2005 6:43 PM by msbuild
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Tuesday, May 16, 2006 5:38 PM by otter

# re: How To: Debug a custom MSBuild task using Visual Studio

thank you, this work for me,  would be great if this info made it into the documentation, perhaps as a remark on creating a custom task.
Thursday, February 22, 2007 4:26 PM by Johan Danforth's WebLog

# [MsBuild] Writing and Debugging Custom Tasks

Writing a custom task for MSBuild is very simple, here&#39;s the bare minimum code you would need to

Friday, February 23, 2007 3:27 AM by Johan's Blog

# [MsBuild] Writing and Debugging Custom Tasks

Writing a custom task for MSBuild is very simple, here&#39;s the bare minimum code you would need to

Wednesday, August 22, 2007 9:48 AM by Jantje

# re: How To: Debug a custom MSBuild task using Visual Studio

This doesn't work, you need to set the working directory instead of  the Command line arguments..

# itworks &raquo; Blog Archive &raquo; How To: Debug a custom MSBuild task using Visual Studio

Friday, April 11, 2008 9:01 PM by Code Junkie

# MSBuild Links

For sometime now I have been working on custom MSBuild task. The following links were very useful in

Saturday, April 12, 2008 12:24 PM by Microsoft news and tips &raquo; MSBuild Links

# Microsoft news and tips &raquo; MSBuild Links

Tuesday, July 15, 2008 12:57 PM by Eric Steuart

# re: How To: Debug a custom MSBuild task using Visual Studio

This was very helpful. Jantje's comment about the working directory is partially true. The working directory can make a huge difference in your output, but the command line is also needed so that MSBuild can know what to build. I suggest making the working directory the same as it would be if you were building the project from Visual Studio. This is NOT always the same as the location of the project file.

Tuesday, February 03, 2009 4:02 PM by ratkiley

# re: How To: Debug a custom MSBuild task using Visual Studio

I was not able to get MSBuild 3.5 to work with the above instructions.  I think it is because of a change to MSBuild and how it passes parameters in v 3.5.  See this link for more info:

http://blogs.msdn.com/aaronhallberg/archive/2007/05/03/tfsbuild-rsp-and-logging-verbosity-in-orcas.aspx

I was able to get it to work after:

1) Queue-ing a new build the usual way by right-clicking on the build definition in the Team Explorer in VS 2008.

2) After the build completes, open up the BuildType\ folder (where the TFSBuild.proj file is located).  The BuildType\ folder should be located in the same folder as the Sources\ and Binaries\ folders are located by default.  Then, open up the auto-generated TFSBuild.rsp file and copy all of the /p: parameters out of it.

3) Paste all of the /p: parameters into the command line parameters field in the project field mentioned in the original blog post, followed by the full path to the TFSBuild.proj file (located in the BuildType\ folder).

4) Set the break point in your custom task, and "Start Debugging" from Visual Studio /(press F5).

Tuesday, February 03, 2009 4:04 PM by ratkiley

# re: How To: Debug a custom MSBuild task using Visual Studio

Oh... and the path the the msbuild.exe in the .NET 3.5 framework folder needs to be specified for the command line.  It should be something like this:

C:\WINDOWS\microsoft.net\Framework\v3.5\MSBuild.exe

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker