I would like to match problem output from a program with JSON output.
I have a file output.json. The format is roughly like this:
"warnings": [
{
"file_location": "abc.hpp",
"line": 73,
"msg": "Some warning message"
}
I would like to show these in the problems view.
How do I read problems from file (do I need to use cat/type?). All the examples seem to be for npm watch or similar.
Even with output.json my current tasks don't show this.
I've defined the following task:
{
"label": "show problems",
"type": "shell",
"command": "cat output.json",
"problemMatcher": {
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": [
{
"regexp": "^\\s*\"file_location\"\\s*:\\s*\"(.+)\",$",
"file": 1
},
{
"regexp": "^\\s*\"line\"\\s*:\\s*(\\d+),$",
"line": 1
},
{
"regexp": "^\\s*\"msg\"\\s*:\\s*(\\d+)$",
"message": 1
}
]
}
}
Related
I am using the checker plugin for vite. It prints errors to the VS Code terminal, like this (example):
ERROR(TypeScript) Property 'foobarasdasdasdas' does not exist on type 'HelloWorld'.
FILE /home/birger/SomeFile.tsx:194:23
192 |
193 | // TODO: fix
> 194 | return helloWorld.foobarasdasdasdas({
| ^^^^^^^^^^^^^^^^^
195 | someprop:args,
196 | asd: "",
197 | dsa: "",
However, the "problems" tab in VS Code is silent.
How can I make VS Code detect the errors that is printed to the terminal?
EDIT: I guess I need to configure the task to pick up the output?
{
"version": "2.0.0",
"tasks": [
{
"label": "client dev",
"type": "npm",
"script": "dev",
"problemMatcher": // WHAT DO I WRITE HERE?
}
]
}
(https://github.com/fi3ework/vite-plugin-checker/issues/95)
// .vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "client dev",
"type": "npm",
"script": "dev",
"problemMatcher": [
{
"owner": "typescript",
"source": "Typescript",
"fileLocation": "absolute",
"applyTo": "allDocuments",
"background": {
"activeOnStart": true
// "beginsPattern": "sd",
// "endsPattern": " > "
},
"pattern": [
{
"regexp": "(ERROR|WARNING)\\(TypeScript\\) (.*)",
"severity": 1,
"message": 2
},
{
"regexp": "^ FILE (.*):(\\d*):(\\d*)$",
"file": 1,
"line": 2,
"column": 3
}
]
}
]
}
]
}
Add below code to your config file of checker plugin
export default {
plugins: [checker({ typescript: true /** or an object config */ })],
}
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.
When writing an extension in vsCode, is it possible to create a configuration (a field in my settings.json) where I can store multiple values and configure an actively selected one?
Say I have an external dependency, which I need to reference in my tasks.json. Through the configuration contribution point of the extension i can provide the following property:
"myextension.dependencyDir": {
"scope": "resource",
"type": "string",
"description": "Path to the external dependency"
}
I can now reference this path in my tasks.json through ${config:myextension.dependencyDir}
However, lets say my dependency comes in various versions, which I would like to switch from the comfort of my settings(UI)
I know that by using an array I can store multiple versions of the dependency.
"myextension.dependencyDir": {
"scope": "resource",
"type": "array",
"items": {
"type": "string"
},
"description": "Path to the external dependency"
},
However, I cannot seem to reference single elements out of this array from my tasks.json.
By calling ${config:myextension.dependencyDir} now, i get the entire array.
I have tried to call
${config:myextension.dependencyDir[0]}
${config:myextension.dependencyDir(0)}
${config:myextension.dependencyDir:0}
... and many other variations
to query the first item in my array. Neigther of those attempts have worked.
${config:myextension.dependencyDir}[0] just appends '[0]' to the
last element.
I know that I can create custom objects and configure them in my settings.
"myextension.dependencyDir": {
"scope": "resource",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Name of your dependency version"
},
"value": {
"type": "string",
"description": "Path to the specific Dependency version"
}
}
},
"description": "Path to the external dependency"
},
However, just like before, I don't know how to access a single entry in my array, let alone adress the specific fields name and value.
Is what I am trying to do possible? Is it even the proper way of doing this? Does anyone have a solution to my problem or suggestions for a different approach?
Thanks in advance
BioZons
You can use the extension Command Variable v1.30.0
It has a command extension.commandvariable.config.expression. It allows to apply a JavaScript expression to the value of a configuration variable.
If the config variable is an array:
{
"version": "2.0.0",
"tasks": [
{
"label": "echo config var",
"type": "shell",
"command": "echo",
"args": [
"ConfigArray: ${input:configArray}",
],
"problemMatcher": []
}
],
"inputs": [
{
"id": "configArray",
"type": "command",
"command": "extension.commandvariable.config.expression",
"args": {
"configVariable": "myextension.dependencyDir",
"expression": "content[1]"
}
}
]
}
If the config variable is an array and you want to pick the array element:
{
"version": "2.0.0",
"tasks": [
{
"label": "echo config var",
"type": "shell",
"command": "echo",
"args": [
"ConfigArrayPick: ${input:configArrayPick}",
],
"problemMatcher": []
}
],
"inputs": [
{
"id": "configArrayPick",
"type": "command",
"command": "extension.commandvariable.config.expression",
"args": {
"configVariable": "myextension.dependencyDir",
"expression": "content[${pickStringRemember:serverNr}]",
"pickStringRemember": {
"serverNr": {
"description": "Which server to use?",
"options": [
["development", "0"],
["live", "1"]
]
}
}
}
}
]
}
If the config variable is an array of objects:
{
"version": "2.0.0",
"tasks": [
{
"label": "echo config var",
"type": "shell",
"command": "echo",
"args": [
"ConfigObject: ${input:configObject}",
],
"problemMatcher": []
}
],
"inputs": [
{
"id": "configObject",
"type": "command",
"command": "extension.commandvariable.config.expression",
"args": {
"configVariable": "myextension.dependencyDir",
"expression": "content[1].name"
}
}
]
}
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
}
}
},
]
}