format icon indicating copy to clipboard operation
format copied to clipboard

RS0016 not recognized using --diagnostics in v6

Open tobias-tengler opened this issue 3 years ago • 12 comments

In the latest v5 I was using the following to execute the code fix of RS0016 through the CLI:

dotnet format SolutionFile.sln --fix-analyzers warn --diagnostics RS0016

Now I'm using the builtin version of .NET 6 and adjusted the syntax to the following:

dotnet format SolutionFile.sln analyzers --diagnostics RS0016

However now I'm receiving the following error:

Unrecognized command or argument 'RS0016'

The Code-Fix doesn't seem to execute at all, even if I use dotnet format analyzers without the --diagnostics filter:

dotnet format SolutionFile.sln analyzers

I also tried to play around with the --severity without success. Am I doing something wrong? Is this no longer possible?

tobias-tengler avatar Nov 01 '21 18:11 tobias-tengler

After some investigation we believe this issue is likely in the dotnet/SDK command line option definitions for the format command. We can either update the SDK definitions to match or remove them entirely. Removing them entirely will disable tab completion for environments that support it.

JoeRobich avatar Nov 03 '21 21:11 JoeRobich

I think matching up the SDK definitions would be great. This also means that there likely won't be a fix with the initial release of .NET 6, right? In the meantime is there anyway for me to execute the original command of v5, i.e. the standalone tool, if our environment is using .NET 6?

tobias-tengler avatar Nov 04 '21 10:11 tobias-tengler

Actually I spoke too soon. The issue is with how the tool is parsing not necessarily the SDK (although it may have the same problem).

Without SlnOrProject argument specified.

~/Source/format [release/6.x ≡ +1 ~6 -0 !]> dotnet tool run dotnet-format [parse] analyzers --diagnostics RS0016
[ dotnet-format [ analyzers <RS0016> *[ --diagnostics <> ] *[ --include <> ] *[ --exclude <> ] ] ]

With SlnOrProject argument specified.

~/Source/format [release/6.x ≡ +1 ~6 -0 !]> dotnet tool run dotnet-format [parse] analyzers ./format.sln --diagnostics RS0016
[ dotnet-format [ analyzers <./format.sln> *[ --diagnostics <> ] *[ --include <> ] *[ --exclude <> ] ] ]   ???--> RS0016

@jonsequitur Not sure what is wrong with our option definition but the --diagnostics option is not capturing arguments. Do you have any advice?

JoeRobich avatar Nov 04 '21 20:11 JoeRobich

@tobias-tengler as a work around you can use --diagnostics=RS0016

JoeRobich avatar Nov 04 '21 20:11 JoeRobich

@JoeRobich thank you for the workaround. The command is now executing, but it still doesn't have the desired effect: The specific code fix is not invoked (or atleast nothing is written to the PublicAPI.Unshipped.txt file). I've tried with both the SlnOrProject argument and without - same result. I tested the following variations:

dotnet format analyzers --diagnostics=RS0016
dotnet format analyzers --diagnostics=RS0016 --severity warn
dotnet format --diagnostics=RS0016

Can you think of anything that could cause these issues? I found some unit tests regarding fixes for RS0016, so I assume it must be working and my setup is just janked: https://github.com/dotnet/format/blob/017b033388019557cd732d3daf9436ecd29559f7/tests/CodeFormatterTests.cs#L508

tobias-tengler avatar Nov 08 '21 16:11 tobias-tengler

@tobias-tengler If you have referenced the analyzer package from your project files and created PublicAPI.Shipped.txt and PublicAPI.Unshipped.txt files in the same folder as your project, then it should be working. Can you link me to your repo or share a cut down repro with me?

  <PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.3">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
  </PackageReference>

I guess I should also add that you need to have configured an adequate severity for RS0016 in your .editorconfig file.

[*.cs]
dotnet_diagnostic.RS0016.severity = warning

JoeRobich avatar Nov 08 '21 16:11 JoeRobich

Your edit about the warning level was the issue. Thanks a lot @JoeRobich for your fast help :) This must've changed recently since this wasn't needed before. My issue is resolved, but I guess you might want to keep this issue open, since the --diagnostics needs to be fixed. If not, feel free to close!

tobias-tengler avatar Nov 08 '21 17:11 tobias-tengler

This must've changed recently since this wasn't needed before.

It was a breaking change in 6.x. After merging https://github.com/dotnet/format/pull/1291 we only apply code fixes for diagnostics explicitly configured in the .editorconfig, since analyzer packages can specify their own defaults, this ensures that only the analyzer rules that your team has discussed and that you really care about are being enforced.

JoeRobich avatar Nov 08 '21 18:11 JoeRobich

I'm also running into this issue with .net sdk 6.0.300 and the work around isn't working for me. Things I've tried so far:

  1. dotnet format analyzers MySolution.sln --diagnostics="SA1028 SA1107" produces: Unrecognized command or argument 'SA1028 SA1107'
  2. dotnet format analyzers --diagnostics="SA1028 SA1107" produces: Unhandled exception: System.IO.FileNotFoundException: The file 'SA1028 SA1107' does not appear to be a valid project or solution file.
  3. dotnet format analyzers --diagnostics=SA1028 produces Unhandled exception: System.IO.FileNotFoundException: The file 'SA1028' does not appear to be a valid project or solution file.
  4. dotnet format analyzers --diagnostics SA1028 produces Unhandled exception: System.IO.FileNotFoundException: The file 'SA1028' does not appear to be a valid project or solution file.

mukunku avatar Aug 09 '22 19:08 mukunku

Same thing for me. Using SDK 6.0.301. Before, I used to only run dotnet format --severity warn and the PublicAPI.* files got updated. Now nothing happens. Would be good to get some help on this.

joaopgrassi avatar Sep 26 '22 15:09 joaopgrassi

Has anyone been able to get the PublicAPI text files to be auto-updated in 6.x?

RS0016/17, etc. are set as errors and generate the expected compiler errors after new methods are added, but running dotnet format does not update the PublicAPI.Unshipped.txt files. The only warnings in the diagnostic output are "Duplicate source file...PublicAPI.Shipped/Unshipped.txt..." in the solution projects. The analyzers run but never update any files.

Using format version 6.4.352107 (but was seeing same behavior in 6.2)

dotnet format analyzers DemoServer.sln --diagnostics=RS0016 --severity error --no-restore --verbosity diag

The dotnet runtime version is '6.0.19'. The dotnet CLI version is '6.0.411'. ... Duplicate source file warnings and standard editor config source messages... Fixing diagnostics... Complete in 5ms. Analysis complete in 3391ms. Complete in 3824ms. Formatted 0 of 221 files.

DigiTorus86 avatar Jul 10 '23 15:07 DigiTorus86

Using .NET SDK 7.0 and the added 'dotnet_diagnostic.RS0016.severity = warning' to .editorconfig, we were able to run the code fix for RS0016 on the entire solution.

dotnet format analyzers --diagnostics=RS0016

remcoros avatar Jul 12 '23 09:07 remcoros