在 Visual Studio Code 中调试之前如何自动运行构建任务? [英] How run build task automatically before debugging in Visual Studio Code?

查看:52
本文介绍了在 Visual Studio Code 中调试之前如何自动运行构建任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VS Code 中,我必须先运行构建任务,然后开始调试,而在 CLion 中,我只需单击调试,然后它会在必要时自动构建并开始调试.有没有办法在 VS 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?

推荐答案

向 Launch.Json 添加构建任务


我对这个主题不够熟悉,无法逐步引导您完成整个过程,但是您正在寻找的可能是如何将构建任务链接到您的调试配置.我将尝试说明下面的过程.

Adding a build task to the Launch.Json


I am not familiar enough on the subject to walk you through the process step-by-step, however 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.

要访问您的构建配置,请转到侧面的调试栏 (1),然后按齿轮图标访问您的 launch.json 配置文件 (2).您需要在该 launch.json 文件中的配置下添加一个预启动任务,并将其链接到您的构建任务 (3).

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

然后,您需要设置构建任务,使用 ctrl-shift-b 运行它.

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

如果它已经存在(如您的帖子中所暗示的那样),您可以在 tasks.json 文件的 .vs-code 文件夹中找到它.如果您打开该 task.json 文件,您将在列表中找到构建任务.您现在需要做的就是获取该任务的标签"并将其放置在启动前配置中的 launch.json 中.

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.

祝你好运!

附录添加了示例,用于在共享的预启动构建之后并行构建和运行配置.

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:@JoKing 给出的潜在解决方案:添加一个新任务,删除二进制文件并在每次构建之前通过在构建任务中使用dependsOn"要求它来执行此任务.选项.下面给出了一个示例,说明它在 tasks.json 文件中的外观,source

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:我之前没有亲自设置过,但是复合启动配置 可能是您正在寻找的.该页面中的示例有两个配置,服务器"和客户端",它们可以在遵循 prelaunchTask ('defaultBuildTask') 的同时并行启动.

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

这篇关于在 Visual Studio Code 中调试之前如何自动运行构建任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆