【问题标题】:Terminate another task from within a postDebugTask - VS Code从 postDebugTask 中终止另一个任务 - VS Code
【发布时间】:2019-02-01 13:44:45
【问题描述】:

我有一个调试启动配置 (launch.json),如下所示。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Current TS File",
            "type": "node",
            "request": "launch",
            "preLaunchTask": "Pre Debug Task",
            "postDebugTask": "Stop Watch Styles",
            "args": ["${relativeFile}"],
            "runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
            "sourceMaps": true,
            "cwd": "${workspaceRoot}",
            "protocol": "inspector",
        }
    ]
}

我的任务配置(tasks.json)是这样的

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "npm",
            "script": "next:copy",
            "label": "Copy Files",
            "problemMatcher": []
        },
        {
            "type": "npm",
            "script": "styles:tsw",
            "label": "Watch Styles",
            "problemMatcher": [],
        },
        {
            "label": "Pre Debug Task",
            "isBackground": true,
            "dependsOn" :[
                "Copy Files",
                "Watch Styles"
            ]
        },
        {
            "label": "Stop Watch Styles",
            // No sure what should come here
        }
    ]
}

试图在postDebugTask 中停止监视进程,有没有办法通过在tasks.json 中提供名称(Watch Styles)作为参数来终止任务。手表样式是一个持续运行的过程,调试完成后是否有其他方法可以终止任务,请建议。

【问题讨论】:

    标签: visual-studio-code vscode-debugger vscode-tasks


    【解决方案1】:

    这将在调试停止后终止所有任务

    或者在这种情况下,只是build_runner watch...

    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": "Flutter",
          "request": "launch",
          "type": "dart",
          "flutterMode": "debug",
          "preLaunchTask": "flutter: build_runner watch",
          "postDebugTask": "Terminate All Tasks"
        }
      ]
    }
    

    tasks.json

    {
      // See https://go.microsoft.com/fwlink/?LinkId=733558
      // for the documentation about the tasks.json format
      "version": "2.0.0",
      "tasks": [
        {
          "label": "Terminate All Tasks",
          "command": "echo ${input:terminate}",
          "type": "shell",
          "problemMatcher": []
        }
      ],
      "inputs": [
        {
          "id": "terminate",
          "type": "command",
          "command": "workbench.action.tasks.terminate",
          "args": "terminateAll"
        }
      ]
    }
    

    更多信息在这里:https://code.visualstudio.com/docs/editor/variables-reference#_command-variables

    【讨论】:

      【解决方案2】:

      这对我有用:

      {
         "label": "postdebugKill",
         "type": "process",
         "command":[
            "${command:workbench.action.tasks.terminate}",
            "${command:workbench.action.acceptSelectedQuickOpenItem}",
         ],
      },
      

      第一个"${command:workbench.action.tasks.terminate}" 将弹出一个面板,要求您选择要终止的任务。因此,如果您有多个正在运行的任务并想选择一个,则只能使用此命令。

      第二个"${command:workbench.action.acceptSelectedQuickOpenItem}" 将终止上述面板中的选定任务。 (因此您甚至不会看到终止面板。)如果您在调用postdebugKill 任务时只有一个正在运行的任务,它将被自动选择并因此终止。否则,首先列出的任务将被终止。同样,如果您有多个其他任务正在运行并且您想要选择终止哪个任务,请不要包含第二个命令。

      我不知道有什么方法可以列出,可能是通过args 选项列出运行时要终止的任务的标签名称。要是有这个功能就好了。

      [postdebugKill 名称可以是任何你想要的。]

      要调用调试后任务,您的 launch.json 配置可能如下所示:

      {
         "type": "node",
         "request": "launch",
         "name": "Gulp: serve",
         "program": "${workspaceFolder}/node_modules/gulp/bin/gulp.js",
         "args": [
           "serve"
         ],
      
         //  "preLaunchTask": "someTask",
      
         "postDebugTask": "postdebugKill"
      },
      

      停止"Gulp: serve" 调试会话后,将触发任务"postdebugKill"。因此,如果我使用"preLaunchTask" 来启动任务,或者只是在启动"Gulp: serve" 调试会话之前启动了正在运行的任务,那么preLaunchTask 将被终止。

      最近在 vscode 中添加了在任务中运行 vscode 命令的功能。这里有一些最基本的信息:using a command in a task doc

      【讨论】:

      • 感谢您的回复。你的 VS Code 版本是多少。我的版本是 1.29.1,我尝试在调试后部分添加命令,对我来说它没有提示要求终止任务。我在终端看到了声明。我正在通过 Shift+F5 停止调试任务或使用停止图标进行调试> Executing task in folder my-app-folder: C:\Users\sandeep\Codebase\my-app-folder\${command:workbench.action.tasks.terminate} ${command:workbench.action.acceptSelectedQuickOpenItem}
      • 我使用的是 1.30.2。我做了一些研究,我会把它放在一个单独的答案中。
      • 当我尝试手动运行此任务时,VS Code 只是打开一个终端并记录The terminal shell path "${command:workbench.action.tasks.terminate} ${command:workbench.action.acceptSelectedQuickOpenItem}" does not exist。我正在使用 VS Code 版本 1.42.1
      【解决方案3】:

      [我将添加另一个答案,因为新的附加信息很广泛。]

      运行 preLaunchTask 然后启动调试会话似乎存在问题。具体见debugging does not work on first try。它似乎已在内部人员版本中得到修复,可能会在 2019 年 2 月上旬发布。terminal not sending onLineData

      与此同时,在我丢失链接的问题之一中,有一个建议的修复方法,它是一个问题匹配器,它将向调试器发出信号,表明相关任务已完成。

      在我的观看任务中,我使用了这个:

      "problemMatcher": [
         {
           "base": "$tsc-watch",
           "background": {
             "activeOnStart": true,
             "beginsPattern": "Using gulpfile ~\\OneDrive\\experimental\\gulpfile.js",
             "endsPattern": "Starting 'watch'..."
           }
         }
      ],
      

      我之所以选择它,是因为当我手动启动 gulp:watch 任务时,我会在终端中得到这个:

      [22:27:48] Using gulpfile ~\OneDrive\experimental\gulpfile.js
      [22:27:48] Starting 'watch'...
      [22:27:48] Starting 'watch'...
      

      所以你可以看到我复制的开始和结束模式(带有额外的转义)。

      我建议您单独运行“预调试任务”并将开始和结束输出复制到“预调试任务”问题匹配器中,看看它现在是否有效。

      我认为我在第一个答案中的代码是正确的,我只是没有像你一样使用“isBackground”和“dependsOn”。但我已将"isBackground" 添加到我的和problemMatcher 选项中,现在它可以完美运行。

      希望这将在 2019 年 2 月的下一个版本中得到修复,并且不需要此解决方法。

      【讨论】:

      • 感谢 Mark 抽出时间,我需要检查我的 watch 任务,尝试识别可以放入 issueMatcher 的模式。我希望这更简单:)。可能我会等待您建议您给出的单独答案。
      • 这是第二个答案。这个“可能”最早会在 2019 年 2 月 6 日修复,届时可能会发布 vscode 的下一次更新。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      相关资源
      最近更新 更多