Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Cannot write problem matcher for VS Code custom task #247

Copy link
Copy link

Description

@AnthonyMastrean
Issue body actions

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\\.$"
        },
    }
}
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature RequestThis issue is requesting an enhancement or new featureThis issue is requesting an enhancement or new featureResolved-FixedThis issue has been fixedThis issue has been fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.