question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot write problem matcher for VS Code custom task

See original GitHub issue

I’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 or error
  • 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” for true 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:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rynowakcommented, Feb 29, 2020

Thanks for figuring this out @AnthonyMastrean

I’m successfully using this matcher as well on github actions.

image

name: Format

on:
  pull_request:
    branches:
      - master
      - release/*

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
      
    - name: Setup .NET Core SDK
      uses: actions/setup-dotnet@v1.0.0
      with:
        version: 3.1.100
     
    - name: dotnet tool restore
      run: dotnet tool restore

    - name: add dotnet format problem matcher
      run: echo "::add-matcher::build/dotnet-format-problem-matcher.json" 
      
    - name: dotnet format
      run: dotnet format -w <mysln>.sln --check --dry-run -v diag
{
    "problemMatcher": [
        {
            "owner": "dotnet-format",
            "severity": "warning",
            "pattern": [
                {
                    "regexp": "^\\s+(.*)\\((\\d+),(\\d+)\\):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "message": 4
                }
            ]
        }
    ]
}
1reaction
sharwellcommented, Oct 17, 2019

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Defining a ProblemMatcher in VSCode tasks -- schema ...
The problem matcher to be used if a global command is executed (e.g. no tasks are defined). A tasks.json file can either contain...
Read more >
Tasks in Visual Studio Code
VS Code can process the output from a task with a problem matcher. Problem matchers scan the task output text for known warning...
Read more >
VSCode Tasks Problem Matchers
Use problem matchers to review the output of tasks.
Read more >
VSCode problemMatcher problems
It looks like there's a bug in VSCode if the error message is the first line of the command's output. I added some...
Read more >
Task.json error on base:gcc - Technical Support
Hello, I have an Apalis iMX8 SOM with the Ixora board. I am using VS Code with the Torizon early access plugin on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found