We’ve been using Cake in our project for quite some time and it has been working great. However for time to time lack of debugging support was really annoying for me. Fortunately it seems that these times are gone now. During NDC Oslo 2016 Gary Ewan Park showed that it is possible to attach debugger to the build process. Here are the steps to achieve that. Let’s assume that we have following simple cake script.
1 2 3 4 5 6 7 8 |
var target = Argument("target", "Default"); Task("Default") .Does(() => { MSBuild(Directory("../") + File("DebuggingCakeScript.sln")); }); RunTarget(target); |
In order to make the debugging possible it is necessary to add
1 |
#break |
preprocessor directive to the task we want the debugger to stop.
1 2 3 4 5 6 7 8 9 |
var target = Argument("target", "Default"); Task("Default") .Does(() => { #break MSBuild(Directory("../") + File("DebuggingCakeScript.sln")); }); RunTarget(target); |
This operation doesn’t affect the default build process, so if you run in console
1 |
.\build.ps1 -target Default |
the build will run as usual. In order to be able to attach the debugger we have to call Cake with
1 |
--debug |
flag. You can’t do it directly via
1 |
\build.ps1 |
script so assuming that you didn’t change directory structure created by bootstrapper, go to Tools/Cake folder and run from PowerShell
1 |
.\Cake.exe ..\..\build.cake -target=Default --debug |
where
1 |
..\..\build.cake |
is path to your cake script and
1 |
Default |
is task you want to start. Once you run the command, cake will wait for attaching the debugger
Now launch Visual Studio, go to Debug-> Attach to Process…, find process id listed in console
and click Attach. After couple of seconds Visual Studio will load your cake script and you will be able to debug it like normal application.