Support Roslyn analyzers in Unity projects

I found this entry in the 2020.2.0a17 release notes:

  • Scripting: Support Roslyn analyzers in Unity projects.

Can you provide more info on that?

2 Likes

I’m interested in this as well. The setting has been in for a few versions just unsure how to use it.

I use roslyn analyzers in all my projects and created the package back in 2018.3 GitHub - tertle/com.bovinelabs.analyzers: Package to add Roslyn Analyzers to Unity projects. to add support, but if it’s now natively supported all the better.

1 Like

Here you go: Unity - Manual: Roslyn analyzers and ruleset files

5 Likes

Anyone had any luck getting the .ruleset file to work?
I managed to load a bunch of analyzers no problem onto my projects but no matter what I do it won’t use the .ruleset file.

I’ve tried both an existing .ruleset file I had that works with my above library as well as testing the one in the documentation and nothing seems to work; whether its the Default or a custom asmdef version. It’s just not added to the csproj.

If I manually edit the .csproj and add
Assets\Default.ruleset
Works fine, as expected. But it’s never being added on project generation.

I couldn’t make the analyzer work. Using 2020.2.0b2 ,project directory is D:\Develop\DENE, keep getting this error over and over

DirectoryNotFoundException: Could not find a part of the path ‘D:\Develop\DENE\Temp\RoslynAnalysisRunner’.
System.IO.__Error.WinIOError (System.Int32 errorCode, System.String maybeFullPath) (at <9577ac7a62ef43179789031239ba8798>:0)
System.IO.FileSystemEnumerableIterator1[TSource].HandleError (System.Int32 hr, System.String path) (at <9577ac7a62ef43179789031239ba8798>:0) System.IO.FileSystemEnumerableIterator1[TSource].CommonInit () (at <9577ac7a62ef43179789031239ba8798>:0)
System.IO.FileSystemEnumerableIterator1[TSource]..ctor (System.String path, System.String originalUserPath, System.String searchPattern, System.IO.SearchOption searchOption, System.IO.SearchResultHandler1[TSource] resultHandler, System.Boolean checkHost) (at <9577ac7a62ef43179789031239ba8798>:0)
System.IO.FileSystemEnumerableFactory.CreateFileNameIterator (System.String path, System.String originalUserPath, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) (at <9577ac7a62ef43179789031239ba8798>:0)
System.IO.Directory.InternalGetFileDirectoryNames (System.String path, System.String userPathOriginal, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) (at <9577ac7a62ef43179789031239ba8798>:0)
System.IO.Directory.InternalGetFiles (System.String path, System.String searchPattern, System.IO.SearchOption searchOption) (at <9577ac7a62ef43179789031239ba8798>:0)
System.IO.Directory.GetFiles (System.String path) (at <9577ac7a62ef43179789031239ba8798>:0)
UnityEditor.Scripting.ScriptCompilation.RoslynAnalysisRunner+<>c__DisplayClass43_0.b__1 (System.Object _) (at <3d7c86b365634083bceadac5c7f36490>:0)
UnityEditor.Scripting.ScriptCompilation.CompilationTask.HandleOnCompilationTaskFinished () (at <3d7c86b365634083bceadac5c7f36490>:0)
UnityEditor.Scripting.ScriptCompilation.CompilationTask.Poll () (at <3d7c86b365634083bceadac5c7f36490>:0)
UnityEditor.Scripting.ScriptCompilation.RoslynAnalysisRunner.Poll () (at <3d7c86b365634083bceadac5c7f36490>:0)
UnityEditor.EditorApplication.Internal_CallUpdateFunctions () (at <3d7c86b365634083bceadac5c7f36490>:0)

2 Likes

Also another question, how will the codefixes are going to work in the editor? In Visual Studio, we press Alt+Enter to make it work.

Hi tertle! Sorry for the late reply.

After adding ruleset files to a Unity project that already contains Roslyn analyzers, will you right-click on a script inside of an assembly to which rulesets should apply, select “Reimport”, and let me know whether that works?

My apologies for the unclear documentation. I will add more clarifying comments and ask the Documentation Team to update it.

1 Like

Hi sapsari! Thank you for your question.

Right now, code fixes are supported in JetBrains Rider. Support in Visual Studio will be released soon – most probably in the next Visual Studio Code Editor package.

2 Likes

Do you see this error every time you move a new DLL into your project and then add the “RoslynAnalyzer” label to it?

I just followed the instructions in the documentation on an empty project. I only tried ErrorProne.NET dlls. And yes,I started to recieve this error after labeling them.

I tried it two times, both failed with the same message.

Which OS are you using?

Windows 10

I was already using ErrorProne with a local OmniSharp config. I’m so glad that Unity actually added Roslyn Analyzer support so that I can finally carry the analyzer around with the project.

Edit: I got excited too soon. I got the same repeating error as @sapsari . My OS is macOS 10.15.6.

@sapsari and @Neonlyte , thank you for the feedback. I will investigate this.

Is there any way for us to include analyzers in a project as .cs files with an assembly definition? Or are we currently only able to include precompiled assemblies? The documentation doesn’t really touch on this currently.

2 Likes

@MiaoYuGoh Any updates? I have avoided trying things out in the subsequent beta versions.

@Neonlyte Apologies for my late reply. Initially I had a hard time reproducing the problem, but after many tries I finally managed to reproduce it consistently. I have made some changes to the implementation, and with those changes I can no longer reproduce the issue, so I believe I have made the correct fix. I will add some testing around the changes and create a pull request for my colleague to review today.

1 Like

Cool. Please let us know when this fix will land.

@MiaoYuGoh
BTW, do you guys have any interest in pushing Microsoft implementing DiagnosticSuppressor for OmniSharp, so that Unity Roslyn Analyzers could properly suppress warnings related to Unity quirks? It should be fair to assume that people would pick up that particular analyzer DLL when 2020.2 is made public with this feature in one of the highlights.

Github Issue: New analyzer API (DiagnosticSuppressor) is not supported ¡ Issue #1711 ¡ OmniSharp/omnisharp-roslyn ¡ GitHub

Edit: also tagging @John_MSFT

2 Likes

So FYI I was running into the same issue here (Win 10). Restarting the project made it go away but it would randomly appear again sometimes. I also found that just putting the three DLLs with the “RoslynAnalyzer” tag on them didn’t work with the RethrowError script from the docs. Instead I had to put an .asmdef file in the same directory as the analyzer DLLs and the script for them to start working. Haven’t tried with the ruleset yet.

I expect this is because an “empy” project still has a lot of .asmdef files in it via Packages, and somehow those .asmdef files are breaking the compilation chain in Assets/ required for analyzers to work.