format
format copied to clipboard
More detailed report messages
Given the following code:
namespace ConsoleApp1
{
class Program {
static void Main(string[] args)
{
Console.WriteLine ("Hello World!");
}
}
}
Running dotnet-format --dry-run
gives the following output:
Formatting code files in workspace 'C:\dev\xt0rted\actions-playground\Application.sln'.
src\ConsoleApp1\Program.cs(5,18): Fix whitespace formatting.
src\ConsoleApp1\Program.cs(8,30): Fix whitespace formatting.
Formatted code file 'Program.cs'.
Format complete in 4450ms.
The two violations have the same message of Fix whitespace formatting
but there's no indication as to what the formatting issues are. The first error is csharp_new_line_before_open_brace
and the second is csharp_space_between_method_declaration_parameter_list_parentheses
.
At the very least the rule names should be included in the output, but more descriptive messages would be best. Knowing that the first issue is because there needs to be a new line before the brace, and the second issue is because there shouldn't be a space after the method declaration, is a lot more helpful then being told there's a generic whitespace issue.
Other linters such as ESLint and StyleLint produce a report in the stylish format which has the rule name a violation message. The compact format is most similar to what's being used now though.
Unfortunately, the architecture of the rules engine used for applying formatting rules makes this effectively impossible. The only way to determine the rule that impacts the scenario would be to iterate over each of the available formatting options, change it to a different value, and then re-run the formatter to see if the reported warning disappears.
That's unfortunate to hear. The reason I'm asking about this is because I built out a couple GitHub Actions workflows to run dotnet-format
and was hoping to be able to adjust the report output to include the rule names & messages since it would greatly improve the experience of it all.
I'd love to include a preview of the code fix with the annotations, similar to what you get in Visual Studio, but I don't even want to ask what's involved in making that happen 😄
I'd love to include a preview of the code fix with the annotations, similar to what you get in Visual Studio,
That part's relatively easy. You can either run the formatter manually to get the information, or you can indirectly invoke the code fix and then provide some sort of UI to present the changes. CodeFixTest<TVerifier>
is a good example of executing code fixes (likely more complicated than you need but shows pretty much everything).
I am in favor of doing something like this, but as @sharwell says, we would need to do a lot of work (most likely in roslyn) to make this happen
+1 This would be a great DX improvement