powershellvisual-studio-codevscode-problem-matcher

VSCode Powershell problemMatcher


I have a task for Powershell in VSCode, but can't figure out how to make the problemMatch work

{
    "version": "0.1.0",
    "command": "PowerShell.exe",
    "isShellCommand": true,
    "suppressTaskName": true,
    "args": [
        "& '${file}'"
    ],
    "tasks": [
    {
        "taskName": "Build",
        "isBuildCommand": true,
        "showOutput": "always",
        "fileLocation": ["absolute"],
        "problemMatcher": [
        {
            "pattern": {
            "regexp": "At (.*\\.ps1):(\\d*) char:(\\d*)(.*)\\n\\+(.*)\\n\\+(.*)\\n(.*)",
            "file": 1,
            "line": 2,
            "column": 3,
            "message": 7
            }
        }]
    }]
}

Regex targets as so :

At C:\tmp\C1-INT to C1-QA\a.ps1:1 char:11
+ "asdasds" !
+           ~
Unexpected token '!' in expression or statement.

file: Group 1 "C:\tmp\C1-INT to C1-QA\a.ps1"

line: Group 2 "1"

column: Group 3 "11"

message: Group 7 Unexpected token '!' in expression or statement.


Solution

  • I'm not sure that the regex for a problem matcher can handle line breaks. By default problem matchers are single line, but you can create multi-line matchers as described here: https://code.visualstudio.com/Docs/editor/tasks#_defining-a-multiline-problem-matcher

    Essentially you provide multiple regex's. For your scenario you could try something like the following:

    "problemMatcher": {
        "owner": "custom",
        "fileLocation": ["absolute"],
        "pattern": [{
            "regexp": "At (.*\\.ps1):(\\d*) char:(\\d*)(.*)",
            "file": 1,
            "line": 2,
            "column": 3                 
        }, {
            "regexp": "\\+.*"
        },{
            "regexp": "\\+.*"
        },{
            "regexp": "(.+)",
            "message": 1
        }]
    }
    

    The first pattern matches the file, line and column in the first line. The second and third patterns match the next two lines of output but don't capture any values. The final line matches the next output line and captures it all as the message.

    Hope that helps!