Reputation: 7963
I'm trying to debug the MSBuild Customtask, that I have just created, but for some reason it never stops at the breakpoint. I've even tried this:
public override bool Execute()
{
System.Diagnostics.Debugger.Break();
And added a break point on that line... I even eliminated all the other code in the method and that didn't change anything.
Is there anything special required to be able to debug the creation of custom tasks for MSBuild ?
Upvotes: 24
Views: 7372
Reputation: 2647
In order to debug the custom Task, you need the debugger to be attached to the MSBuild (or dotnet build) process.
I found an article that explains why System.Diagnostics.Debugger.Launch();
does not work.
What you can do instead as a workaround is use
SpinWait.SpinUntil(() => Debugger.IsAttached);
When you build the project that uses the custom task, it will not continue the build until there is a debugger attached. At which point, your breakpoint will then be able to be hit and you can step through the Task.
I found this from this article: Implementing and Debugging Custom MSBuild Tasks
Huge kudos to the author; because this had me stuck for quite a while.
Upvotes: 1
Reputation: 835
You can set the environment variable MSBUILDDEBUGONSTART=1
to make MSBuild.exe
prompt to launch a debugger at its application startup.
You can then select the Visual Studio instance you're using to develop your task as the debugger, and set a breakpoint in your task's code.
This is generally a bit more difficult than modifying your task to call Debugger.Launch()
, but it can be used on release builds and without modifying a task, which can be useful.
There's one major caveat here: when you build with -m
, MSBuild will launch multiple processes, and each of them will prompt to attach a debugger. Try to build with -m:1
or build single projects when doing this, so that you don't accidentally spawn dozens of processes that want to be debugged.
Upvotes: 5
Reputation:
This is what I do... In the Project Properties dialog on the Debug Tab Select "Start an External App" - put C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe in the box..
Then in the command line parms, put your arguments /Target:Whatever test.proj
Put a code stop in your custom task and start the app..
Upvotes: 10
Reputation: 47749
It's a bit of a hack, but you could always just put this line of code wherever it is that you want to start debugging:
System.Diagnostics.Debugger.Launch();
When you invoke it, the CLR will launch a dialog asking you what debugger you want to attach.
Upvotes: 42