Search Unity

Help Wanted Support Roslyn analyzers in Unity projects

Discussion in '2020.2 Beta' started by Peter77, Jul 8, 2020.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,599
    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?
     
    phobos2077 and SugoiDev like this.
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    2,873
    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 https://github.com/tertle/com.bovinelabs.analyzers to add support, but if it's now natively supported all the better.
     
  3. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    2,491
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    2,873
    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
    <CodeAnalysisRuleSet>Assets\Default.ruleset</CodeAnalysisRuleSet>
    Works fine, as expected. But it's never being added on project generation.
     
    Last edited: Aug 4, 2020
  5. sapsari

    sapsari

    Joined:
    May 30, 2013
    Posts:
    8
    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.FileSystemEnumerableIterator`1[TSource].HandleError (System.Int32 hr, System.String path) (at <9577ac7a62ef43179789031239ba8798>:0)
    System.IO.FileSystemEnumerableIterator`1[TSource].CommonInit () (at <9577ac7a62ef43179789031239ba8798>:0)
    System.IO.FileSystemEnumerableIterator`1[TSource]..ctor (System.String path, System.String originalUserPath, System.String searchPattern, System.IO.SearchOption searchOption, System.IO.SearchResultHandler`1[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.<CreateCompilationTask>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)
     
    Last edited: Sep 15, 2020
    Adagio-81 and SolidAlloy like this.
  6. sapsari

    sapsari

    Joined:
    May 30, 2013
    Posts:
    8
    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.
     
  7. MiaoYuGoh

    MiaoYuGoh

    Unity Technologies

    Joined:
    Nov 12, 2019
    Posts:
    8
    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.
     
    Last edited: Sep 17, 2020
    LeonhardP likes this.
  8. MiaoYuGoh

    MiaoYuGoh

    Unity Technologies

    Joined:
    Nov 12, 2019
    Posts:
    8
    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.
     
    Last edited: Sep 17, 2020
  9. MiaoYuGoh

    MiaoYuGoh

    Unity Technologies

    Joined:
    Nov 12, 2019
    Posts:
    8
    Do you see this error every time you move a new DLL into your project and then add the "RoslynAnalyzer" label to it?
     
  10. sapsari

    sapsari

    Joined:
    May 30, 2013
    Posts:
    8
    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.
     
  11. MiaoYuGoh

    MiaoYuGoh

    Unity Technologies

    Joined:
    Nov 12, 2019
    Posts:
    8
    Which OS are you using?
     
  12. sapsari

    sapsari

    Joined:
    May 30, 2013
    Posts:
    8
    Windows 10
     
  13. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    338
    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.
     
    Last edited: Sep 20, 2020
  14. MiaoYuGoh

    MiaoYuGoh

    Unity Technologies

    Joined:
    Nov 12, 2019
    Posts:
    8
  15. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    143
    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.
     
    bdovaz likes this.
  16. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    338
    @MiaoYuGoh Any updates? I have avoided trying things out in the subsequent beta versions.
     
  17. MiaoYuGoh

    MiaoYuGoh

    Unity Technologies

    Joined:
    Nov 12, 2019
    Posts:
    8
    @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.
     
    LeonhardP likes this.
  18. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    338
    Cool. Please let us know when this fix will land.
     
  19. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    338
    @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: https://github.com/OmniSharp/omnisharp-roslyn/issues/1711

    Edit: also tagging @John_MSFT
     
    Last edited: Oct 22, 2020
    goncalo-vasconcelos likes this.
  20. massivebacon

    massivebacon

    Joined:
    Apr 24, 2014
    Posts:
    27
    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.
     
  21. AlphaDreams

    AlphaDreams

    Joined:
    Jan 21, 2015
    Posts:
    26
    Same error here as @sapsari and @Neonlyte (missing the folder "RoslynAnalysisRunner" in the Temp folder). Even after restarting the project like @massivebacon suggested, still got the error.

    After some tests, it clearly is triggered when the .dll files are tagged with "RoslynAnalyzer". I'm on an empty project, and I also strictly followed the instructions of the manual page. I'm on Windows 10, Unity 2020.2.0b11.
     
  22. septN

    septN

    Joined:
    Oct 29, 2015
    Posts:
    26
    The bug is still exist. Surely restarting Unity will make it gone, but that's not a solution, just a temporary workaround
     
  23. mechaniqe

    mechaniqe

    Joined:
    Jan 25, 2017
    Posts:
    18
    I too tried to get it to work following the Documentation, but am hit with same errors as previous devs above (missing the folder "RoslynAnalysisRunner" in the Temp folder). Restarting doesn't help anyhow, the moment scripts are recompiled I start getting same errors again.

    Edit: After deleting everything and redoing it all using Rider (vs Visual Studio), it worked. But doesn't sound like a valid solution, still feels like errors may come up again any moment.
     
  24. danm36

    danm36

    Joined:
    May 18, 2016
    Posts:
    10
    I've found out that if you create a folder in [YourProject]/Temp called 'RoslynAnalysisRunner', Unity will immediately detect it, run the analysis then delete it, stopping the error. I've worked for another thirty minutes now and analysis continues to run without issue. Weird. I'm running 2020.2.0f1 with Visual Studio. My Visual Studio seems to still be doing analysis for every asmdef project, rather than just the one I've put the analyzer dlls under, but I can work with this.
     
  25. konstantin890

    konstantin890

    Joined:
    Jul 16, 2016
    Posts:
    7
    @MiaoYuGoh I'm also facing this issue. Even after I restart the editor, the error sometimes appears on recompilation.
     
  26. SolidAlloy

    SolidAlloy

    Joined:
    Oct 21, 2019
    Posts:
    41
    I would like to add that the error appears even if there are no RoslynAnalyzer-labeled assemblies in the project. I'm using Roslyn Analyzers through Directory.Build.props until the error is fixed. It is so annoying I created a small workaround so that I don't have to create the directory manually: https://gist.github.com/SolidAlloy/68b02da3c774c6691da7dab2eba190cc

    I updated to 2020.2.1f1 today, and the error is still there. Please prioritize the issue because it disrupts the workflow of users who don't even use the feature.
     
    Last edited: Dec 30, 2020
    grofie and meikellp like this.
  27. matteo_unity996

    matteo_unity996

    Joined:
    Dec 30, 2020
    Posts:
    1
    I'm having the same issue with a super simple, brand new Unity project!
    Every time I try to run my app I start getting thousand of errors, only way to get rid of them is to manually create the `RoslynAnalysisRunner` folder. Extremely disuptive!
    Please Unity staff do something!

    EDIT:

    PS
    Thanks for the temp fix SolidAlloy !
     
    SolidAlloy likes this.
  28. MiaoYuGoh

    MiaoYuGoh

    Unity Technologies

    Joined:
    Nov 12, 2019
    Posts:
    8
    Thank you all for your patience, and many apologies for the late reply. The fix will become available in 2021.1.

    @SolidAlloy Thank you for providing a temporary fix, and for mentioning that the error appears even in the absence of Roslyn Analyzer assemblies -- this is definitely new information. Currently the compilation pipeline is undergoing major refactoring, and we will keep an eye out to see whether the same error shows up again.
     
    SolidAlloy likes this.
  29. lokihornsby

    lokihornsby

    Joined:
    Jun 24, 2020
    Posts:
    15
    Haya, just letting you know (incase it helps) that this occured to me after moving multiple folders from a folder named "Main Assets" to the base "Assets" folder

    Unity Version: 2020.2.1f1
    OS: Windows 10
     
  30. Adagio-81

    Adagio-81

    Joined:
    Mar 28, 2015
    Posts:
    14
    I also just started seeing this error about missing folder today after moving the package SimpleInput from it's default folder to a different folder as it was impossible to use in the default folder
    I never added any Roslyn analyzer to my project. I have no idea what it is and why it's there. But it keeps deleting the missing folder and then complains that the folder is not there
     
  31. usewits

    usewits

    Joined:
    Aug 4, 2020
    Posts:
    6
    Here is a version of your script that works on Linux and Mac (it only worked on Windows because slashes in paths are "\" on Windows, while they are "/" on any Unix based system).
    https://gist.github.com/melonmouse/ef4c892ef7d5dd36b77360c622eb7b57

    FYI on 2020.2.2f1 the issue persists. In addition, newly added DLLs don't run for me, regardless of the "RoslynAnalyzer" tag, rebuilds of my scripts or the DLL location. However, if I move non-functioning DLLs from one directory to another and back to their original location, they start working on the next rebuild of my scripts.
     
    Last edited: Feb 2, 2021
  32. SolidAlloy

    SolidAlloy

    Joined:
    Oct 21, 2019
    Posts:
    41
    @usewits Thanks, I updated the gist.
     
    usewits likes this.
  33. usewits

    usewits

    Joined:
    Aug 4, 2020
    Posts:
    6
    To add to this; whenever I restart Unity, none of the Roslyn analyzers run when my scripts get built. As before, re-importing the DLLs doesn't help, but moving the DLLs back and forth once to any (temporary) directory fixes it for the rest of the session.

    P.S. many thanks to the Unity people who implemented Roslyn analyzer support - its a great addition!
     
  34. usewits

    usewits

    Joined:
    Aug 4, 2020
    Posts:
    6
  35. dock

    dock

    Joined:
    Jan 2, 2008
    Posts:
    426
    I just grabbed Unity 2020 LTS under the assumption that it was stable.
    Immediately I get hit with 100000 errors from Roslyn about part of a path being missing without any info about which path it is talking about.

    How can I disable Rosyln? I don't need this, especially with such aggressive and useless error messages.
     
  36. SolidAlloy

    SolidAlloy

    Joined:
    Oct 21, 2019
    Posts:
    41
    I noticed the "Enable Roslyn Analyzers" checkbox in Project Settings/Player, in 2020.2.4. It was enabled by default for me. Does disabling it turn off RoslynAnalysisRunner, and as a result, Temp folder errors?
     
  37. dariuslol

    dariuslol

    Joined:
    May 7, 2013
    Posts:
    1
    Can confirm that after turning off 'Enable Roslyn Analyzers' and 'Use Roslyn Reference Assemblies' (for good measure), Temp folder errors stopped appearing. Of course, that also disables code analysis.

    Currently using 2020.3.0f1 LTS.

    Users of 2021.1, is the fix mentioned by @MiaoYuGoh working?
     
    SolidAlloy likes this.
  38. user012345

    user012345

    Joined:
    Nov 10, 2020
    Posts:
    1
    Hello. Does anybody know, is it possible to install Roslyn code-analyzer into Unity project & make it work (for Unity below version 2020)? I've tried - when Unity regenerates .csproj file, it erases lines about added analyzers & analyzers stop working =( //more info here
    #UPD: I've found a solution
     
    Last edited: Apr 11, 2021
  39. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    854
    @MiaoYuGoh is it possible to create a Default.ruleset without having custom roslyn analyzers?

    I want for certain assemblies that are not mine (third party assets) to remove warnings but without putting it in the folder structure of that asset. Both with the csc.rsp and *.ruleset files they have to be next to the *.asmdef.

    In order to be in Assets folder it must be Default.ruleset, Assembly-CSharp.ruleset and two or three other types only. It cannot be any other assemblies *.ruleset. I don't understand why I can't override this behaviour from my project for other different Assembly.

    My first problem I have is that it does not include the Default.ruleset file inside the *.csproj.

    The second problem is that I would not know how to add the rules since I do not have a custom analyzer, I do not know which is the default "AnalyzerId" of Roslyn because I understand that the warnings that appear in the console are rules that could be created and warnings be excluded.

    https://docs.unity3d.com/2020.2/Documentation/Manual/roslyn-analyzers.html
     
    Last edited: Apr 20, 2021
  40. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    854
    What about 2020.3.x LTS? I still see this behaviour on 2020.3.0.
     
  41. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    854
    @MiaoYuGoh I also see that despite of adding analyzers it doesn't get included in *.csproj files. I see no <Analyzer> tag regarding the Analyzers I added.

    The same goes with *.ruleset files.
     
  42. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    72
    Hi bdovaz :)

    What IDE package are you using? Recently I fixed that Analyzers was not added to the generated csproj's.
     
  43. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    72
    These settings depend on the Analyzer you use, you can read a bit here:
    https://github.com/dotnet/roslyn/blob/main/docs/compilers/Rule Set Format.md#rules

    But in general its based on the name of the Assembly that is your custom Analyzer
     
  44. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    854
    @HaraldNielsen Visual Studio package.

    I use sonar to analyze code (with msbuild) but it needs to have analyzers and rulesets reference in csproj to be as close as possible to unity compilation output console result.
     
    Last edited: Apr 27, 2021
  45. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    854
    @HaraldNielsen what about this user's problem? I'm experiencing the same:

    https://forum.unity.com/threads/rsp-files-settings-not-propagated-to-csproj.1051115/

    What I thought is that all Unity settings are propagated in *.csproj files:

    - Flags from csc.rsp files.
    - Rulesets
    - Analyzers

    In our organization we run static code analysis with Sonarqube for which it is necessary to build the solution with dotnet/msbuild "outside Unity", that is, without Unity compiling the code.

    Right now the results are different because the *.csproj files do not include the elements I mentioned above.
     
  46. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    72
    @bdovaz - We notified MS about those flags not being set in the csproj generated using their package and they will look into it :)

    What Visual Studio package do you have in the project for checking up on the Analyzer not being added?
     
    bdovaz likes this.
  47. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    854
    @HaraldNielsen I attach the project so that you can reproduce it as is.
     

    Attached Files:

    HaraldNielsen likes this.
unityunity