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)"/>

  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 ]

Comments (14)
  1. otter says:

    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.

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

  3. Johan's Blog says:

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

  4. Jantje says:

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

  5. Code Junkie says:

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

  6. Eric Steuart says:

    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.

  7. ratkiley says:

    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:

    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).

  8. ratkiley says:

    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:


  9. @ratkliev: thanks, very useful addition to the article!

  10. tote says:

    do you just add the Using Task XML to the top of the project element?

    i can't seem to hit a breakpoint or get this to work really.

    i'm going directly to msbuild 2.0 as the example suggests

    ms build is running in the command window but i see no evidence of it running my custom task.

    I added the following to my Task.Test.csproj file.

    <?xml version="1.0" encoding="utf-8"?>

    <Project ToolsVersion="3.5" DefaultTargets="Build" "mlns="…/2003">

    <UsingTask AssemblyFile="D:tasksassemblyinfotask.dll" TaskName="AssemblyInfo"/>

    <Target Name="BeforeBuild" Outputs="@(AssemblyInfo)">

             <AssemblyInfo AssemblyInfoFile="@(AssemblyInfo)"/>


    <PropertyGroup> etc


    I then modded my CustomTask project to start up msbuild 2.0 with the path to Task.Test.csproj as the parameter.

    How do i set the required fields in mu Custom Task? When I call my custom task through MsBuild, it would look like this

       <MyCustomTask RequiredFiles="@(AllProjectFiles)" SelectedFiles="@(SelectedFiles)" >

    <Output ItemName="BuildProjects" TaskParameter="BuildProjects"  />

       </MyCustomTask >

  11. Brandon Hawbaker says:

    Yeah, even though my project is msbuild 4.0 I ended up needing to use the .NET 2.0 framework msbuild.exe to get it to start hitting the breakpoints.

  12. Scott Munro says:

    An alternative is to use the Debug | Attach to Process… command to attach your task project to the test project. If you build your test project then a breakpoint within your task should be hit.

Comments are closed.

Skip to main content