Skip to content

Analyzers not working with dotnet format #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pierre-fove opened this issue Apr 15, 2025 · 4 comments · May be fixed by dotnet/sdk#48512
Closed

Analyzers not working with dotnet format #387

pierre-fove opened this issue Apr 15, 2025 · 4 comments · May be fixed by dotnet/sdk#48512

Comments

@pierre-fove
Copy link

pierre-fove commented Apr 15, 2025

Bug description

When using the dotnet format unity-solution.sln to ensure that our Unity project respects the .editorconfig linting rules, the Unity analyzer dll isn't properly loaded/used, resulting in a lot of IDE0051 (unused private member) and IDE0044 (field can be make readonly).

  • Version of analyzers assembly: VS2022 Unity one ("C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\Microsoft\Visual Studio Tools for Unity\Analyzers\Microsoft.Unity.Analyzers.dll")
  • Analyzer rule: All

To Reproduce

  • Create a new Unity project (tested with 2019.4.40f1, but experienced same issue with other versions)
  • Create a new cs behavior script via Unity (with a Start & Update functions)
  • Create a .editorconfig file with the below content
root = true

[*.cs]
dotnet_diagnostic.IDE0051.severity = warning # Remove unused private member
  • Run dotnet format project-name.sln --severity warn --verify-no-changes --verbosity diagnostic

Expected behavior

Same as what is displayed in Visual Studio (eg. no linting error)
Image

Actual Result

IDE0051 errors

$ dotnet format TestLinting.sln --severity warn --verify-no-changes --verbosity diagnostic
  The dotnet runtime version is '9.0.4'.
  Using MSBuild.exe located in 'C:\Program Files\dotnet\sdk\9.0.203\'.
  Formatting code files in workspace 'C:\Dev\test-projects\TestLinting2\TestLinting\TestLinting.sln'.
  Loading workspace.
    Determining projects to restore...
  Nothing to do. None of the projects specified contain packages to restore.
  Project Assembly-CSharp is using configuration from 'C:\Dev\test-projects\TestLinting2\TestLinting\.editorconfig'.
  Complete in 2901ms.
  Determining formattable files.
  Complete in 76ms.
  Running formatters.
  Running Code Style analysis.
  Determining diagnostics...
  Running 4 analyzers on Assembly-CSharp.
C:\Dev\test-projects\TestLinting2\TestLinting\Assets\TestLintingScript.cs(6,10): warning IDE0051: Private member 'TestLintingScript.Start' is unused [C:\Dev\test-projects\TestLinting2\TestLinting\Assembly-CSharp.csproj]
C:\Dev\test-projects\TestLinting2\TestLinting\Assets\TestLintingScript.cs(11,10): warning IDE0051: Private member 'TestLintingScript.Update' is unused [C:\Dev\test-projects\TestLinting2\TestLinting\Assembly-CSharp.csproj]
  Complete in 522ms.
  Analysis complete in 523ms.
  Running Analyzer Reference analysis.
  Determining diagnostics...
  Running 3 analyzers on Assembly-CSharp.
  Complete in 21ms.
  Analysis complete in 21ms.
  Complete in 813ms.
  Formatted code file 'C:\Dev\test-projects\TestLinting2\TestLinting\Assets\TestLintingScript.cs'.
  Formatted 1 of 1 files.
  Format complete in 3790ms.

Additional info

This issue can be easily reproduced in Visual Studio by removing the following lines from Assembly-CSharp.csproj

  <ItemGroup>
    <Analyzer Include="C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\Microsoft\Visual Studio Tools for Unity\Analyzers\Microsoft.Unity.Analyzers.dll" />
  </ItemGroup>

Image

@sailro
Copy link
Member

sailro commented Apr 15, 2025

This is not an issue on our side, but rather on the dotnet format tool.

We use diagnostic suppressors to handle specific IDE0051 cases with Unity. And unfortunately, dotnet format is not taking them into account.

I spotted a similar issue with NUnit suppressors:

dotnet/sdk#44867

@sailro
Copy link
Member

sailro commented Apr 15, 2025

Here is the initial tracking ticket:

dotnet/format#1998

(but I believe newer devs are in the sdk repository now)

Closing this one.

@sailro
Copy link
Member

sailro commented Apr 16, 2025

@pierre-fove I made a PR that fixes this issue: dotnet/sdk#48512

@pierre-fove
Copy link
Author

Oh great! much thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants