9

launch.json

...
"preLaunchTask": "startDebug",
"postDebugTask": "closeOpenOCDTerminal"

tasks.json

{
"version": "2.0.0",
"tasks": [
    {
        "label": "startDebug",
        "type": "shell",
        "command": "make -j4; openocd -f interface/cmsis-dap.cfg -c 'transport select swd' -f target/stm32f1x.cfg",
        "isBackground": true,
        "problemMatcher": {
            "pattern": {
                "regexp": "."
            },
            "background": {
                "activeOnStart": true,
                "beginsPattern": ".",
                "endsPattern": "."
            }
        }
    },
    {
        "label": "closeOpenOCDTerminal",
        "type": "process",
        "command":[
            "${command:workbench.action.tasks.terminate}",
            // "${command:workbench.action.acceptSelectedQuickOpenItem}" //invalid
         ]
    }
]
}

The OpenOCD service does not end automatically like the make command, only depends on closing the terminal or executing Ctrl + C. How to automatically close the task terminal after debugging? Or execute Ctrl + C command automatically in the task terminal to end the Openocd service. The currently used closeOpenOCDTerminal method requires manual clicking on the pop-up list.

1
  • 2
    Is your startDebug in launch.json or tasks.json? Shouldn't it be in launch.json? Commented Feb 17, 2020 at 4:49

2 Answers 2

8

The tasks.json file in VSCode (1.57) now has a property called close in the presentation property. And you can use it to close the terminal.

Automatically close task terminals

The task presentation property has a new close property.
Setting close to true will cause the terminal to close when the task exits.

{
  "type": "shell",
  "command": "node build/lib/preLaunch.js",
  "label": "Ensure Prelaunch Dependencies",
  "presentation": {
      "reveal": "silent",
      "close": true
  }
}

TIP

Note that it is recommended to switch to the "Problems" tab in the terminal after using the close option - Reference

Sign up to request clarification or add additional context in comments.

Comments

2
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "start",
            "type": "shell",
            "command": "make -j4; openocd -f interface/cmsis-dap.cfg -c 'transport select swd' -f target/stm32f1x.cfg",
            "isBackground": true,
            "problemMatcher": {
                "pattern": {
                    "regexp": "."
                },
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": ".",
                    "endsPattern": "."
                }
            }
        },
        {
            "label": "stop",
            "command": "echo ${input:terminate}",
            "type": "shell",
        }
    ],
    "inputs": [
        {
          "id": "terminate",
          "type": "command",
          "command": "workbench.action.tasks.terminate",
          "args": "terminateAll"
        }
    ]
}

This example happens to close the task terminal.

1 Comment

That works! don't forget to add the postDebugTask on the launch.json file: "postDebugTask": "stop"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.