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

Adding a build task to the Launch.Json


What you are looking for is probably how to link a build task to your debug config. I’ll try to illustrate the process below.

debug configs

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).

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.

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}"
    }
  ]
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)