I have a bunch of custom build tasks in VS Code and I want a custom problem matcher for the C++ ones.
So I decided to take the default problem matcher example in VS Code docs (https://code.visualstudio.com/Docs/editor/tasks#_defining-a-problem-matcher) and modified it slightly.
"problemMatcher": {
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^(\\.\\.\\/+)(.*):(\\d+):(\\d+):\\s+(.*):\\s+(.*)$",
"file": 2,
"line": 3,
"column": 4,
"severity": 5,
"message": 6
}
My error messages have the following structure:
../../../module/src/module/specific/File.cpp:155:31: error: errorMessage
The three directory-up instructions (../../../) bring me back to ${workspaceFolder}. So the idea was to use the second capture group as a relative path from my workspace folder to follow up to the file.
Unfortunately, the file path does not highlight and it doesn't tell me to Ctrl+click to follow the message. I double checked the regex on https://regexr.com/ and it seems to be correct. I tried using the the default problem matcher too, without any luck.
This is the full build task:
{
"label": "build_c++",
"type": "shell",
"command": "${workspaceFolder}/build_command",
"problemMatcher": [ {
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^(\\.\\.\\/+)(.*):(\\d+):(\\d+):\\s+(.*):\\s+(.*)$",
"file": 2,
"line": 3,
"column": 4,
"severity": 5,
"message": 6
}
}],
"group": {
"kind": "build",
"isDefault": true
}
},
Any ideas?
Related
I am trying to setup a problemMatcher for a build task inside VSCode, the command generate messages like the following:
14 : ../../Dsrc/path/to/file.h:44:58: error: class ‘tf::ARandomClass’ does not have any field named ‘epsilon’
Using https://regex101.com/ I constructed a the following expression to build the problem matcher: \.\.\/\.\.\/Dsrc\/(.*?):(.*?):(.*?): (.*?): (.*?)$
However, the following problem matcher inside VS Code does not work:
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "\.\.\/\.\.\/Dsrc\/(.*?):(.*?):(.*?): (.*?): (.*?)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
Even if I remove the \.\.\/\.\.\/Dsrc\/ part, it does seems do match anything inside the terminal. What I am doing wrong ?
try the regex:
^\d+\s*:\s*([^:]+):(\d+):(\d+): (.*?): (.*?)$
Because the configuration file is JSON, you need to escape your escapes in the regexp value (i.e., double up your backslashes):
"regexp": "\\.\\.\\/\\.\\.\\/Dsrc\\/(.*?):(.*?):(.*?): (.*?): (.*?)$",
I have written a problemMatcher in tasks.json that looks like this:
"problemMatcher": {
"owner": "myFileExtension",
"pattern": {
"regexp": "myRegExp",
"file": 1,
"line": 2,
"severity": 3,
"message": 4,
"location": 2
}
}
I am using this problem matcher to squiggle the lines that have problems after I run my build task. However, instead of squiggling the whole line, I would like to squiggle a particular range, based on where the problem actually comes from. After reading the documentation, I am still not sure how this can be done.
How can I squiggle a range in tasks.json?
See last example in the doc.
The location can be 1, 2 or 4 numbers enclosed in ()
1: (line)
2: (line,char)
4: (lineStart,charStart,lineEnd,charEnd)
{
"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+,\\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\\."
}
}
}
]
}```
The following Tasks.json
Problem matcher regular expression should match the following typical warning. But it doesn't.
ctc W505: ["somedirectory/somefile.c" 350/18] implicit declaration blah blah blah
What is the issue ? I verified the built-in parser matches gcc output errors and warnings.
Thanks,
Satish K
"tasks": [
{
"label": "build.bat",
"type": "shell",
"command": "build.bat",
"problemMatcher": {
"owner": "cpptools",
"fileLocation": [
"relative",
"${env:PWD}"
],
"pattern": {
"regexp": "^ctc W(\\d+): \\[\\\"(.*)\\\" (\\d+)\\\/(\\d+)\\] (.*)$",
"file": 2,
"line": 3,
"column": 4,
"message": 5
}
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
You add an additional \ to the expression, you only need to escape the " and you don't need to escape /
"tasks": [
{
"label": "build.bat",
"type": "shell",
"command": "build.bat",
"problemMatcher": {
"owner": "cpptools",
"fileLocation": [
"relative",
"${env:PWD}"
],
"pattern": {
"regexp": "^ctc W(\\d+): \\[\"(.*)\" (\\d+)/(\\d+)\\] (.*)$",
"file": 2,
"line": 3,
"column": 4,
"message": 5
}
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
Edit
This works in regex101 (flavor Javascript)
^ctc W(\d+): \["(.*)" (\d+)\/(\d+)\] (.*)$
To translate it to a JSON string, escape \" and regex also wants you to escape / but that would only be needed if you use the regex in a literal Javascript (like /\d+/g) but we don't do that in VSC, but it won't hurt.
Resulting in:
"^ctc W(\\d+): \\[\"(.*)\" (\\d+)\\/(\\d+)\\] (.*)$"
I couldn't get this to work. But I ran the working regex101 through a json escpare. It did some more escaping of slashes and ended up as:
"regexp": "^ctc W(\\d+): \\[\\\"(.*)\\\" (\\d+)\\/(\\d+)\\] (.*)$",
I am trying to match errors of this format for the VSCode ProblemMatcher...
C:\projects\folder\main.cpp(6) : Error[AA000]: identifier "level2" is undefined
C:\projects\folder\main.cpp(7) : Error[AA000]: identifier "level3" is undefined
With regex101.com I am able to match what I need with this regex...
^([][{} \t#%$~A-Za-z0-9_:+\.-\\]+)\(([0-9]+)\) (:) (Warning|Error)(.*)$
Alas, when put into my tasks.json file with the (hopefully) correct escape slashes I don't receive any output in the Problems tab after running the task that will generate these errors in the terminal of VSCode.
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build simple regex",
"type": "shell",
"command": "iarbuild iar_ewb_build_timer.ewp -make Debug -log errors -parallel 8",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": {
"fileLocation": "absolute",
"pattern": {
"regexp": "^([][{} \\t#%$~A-Za-z0-9_:+\\.-\\\\]+)\\(([0-9]+)\\) (:) (Warning|Error)(.*)$",
"file": 1,
"line": 2,
"severity": 4,
"message": 5
}
}
},
]
}
Perhaps there is a problem with my Regex. Any help?
I was able to get this working for example error codes in my original post. Thank you for the replies!
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build simple regex",
"type": "shell",
"command": "iarbuild iar_ewb_build_timer.ewp -make Debug -log errors -parallel 8",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": {
"fileLocation": "absolute",
"pattern": {
"regexp": "^([#%$~A-Za-z0-9_:+-\\\\]+)[(]([0-9]+)[)] (:) (Warning|Error)(.*)$",
"file": 1,
"line": 2,
"severity": 4,
"message": 5
}
}
},
]
}
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.
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!