Cannot write problem matcher for VS Code custom task
See original GitHub issueI’m trying to run dotnet format
in a VS Code custom task. But, there’s not enough detail in the output to write a decent problem matcher.
tasks.json
"problemMatcher": {
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^\\s*Formatted code file '(.*)'.$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
The output doesn’t provide enough of a file path for fileLocation
. It doesn’t provide line or column numbers. And it doesn’t provide a specific message.
$ dotnet format --check --dry-run
Formatting code files in workspace 'C:\Users\anthony\example\src\Example\Example.csproj'.
Formatting code files in project 'Example'.
Formatted code file 'Startup.cs'.
Format complete.
Considerations
The important points to consider when redesigning the output:
- The output should include workspace relative paths, no matter how I specify the
--workspace
- The output should include line and column number where the violation occurs
- The output should not include severity, since that’s not a relevant concept; the user can choose whichever is appropriate for their project:
warning
orerror
- The output should include not just the rule name, but whether the rule was required or not and how it was violated (more thought on this is probably required… maybe append “not required” for
false
rules and “required” fortrue
rules?)
Simple Problem Matcher
https://code.visualstudio.com/Docs/editor/tasks#_defining-a-problem-matcher
template
[workspace relative file path](line,col): [rule name]
output
$ dotnet format --check --dry-run
Formatting code files in workspace 'C:\Users\anthony\example\Example.sln'.
src\Example\Startup.cs(26,22): space between parentheses not required
src\Example\Controllers\ExampleController.cs(100,31): new line before open brace required
Format complete.
tasks.json (regex101)
{
"label": "format",
"command": "dotnet",
"type": "process",
"args": ["format", "--check", "--dry-run"],
"problemMatcher": {
"fileLocation": ["relative", "${workspaceFolder}"],
"severity": "warning",
"source": "dotnet format",
"pattern": {
"regexp": "^\\s+(.*)\\((\\d+),(\\d+)\\):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"message": 4,
}
}
}
Multiline Problem Matcher
https://code.visualstudio.com/Docs/editor/tasks#_defining-a-multiline-problem-matcher
It’s possible for a single file to have multiple violations and even for a single line in that file to have multiple violations. So, we need to output text that can be matched properly. I wonder if repeated lines and matches are simply added up in the problem windows. If so, then flat repeated output, not “stylish” nested output, might be best.
output
$ dotnet format --check --dry-run
Formatting code files in workspace 'C:\Users\anthony\example\Example.sln'.
src\Example\Startup.cs(26,22): space between parentheses not required
src\Example\Startup.cs(26,42): new line before open brace required
src\Example\Controllers\ExampleController.cs(100,31): new line before open brace required
Format complete.
tasks.json
No change necessary! Repeated lines worked! I tested using a text file containing the expected output and a fake cmd.exe
task.
{
"label": "format",
"command": "cmd",
"type": "process",
"args": ["/c", "type", "example.txt"],
"problemMatcher": {
"fileLocation": ["relative", "${workspaceFolder}"],
"severity": "warning",
"source": "dotnet format",
"pattern": {
"regexp": "^\\s+(.*)\\((\\d+),(\\d+)\\):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"message": 4,
}
}
}
Background/Watching Problem Matcher
https://code.visualstudio.com/Docs/editor/tasks#_background-watching-tasks
What about when we want to run formatting constantly in the background? Maybe via dotnet watch
? We need to implement the task slightly differently. Luckily, dotnet format already outputs a begin and end statement.
tasks.json
I tweaked the fake cmd.exe
task to output the contents of that file in a loop. You can modify the contents during the wait time. The problem matcher seems to have some trouble clearing out the last problem for a given file, but if you stack up multiple problems and remove them one-by-one, you can see the general behavior.
{
"label": "format",
"command": "cmd",
"type": "process",
"args": ["/c", "FOR /L %G IN (1,1,100) DO timeout /T 10 & type example.txt"],
"isBackground": true,
"problemMatcher": {
"fileLocation": ["relative", "${workspaceFolder}"],
"severity": "warning",
"source": "dotnet format",
"pattern": {
"regexp": "^\\s+(.*)\\((\\d+),(\\d+)\\):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"message": 4,
},
"background": {
"activeOnStart": true,
"beginsPattern": "^Formatting code files in workspace\\s+'(.*)'\\.$",
"endsPattern": "^Format complete\\.$"
},
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (6 by maintainers)
Top GitHub Comments
Thanks for figuring this out @AnthonyMastrean
I’m successfully using this matcher as well on github actions.
@AnthonyMastrean please consider sending a pull request to this project adding comments in code to any places that are sensitive to this, and/or refactoring the output sections to go through helper methods that ensure the output always has the expected form.