Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 945
smichaud Avatar asked Feb 21 '26 03:02

smichaud


1 Answers

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!

like image 167
Stuart Leeks Avatar answered Feb 23 '26 16:02

Stuart Leeks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!