-1

CONTEXT

I'm trying to use Visual Studio Code's (VSCode) tasks (tasks.json) and better understand how the problemMatcher settings work or if they work at all.

For this purpose, I've created the following setup in Visual Studio Code (VSCode) to test...

NOTES:
I - The official documentation for Visual Studio Code (VSCode) didn't help me much in understanding this functionality.
II - The information about Python is for illustrative purposes only.
III - The Bash scripts simulate operations that are intended to be performed, and they handle error situations.

FILES

test-vscode-tasks
    .vscode
        launch.json
        tasks.json
    .debug
        build.bash
        start.bash
        stop.bash

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debug",
            "type": "python",
            "request": "attach",
            "port": 5890,
            "host": "127.0.0.1",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/var/task"
                }
            ],
            "preLaunchTask": "start",
            "postDebugTask": "stop"
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "bash ${workspaceFolder}/.debug/build.bash",
            "isBackground": true,
            "problemMatcher": [
                {
                    "pattern": [
                        {
                            "regexp": "\\b\\B",
                            "file": 1,
                            "location": 2,
                            "message":3
                        }
                    ],
                    "background": {
                        "activeOnStart": true,
                        "beginsPattern": "^.*BUILD*",
                        "endsPattern": "^.*ERROR*"
                    }
                }
            ],
            "presentation": {
                "reveal": "silent",
                "revealProblems": "onProblem"
            }
        },
        {
            "label": "start",
            "type": "shell",
            "command": "bash ${workspaceFolder}/.debug/start.bash",
            "isBackground": true,
            "dependsOrder": "sequence",
            "dependsOn": [
                "build"
            ],
            "problemMatcher": [
                {
                    "pattern": [
                        {
                            "regexp": "\\b\\B",
                            "file": 1,
                            "location": 2,
                            "message": 3
                        }
                    ],
                    "background": {
                        "activeOnStart": true,
                        "beginsPattern": "^.*START*",
                        "endsPattern": "^.*ERROR*"
                    }
                }
            ],
            "presentation": {
                "reveal": "always",
                "revealProblems": "onProblem"
            }
        },
        {
            "label": "stop",
            "type": "shell",
            "command": "bash ${workspaceFolder}/.debug/stop.bash",
            "isBackground": true,
            "problemMatcher": [
                {
                    "pattern": [
                        {
                            "regexp": "\\b\\B",
                            "file": 1,
                            "location": 2,
                            "message": 3
                        }
                    ],
                    "background": {
                        "activeOnStart": true,
                        "beginsPattern": "^.*STOP*",
                        "endsPattern": "^.*ERROR*"
                    }
                }
            ],
            "presentation": {
                "reveal": "silent",
                "revealProblems": "onProblem"
            }
        }
    ]
}

build.bash

#!/bin/bash

BUG_MOCK_BUILD=1
sleep 10
if [ "$BUG_MOCK_BUILD" -eq 1 ]; then
    echo " > ------------------- 
BUILD
ERROR
 < ------------------- "
    sleep 5
    exit 1
else
    echo " > ------------------- 
BUILD
SUCCESS
< ------------------- "
    sleep 5
    exit 0
fi

start.bash

#!/bin/bash

BUG_MOCK_STAR=1
sleep 10
if [ "$BUG_MOCK_STAR" -eq 1 ]; then
    echo " > ------------------- 
START
ERROR
 < ------------------- "
    sleep 5
    exit 1
else
    echo " > ------------------- 
START
SUCCESS
 < ------------------- "
    sleep 5
    exit 0
fi

stop.bash

#!/bin/bash

BUG_MOCK_STOP=1
sleep 10
if [ "$BUG_MOCK_STOP" -eq 1 ]; then
    echo " > ------------------- 
STOP
ERROR
 < ------------------- "
    sleep 5
    exit 1
else
    echo " > ------------------- 
STOP
SUCCESS
 < ------------------- "
    sleep 5
    exit 0
fi

QUESTIONS:

I - If the "build" fails, why does the IDE proceed to "start" (see image)?

II - Is it possible to use "problemMatcher" in reverse? In other words, proceed to the next task only if the previous one was successful?

Thanks!

VSCode tasks

rioV8
  • 24,506
  • 3
  • 32
  • 49
Eduardo Lucio
  • 1,771
  • 2
  • 25
  • 43
  • 1
    I could be wrong, but IIRC the task success is indicated by its exit code alone, while the problem matchers just populate the error list. – HolyBlackCat Aug 07 '23 at 20:52
  • I would leave this dependency logic to the specialized tools (aka `make`), and use `tasks.json` only to call it. – HolyBlackCat Aug 07 '23 at 20:54
  • @HolyBlackCat "the task success is indicated by its exit code alone" -> Yes and no. Within "start.bash" VSCode can also use "problemMatcher" to find out if it can attach the debugger, for example. If so VSCode would wait indefinitely... In my view, the way "problemMatcher" works needs better documentation. – Eduardo Lucio Aug 08 '23 at 18:28
  • @HolyBlackCat "the task success is indicated by its exit code alone" -> Yes, I managed to prove that, but I also realized that as soon as "problemMatcher" becomes true VSCode moves on to the next task even if the current task is still running execution (the exit code has not yet been provided). – Eduardo Lucio Aug 08 '23 at 18:29
  • @HolyBlackCat Regarding the non-execution of the next activities (the previous one failed), this can only be done by the bash script itself, identifying that the previous one did not have a successful execution. This can be done using an output file, for example. – Eduardo Lucio Aug 08 '23 at 18:32

0 Answers0