Terminate another task from within a postDebugTask - VS Code

佐手、 提交于 2019-12-13 03:56:55

问题


I have a debug launch configuration (launch.json) like below.

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

My tasks configuration (tasks.json) is like

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

Trying to stop watch process in postDebugTask, is there a way to Terminate Task by providing name (Watch Styles) as parameter in tasks.json. Watch styles is a continuously running process, please suggest if there is any other approach to terminate a task after debugging is complete.


回答1:


This works for me:

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

The first "${command:workbench.action.tasks.terminate}" will bring up a panel asking you to select which task to terminate. So if you had multiple running tasks and wanted to choose one you would use this command only.

The second "${command:workbench.action.acceptSelectedQuickOpenItem}" will terminate the selected task in that panel mentioned above. (So you will not even see the terminate panel.) If you have only one running task when you call the postdebugKill task, it will be selected automatically and thus terminated. Otherwise whichever task is listed first will be terminated. Again, if you have more than one other task running and you want to select which to terminate don't include this second command.

I don't know of any way to list, perhaps via an args option a label name for which task to terminate if running. It would be nice to have this functionality.

[postdebugKill name can be whatever you want.]

To call a postdebug task your launch.json config might look like this:

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

   //  "preLaunchTask": "someTask",

   "postDebugTask": "postdebugKill"
},

Upon stopping that "Gulp: serve" debugging session, the task "postdebugKill" will be triggered. And so if I had used the "preLaunchTask" to start a task or had simply started a task running before launching the "Gulp: serve" debugging session - that preLaunchTask would be terminated.

The ability to run vscode commands in a task was recently added to vscode. Some minimal info here: using a command in a task doc.




回答2:


[I'll add another answer because the new additional info is extensive.]

It seems there is an issue with running a preLaunchTask and then launching a debugging session. See particularly debugging does not work on first try. It appears to be fixed though in an insiders edition and may be released in early February 2019. terminal not sending onLineData.

In the meantime there is a suggested fix within one of the issues that I have lost the link to and that is a problemMatcher which will signal to the debugger that the dependent tasks have completed.

In my watching task I used this:

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

I chose that because when I manually start the gulp:watch task I get this in the terminal:

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

So you see the start and end pattern there that I copied (with extra escaping).

I suggest that you separately run your "Pre Debug Task" and copy the start and ending output into your "Pre Debug Task" problemMatcher and see if it now works.

My code in the first answer I believe is correct, I just wasn't using "isBackground" and "dependsOn" as you are. But I have added "isBackground" to mine and the problemMatcher option and it works flawlessly now.

Hopefully, this will be fixed in the next February 2019 release and there will be no need for this workaround.



来源:https://stackoverflow.com/questions/54480792/terminate-another-task-from-within-a-postdebugtask-vs-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!