JetBrains / resharper-unity

Unity support for both ReSharper and Rider
Apache License 2.0
1.21k stars 134 forks source link

Support Roslyn analyzer under packages, and analyzer scope #2200

Closed nowsprinting closed 2 years ago

nowsprinting commented 3 years ago

In Unity 2020.3.4f1, Fixed problem about Roslyn analyzer under packages. Roslyn analyzer under packages is working on Unity Editor.

But, the same result cannot be obtained with Rider. Because Rider Editor package v3.0.7 generates an incorrect .csproj file.

1. Incorrect analyzer path when local package

Does not work analyzer under the local package (e.g. LocalPackages/com.nowsprinting.analyzer-in-localpackage/WithAsmdef/AnalyzerInLocalPackageWithAsmdef.dll)

But, the following .csproj will be generated. Invalid path.

<Analyzer Include="Packages/com.nowsprinting.analyzer-in-localpackage/WithAsmdef/AnalyzerInLocalPackageWithAsmdef.dll" />

2. Incorrect analyzer path when package from Git URL (and maybe from UPM repository)

Does not work analyzer under the package from Git URL (e.g. https://github.com/nowsprinting/analyzer-in-package)

But, the following .csproj will be generated. Analyzer dll will be placed under the Library/PackageCache.

<Analyzer Include="Packages/com.nowsprinting.analyzer-in-package/WithAsmdef/AnalyzerInPackageWithAsmdef.dll" />

3. Not supported analyzer scope

On Unity Editor, Analyzers works by asmdef dependencies. This is clearly stated in the manual as follows.

Analyzers apply globally to any assembly in your project if they’re in the Assets folder, or in a subfolder that doesn’t contain an assembly definition file in any of their parent folders. If an analyzer is in a folder that contains an assembly definition, or a subfolder of such a folder, the analyzer only applies to the assembly generated from that assembly definition, and to any other assembly that references it.

But, generated .csproj filr will contain all analyzers regardless of their scope.

Refs

van800 commented 3 years ago

I think this is already fixed in https://github.com/van800/com.unity.ide.rider/pull/9, I would need to check.

nowsprinting commented 3 years ago

Thanks! I'll try to verify it with the 3.0.8 branch.

And I fixed the mistake sample repository URL in descriptions.

nowsprinting commented 3 years ago

I tried branch 3.0.8 in my sample project. https://github.com/nowsprinting/RoslynAnalyzerOnUnitySandbox/tree/rider_editor_308

But The problem I wrote above doesn't seem to be solved.

The Unity version of the above project is 2020.2.0f1, but the behavior of Rider Editor did not change even if I raised it to 2021.2.0f1.

van800 commented 3 years ago

Thank you for the complete repro-case, I have fix in https://github.com/van800/com.unity.ide.rider/pull/9. I have tried it with 2020.2.0f1 and 2021.2.0f1.

nowsprinting commented 3 years ago

Thanks! I was able to confirm that the path problems (cases 1 and 2) have been fixed!

However, the problem with analyzer scope (case 3) remains. You can check it with NoAsmdef.cs and NoReferences.cs in my sample code.

van800 commented 3 years ago

From what I see: Unity 2021.2.0f1 is showing 4 warnings for NoAsmdef.cs, 3 Errors and 5 Warnings in SpecifiedReferences.cs. Rider shows all those + 4 Warnings for NoReferences.cs. For each csproj Unity gives a set of analysers to the Rider package. So the behaviour would likely be consistent over Rider/VsCode/VS. I would try asking in our channel with Unity team.

image image

nowsprinting commented 3 years ago

Thanks! I got the expected result with a combination of Rider Editor package v3.0.8 and Unity 2021.2.0f1!

I would like to know for sure that this is due to a fix on the Unity side, and what the support for older versions will be. I'm waiting for an answer from the Unity team.