VSCode Powershell problemMatcher - powershell

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!

Related

VsCode : How to generate custom task input options from a command?

Let's say we have the following custom task :
{
"version": "2.0.0",
"tasks": [
{
"label": "do smthg",
"type": "shell",
"command": "echo \"selected option: ${input:option_name}\"",
"problemMatcher": [],
"presentation": {
"panel": "dedicated",
"focus": true
}
}
],
"inputs": [
{
"type": "pickString",
"id": "option_name",
"description": "select an option :",
"options": [
// want possible options to be the output of a command
],
"default": ""
}
]
}
But I want the available options to be the result of a command,
like ls, or a cat smthg.txt | grep -oP '\"value\:\"\K\w*',
how can I do that ? Is it only possible ?
You can use the extension Command Variable v1.34.0
Use the replacement for pickString named extension.commandvariable.pickStringRemember.
This command can read options from a file, you determine the format with a regexp like the problem matcher of tasks.
An example:
"inputs": [
{
"type": "command",
"id": "option_name",
"command": "extension.commandvariable.pickStringRemember",
"args": {
"description": "select an option :",
"options": [
["always 1", "5000"],
["always 2", "5100"]
],
"default": "",
"fileName": "${workspaceFolder}/foo-bar.txt",
"pattern": {
"regexp": "^\\s*(?!#)([^=]+?)\\s*=\\s*(?:(\\{.+\\})|(.+))$",
"label": "$1",
"json": "$2",
"value": "$3"
}
}
}
]
If you don't have label - value lines you can simplify the pattern to
"pattern": {
"regexp": "^\\s*(?!#)(.+)$"
}
If you don't have static options (always 1/2 in example) you can remove the options property from args.
With an additional task you create the file with a command or a shell script if grep-ping and file redirection might be a problem (pass the output file name, using variables, as argument to the script).
You can construct a sequence of tasks, a compound task, see VSC doc.

How to define a squiggle range for the problem matcher in tasks.json (VS Code)?

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\\."
}
}
}
]
}```

C++ custom problem matchers in VS Code not highlighting file paths

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?

vscode problemMatcher doesn't not recognize Tasking compiler warnings

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+)\\] (.*)$",

VSCode problemmatcher regex not matching my error output

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
}
}
},
]
}