Debugging cake scripts

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.

In order to make the debugging possible it is necessary to add

preprocessor directive to the task we want the debugger to stop.

This operation doesn’t affect the default build process, so if you run in console

the build will run as usual. In order to be able to attach the debugger we have to call Cake with

flag. You can’t do it directly via

script so assuming that you didn’t change directory structure created by bootstrapper, go to Tools/Cake folder and run from PowerShell

where

is path to your cake script and

is task you want to start. Once you run the command, cake will wait for attaching the debugger
WaitingForDebugger
Now launch Visual Studio, go to Debug-> Attach to Process…, find process id listed in console
Attach
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.
attached

Debugging cake scripts