Skip to content

Analyzers not working with dotnet format #387

@pierre-fove

Description

@pierre-fove

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions