Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 494
Svilen Avatar asked Sep 11 '19 14:09

Svilen


People also ask

How do I auto compile in Visual Studio Code?

If pressing Ctrl + Shift + B seems like a lot of effort, you can switch on "Auto Save" (File > Auto Save) and use NodeJS to watch all the files in your project, and run TSC automatically. And hey presto, each time VS Code auto saves the file, TSC will recompile it. Save this answer.

How do I skip a step while debugging in Visual Studio?

You can also click on the line you want to skip to and hit Ctrl+F10 (Run to Cursor). It will jump directly to that line.

How do I enable run and debug code in Visual Studio?

To run or debug a simple app in VS Code, select Run and Debug on the Debug start view or press F5 and VS Code will try to run your currently active file. However, for most debugging scenarios, creating a launch configuration file is beneficial because it allows you to configure and save debugging setup details.


2 Answers

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}"
    }
  ]
}
like image 195
Enthus3d Avatar answered Sep 22 '22 04:09

Enthus3d


It took me some time to understand the accepted answer. It was not clear to me from that explanation the exact way that would make my program build and debug with 1 click. Also I'm using Mingw-w64 in Windows. Following the instructions on this link and based on the accepted answer I created the following files:

launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            
            "preLaunchTask": "build"
        }
    ]
}

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "build",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
        }
    ]
}

The key aspect is to rename the default "label": "C/C++: g++.exe build active file" in tasks.json, for something else, for instance I used the word "build" and later reference that same word (not the path or link to tasks.json) in "preLaunchTask": "build" inside launch.json.

Notice the renaming is not strictly necessary. You can also say "preLaunchTask": "C/C++: g++.exe build active file" inside launch.json and it will also work.

like image 44
VMMF Avatar answered Sep 21 '22 04:09

VMMF