How to debug a Cake file using Visual Studio Code

Published
Tuesday, 27 September 2016
Category
How To's
Author
mholo65

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:

  1. Install Cake.CoreCLR NuGet package to your tools folder
  2. Install Cake Extension for Visual Studio Code
  3. Set up .NET Core debugger in Visual Studio Code. See http://aka.ms/vscclrdebugger for details
  4. Open the directory containing your Cake files in Visual Studio Code
  5. Create file .vscode/launch.json and add the following content (assuming your Cake file is build.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
            }
        ]
}
  1. Open your Cake file and add a breakpoint by hitting F9
  2. Hit F5 to start debugging
    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:

  1. 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}"
}
  1. Open your Cake file and add a breakpoint by hitting F9
  2. Select the configuration you added in the previous step
    Select config
  3. Open terminal and run
    dotnet tools/Cake.CoreCLR/Cake.dll build.cake --debug
  4. Hit F5 and select the process from the drop-down list
    Select process

Example Code

A working example can be found here