How to make vscode not wait for finishing a preLaunchTask?

I have a debug setup in Visual Studio code where I run an external binary which can execute my JS files (using duktape). The debug adapter currently only supports attach requests (not launch) so I have to run the binary before I can debug the JS scripts.

In order to avoid having to start the application manually I created a task for it and set that in my launch.json file:

{
    "version": "0.2.0",
    "configurations": [{
        "name": "Attach MGA",
        "type": "duk",
        "preLaunchTask": "debug mga",
        "request": "attach",

        "address": "localhost",
        "port": 9091,

        "localRoot": "${workspaceRoot}",

        "stopOnEntry": false,
        "debugLog": true
    }]
}

The task is defined so:

{
    "version": "0.1.0",
    "command": "<absolute path to>/mga",
    "isShellCommand": false,
    "showOutput": "always",
    "suppressTaskName": true,
    "tasks": [{
        "taskName": "debug mga",
        "args": ["--debugger", "main.json"]
    }]
}

The problem is now that vscode waits for the pre launch task to finish, while the application waits for a debugger to attach. Catch 22.

How can I avoid that vscode waits for the pre launch task to finish?

Update:

Meanwhile I have read up on the vscode task page and came up with this task configuration. Still, it doesn't work for me

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "launch-mga",
            "type": "shell",
            "command": "<absolute path to>/mga",
            "args": [
                "config/main.json",
                "--debugger"
            ],
            "isBackground": true,
            "problemMatcher": {
                "owner": "custom",
                "pattern": {
                    "regexp": "_____"
                },
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": "^.*Waiting for debug connection.*$",
                    "endsPattern": "^.*blah.*$"
                },
            },
        }
    ]
}

The launched application prints the wait message and then waits endlessly for a debug connection. Maybe the problem has to do with the application (which is kinda Node.js like terminal app), written in C++?


This worked for me.

Note all these are required, even though none are important:

  • problemMatcher.pattern.regexp
  • problemMatcher.pattern.file
  • problemMatcher.pattern.location
  • problemMatcher.pattern.message
  • problemMatcher.background.activeOnStart
  • problemMatcher.background.beginsPattern
  • problemMatcher.background.endsPattern
{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build-extras",
      "type": "shell",
      "isBackground": true,
      "command": "./script/build-extras",

      // This task is run before some debug tasks.
      // Problem is, it's a watch script, and since it never exits, VSCode
      // complains. All this is needed so VSCode just lets it run.
      "problemMatcher": [
        {
          "pattern": [
            {
              "regexp": ".",
              "file": 1,
              "location": 2,
              "message": 3
            }
          ],
          "background": {
            "activeOnStart": true,
            "beginsPattern": ".",
            "endsPattern": ".",
          }
        }
      ]
    }
  ]
}

Background / watching tasks

Some tools support running in the background while watching the file system for changes and then triggering an action when a file changes on disk. With Gulp such functionality is provided through the npm module gulp-watch. The TypeScript compiler tsc has built in support for this via the --watch command line option.

To provide feedback that a background task is active in VS Code and producing problem results, a problem matcher has to use additional information to detect these state changes in the output. Let's take the tsc compiler as an example. When the compiler is started in watch mode, it prints the following additional information to the console:

> tsc --watch
12:30:36 PM - Compilation complete. Watching for file changes.

When a file changes on disk which contains a problem, the following output appears:

12:32:35 PM - File change detected. Starting incremental compilation...
src/messages.ts(276,9): error TS2304: Cannot find name 'candidate'.
12:32:35 PM - Compilation complete. Watching for file changes.

Looking at the output shows the following pattern:

  • The compiler runs when File change detected. Starting incremental compilation... is printed to the console.
  • The compiler stops when Compilation complete. Watching for file changes. is printed to the console.
  • Between those two strings problems are reported.
  • The compiler also runs once the initial start (without printing File change detected. Starting incremental compilation... to the console).

To capture this information, a problem matcher can provide a background property.

For the tsc compiler, an appropriate background property looks like this:

"background": {
    "activeOnStart": true,
    "beginsPattern": "^\\s*\\d{1,2}:\\d{1,2}:\\d{1,2}(?: AM| PM)? - File change detected\\. Starting incremental compilation\\.\\.\\.",
    "endsPattern": "^\\s*\\d{1,2}:\\d{1,2}:\\d{1,2}(?: AM| PM)? - Compilation complete\\. Watching for file changes\\."
}

In addition to the background property on the problem matcher, the task itself has to be marked as isBackground so that the task keeps running in the background.

A full handcrafted tasks.json for a tsc task running in watch mode looks like this:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "watch",
            "command": "tsc",
            "args": ["--watch"],
            "isBackground": true,
            "problemMatcher": {
                "owner": "typescript",
                "fileLocation": "relative",
                "pattern": {
                    "regexp": "^([^\\s].*)\\((\\d+|\\,\\d+|\\d+,\\d+,\\d+,\\d+)\\):\\s+(error|warning|info)\\s+(TS\\d+)\\s*:\\s*(.*)$",
                    "file": 1,
                    "location": 2,
                    "severity": 3,
                    "code": 4,
                    "message": 5
                },
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": "^\\s*\\d{1,2}:\\d{1,2}:\\d{1,2}(?: AM| PM)? - File change detected\\. Starting incremental compilation\\.\\.\\.",
                    "endsPattern": "^\\s*\\d{1,2}:\\d{1,2}:\\d{1,2}(?: AM| PM)? - Compilation complete\\. Watching for file changes\\."
                }
            }
        }
    ]
}

PS: Content taken from https://code.visualstudio.com/docs/editor/tasks

Edit-1

The task needs to be launched as a daemon then only isBackground is going to help. So you will have something like

"isShellCommand": true,
"command": "<absolute path to>/mga --config xyz abc &",