As you might already know, debugging Cake scripts using Visual Studio has been supported since the v0.12.0 release of Cake. But since the v0.16.1 release of Cake, thanks to porting Cake to .NET Core, it is now also possible to debug Cake files using Visual Studio Code.
What does this mean?
This means that while creating/modifying your Cake build script, you can use Visual Studio Code to step into the Cake file and get full debug support regardless of which operating system you are running (Windows / Linux / Mac).
How does this work?
In order to enable debugging of a Cake file using Visual Studio Code, follow these steps:
- Install Cake.CoreCLR NuGet package to your
tools
folder - Install Cake Extension for Visual Studio Code
- Set up .NET Core debugger in Visual Studio Code. See http://aka.ms/vscclrdebugger for details
- Open the directory containing your Cake files in Visual Studio Code
- Create file
.vscode/launch.json
and add the following content (assuming your Cake file isbuild.cake
)
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"program": "${workspaceRoot}/tools/Cake.CoreCLR/Cake.dll",
"args": [
"${workspaceRoot}/build.cake",
"--debug",
"--verbosity=diagnostic"
],
"cwd": "${workspaceRoot}",
"stopAtEntry": true,
"externalConsole": false
}
]
}
- Open your Cake file and add a breakpoint by hitting
F9
- Hit
F5
to start debugging
Alternative Approach
Another way to enable debugging of a Cake script is to start Cake from the commandline and then attach the debugger from within Visual Studio Code. This can be done using the following steps:
- Add the following
configuration
to the.vscode/launch.json
file that you created in the previous steps
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command.pickProcess}"
}
- Open your Cake file and add a breakpoint by hitting
F9
- Select the
configuration
you added in the previous step - Open terminal and run
dotnet tools/Cake.CoreCLR/Cake.dll build.cake --debug
- Hit
F5
and select the process from the drop-down list
Example Code
A working example can be found here