Search code examples
visual-studio-code

How run build task automatically before debugging in Visual Studio Code?


In VS Code I have to run the build task first and then start debugging, while in CLion I just click debug, then it builds automatically if necessary and starts debugging. Is there a way to automate this in VS Code as well?


Solution

  • Prerequisite Config


    First, if you do not yet have VSCode set up for debugging, follow this link for C++ setup on Windows with VSCode. If you have a different OS or language, you can also find it on the left sidebar of this page.

    Adding a build task to the Launch.Json


    To run a build task automatically before debugging, you will need to link a build task to your debug config. I'll try to illustrate the process below.

    debug configs

    First to access your build configs, go to the Debug bar on the side (1), and press the gear icon to access your launch.json config file (2). You will need to add a pre-launch task under your configurations in that launch.json file, and link it to your build task (3).

    The string, "buildTaskLinkHere!" here represents the label of that task.

    Defining the Build Task in Tasks.Json


    tasks.json file

    Then, you will need to set up your build task, by running it with ctrl-shift-b.

    If it already exists (as implied in your post), you can find it in your .vs-code folder in the tasks.json file. If you open that task.json file, you will find the build task in the list.

    All you need to do now is to take the 'label' of that task and place it in your launch.json in that pre-launch config, replacing that "buildTaskLinkHere!" label mentioned in the step before.

    Good luck!

    Appendix


    Appendix added with examples for clean build and running configs in parallel following a shared pre-launch build.

    Q: What to do if the build task fails, but the launch process starts with the old binary?

    A: Potential solution given by @JoKing: add a new task that deletes the binary and execute this task before each build by requiring it in the build task with the "dependsOn" option. An example is given below for how it might look in the tasks.json file, source

    {
       "tasks": [
        {
          "taskName": "build",
          "command": "tsc",
          "group": {
            "kind": "build",
            "isDefault": true
          },
          "dependsOn": [
            "build client",
            "build server"
          ]
        },
        {
          "taskName": "build client",
          "command": "tsc",
          "args": [
            "-w",
            "-p",
            "${workspaceRoot}/src/typescript/client"
          ]
        },
        {
          "taskName": "build server",
          "command": "tsc",
          "args": [
            "-w",
            "-p",
            "${workspaceRoot}/src/typescript/server"
          ]
        }
      ]
    }
    

    Q: I have multiple configurations, but want to run build task to run once before all the configurations, is it possible?

    A: I have not personally set this up before, but compound launch configurations may be what you are looking for. The example from that page has two configurations, 'Server' and 'Client', which can be launched in parallel while following the prelaunchTask ('defaultBuildTask').

    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Server",
          "program": "${workspaceFolder}/server.js"
        },
        {
          "type": "node",
          "request": "launch",
          "name": "Client",
          "program": "${workspaceFolder}/client.js"
        }
      ],
      "compounds": [
        {
          "name": "Server/Client",
          "configurations": ["Server", "Client"],
          "preLaunchTask": "${defaultBuildTask}"
        }
      ]
    }