Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminate another task from within a postDebugTask - VS Code

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.

like image 263
Sandeep Kumar Avatar asked Feb 01 '19 13:02

Sandeep Kumar


2 Answers

This will terminate all tasks after debug stops

Or in this case, just 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"
    }
  ]
}

More info here: https://code.visualstudio.com/docs/editor/variables-reference#_command-variables

like image 98
Daniel M. Avatar answered Sep 18 '22 15:09

Daniel M.


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.

like image 38
Mark Avatar answered Sep 16 '22 15:09

Mark