【问题标题】:Using "preLaunchTasks" and Naming a Task in Visual Studio Code在 Visual Studio Code 中使用“preLaunchTasks”和命名任务
【发布时间】:2016-05-21 12:03:09
【问题描述】:

根据the documentation,可以在调试前启动程序:

要在每个调试会话开始之前启动任务,请将 preLaunchTask 设置为 tasks.json 中指定的任务之一的名称

我没有看到“命名”任务的示例语法,但schema documentation 显示了一个名为taskName 的属性。我尝试使用它将我的 launch.json preLaunchTasks 链接到任务,但它没有工作。当我启动我的程序时,Visual Studio Code 报告了这个错误:

找不到独特的任务“启动核心”。确保任务存在并且具有唯一的名称。

我的自定义“命名”任务如下所示:

{
    "taskName": "launch-core",
    "version": "0.1.0",
    "command": "C:\\utils\\mystuff.exe",
    // The command is a shell script
    "isShellCommand": true,
    // Show the output window only if unrecognized errors occur.
    "showOutput": "silent",
}

然后我尝试将属性名称从 taskName 更改为 namebased on this link。那也没用。

Intellisense 没有给出如何命名任务的建议。

有人知道如何在 tasks.json 文件中唯一地命名一个任务吗?语法是什么?属性名称是什么?

最终我想在我自己的 node.js 应用程序启动之前执行两个或三个 node.js 进程。例如,我想在我的应用程序启动到调试器之前启动以下三个应用程序:

sh -c 'cd ./manager/ && node manager.js'
sh -c 'cd ./adapter/ && node adapter.js'
sh -c 'cd ./core/ && node core.js'

如果我在 Windows 机器上工作,我的任务可能如下所示:

{
    "taskName": "core-launch",
    "version": "0.1.0",

    // The command is tsc. Assumes that tsc has been installed using npm install -g typescript
    "command": "start",

    // The command is a shell script
    "isShellCommand": true,

    // Show the output window only if unrecognized errors occur.
    "showOutput": "silent",

    // args is the HelloWorld program to compile.
    "args": [
        "ACD-Manager",
        "/B",
        "/D",
        "./manager/",
        "node",
        "manager.js"
        ]
}

上述任务使用cmd start capability。我还不确定如何启动多个节点任务而不是一个,但由于这个任务命名问题,我什至无法启动一个任务。

如何在 tasks.json 文件中命名任务?

【问题讨论】:

    标签: node.js visual-studio-code


    【解决方案1】:

    对于vscode1.36.1 (1.36.1)

    tasks.json:

    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "build:tsc",
          "type": "npm",
          "script": "build:tsc"
        },
        {
          "label": "clean",
          "type": "npm",
          "script": "clean"
        },
        {
          "label": "build",
          "dependsOrder": "sequence",
          "dependsOn": ["clean", "build:tsc"]
        }
      ]
    }
    

    launch.json:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Launch Program",
          "program": "${workspaceFolder}/dist/main.js",
          "preLaunchTask": "build",
          "outFiles": ["${workspaceFolder}/dist/**/*.js"],
          "console": "integratedTerminal"
        }
      ]
    }
    

    在运行node ${workspaceFolder}/dist/main.js之前,preLaunchTask将首先运行build任务,其中包括两个子任务:cleanbuild。它将首先运行clean 任务,然后运行build 任务。

    您可以将任务的标签指定为preLaunchTask 选项。

    【讨论】:

    • 这是否意味着build:tsc 将在clean 之后运行?
    【解决方案2】:

    问题标题是:

    “在 Visual Studio Code 中使用“preLaunchTasks”并命名任务

    我需要定义 preLaunchTask***s***。

    您可以使用 here 中描述的 dependsOn 属性配置多个任务

    例如,您的 tasks.json 中的复合任务:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Client Build",
                "command": "gulp",
                "args": ["build"],
                "options": {
                    "cwd": "${workspaceRoot}/client"
                }
            },
            {
                "label": "Server Build",
                "command": "gulp",
                "args": ["build"],
                "options": {
                    "cwd": "${workspaceRoot}/server"
                }
            },
            {
                "label": "Build",
                "dependsOn": ["Client Build", "Server Build"]
            }
        ]
    }
    

    您可以找到有关命名任务here 的更多信息。

    【讨论】:

      【解决方案3】:

      对于 2.0.0 版配置,您现在使用 label 而不是 taskName

      package.json:

      ...
      "scripts": {
          "tsc": "tsc",
          ...
      }
      ...
      

      launch.json(我的源码在src目录,tsc编译到dist目录):

      {
          "version": "0.2.0",
          "configurations": [
              {
                  "type": "node",
                  "request": "launch",
                  "preLaunchTask": "Compile",
                  "name": "Launch Program",
                  "program": "${workspaceFolder}/src/index.ts",
                  "outFiles": [
                      "${workspaceFolder}/dist/**/*.js"
                  ],
                  "protocol": "inspector",
                  "sourceMaps": true
              }
          ]
      }
      

      tasks.json:

      {
          "version": "2.0.0",
          "tasks": [
              {
                  "label": "Compile",
                  "type": "npm",
                  "script": "tsc",
                  "problemMatcher": []
              }
          ]
      }
      

      【讨论】:

        【解决方案4】:

        FWIW,我正在使用 VS Code 1.20.1,以下是我的 preLaunchTask 工作方式:

        launch.json:

        {
          "version": "0.2.0",
          "configurations": [
            {
                "type": "node",
                "request": "launch",
                ...
                "preLaunchTask": "npm: build",
            }
          ]
        }
        

        在我的package.json

        {
          ...
          "scripts": {
             "build": "tsc"
             ...
          }
        }
        

        【讨论】:

          【解决方案5】:

          所以,如果它仍然相关,或者如果有人发现这个线程有同样的问题,我刚刚弄清楚它是如何工作的:

          tasks.json 中,您需要创建一个 'tasks' 数组 - 代码提示会帮助您 - 它包含一个对象数组。在对象内部,您可以拥有 'taskName' 键值对。

          例子:

          {
              "version": "0.1.0",
              "command": "npm",
              "isShellCommand": true,
              "args": ["run-script", "webpack"],
              "showOutput": "always",
              "tasks": [
                  { 
                      "taskName": "runwebpack",
                      "suppressTaskName": true
                  }
              ]
          }
          

          在我的例子中,我必须在运行我的项目之前运行npm run-script webpack 命令。 在 launch.json 文件中,"preLaunchTask": "runwebpack" 现在可以工作了。

          注意:suppressTaskName 在我的示例中是正确的。省略它或将其设置为 false 将导致 VS Code 在命令后附加 taskName

          更通用的方法是这样的:

          {
              "version": "0.1.0",
              "command": "npm",
              "isShellCommand": true,
              "args": ["run-script"],
              "showOutput": "always",
              "tasks": [
                  { "taskName": "webpack" }
              ]
          }
          

          对于后一个示例,您可以使用其他要运行的脚本来扩展 tasks 数组。

          我的使用提示:npm run-script 从 package.json 文件的 scripts 对象中获取要执行的操作。

          编辑:这适用于 VS Code 1.3.1

          【讨论】:

          【解决方案6】:

          我只真正看到了与 Gulp 相关的 taskName;我敢肯定还有其他人,但没有什么我有太多见识的。也许这可以让您从已有的开始?

          Run a pre-launch task in VSCODE

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-10-20
            • 2016-02-15
            • 1970-01-01
            • 1970-01-01
            • 2018-09-10
            • 2019-01-07
            • 2017-09-25
            相关资源
            最近更新 更多