Search Unity

Exclude Unity packages from Roslyn Analyzers

Discussion in 'Testing & Automation' started by rmiha, Sep 1, 2021.

  1. rmiha

    rmiha

    Joined:
    Sep 29, 2016
    Posts:
    10
    I just added some Roslyn analyzers into my Unity project like described here:
    https://docs.unity3d.com/2020.2/Documentation/Manual/roslyn-analyzers.html

    Everything works nicely, however I cannot find a way to exclude Unity packages from the analysis. What happens is that when compiling the project, I see bunch of analysis errors and warnings in the console for the files which are actually part of Unity packages. Is there a way to disable the analysis on those?

    A workaround that I currently found is that I put my analyzer dlls into the Folder containing the asmdef file instead of putting them into the Assets folder directly. But I don't like that solution since I have multiple folders containing the asmdef files like this:

    Assets/Code/MyNamespace1/MyAsmDef1.asmdef
    Assets/Code/MyNamespace2/MyAsmDef2.asmdef
    Assets/Code/MyNamespace3/MyAsmDef3.asmdef

    and so on.

    What I basically want is to exclude the code files which I didn't write from the analysis.

    Any help is highly appreciated!
     
  2. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    A way to control the asmdef's your analyzer runs on, is to have a asmdef(fx. analyzer.asmdef) with an analyzer next to it, and reference the asmdef. Then only the Asmdef's that references the analyzer.asmdef with the analyzer will include it.
     
    liortal likes this.
  3. rmiha

    rmiha

    Joined:
    Sep 29, 2016
    Posts:
    10
    @HaraldNielsen thanks for your reply. I think I understand. But what if the analyzer is already compiled - let's say that I have analyzer dll like ErrorProne.Net.CoreAnalyzers.dll ?
     
    Last edited: Sep 16, 2021
  4. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    For that you place `ErrorProne.Net.CoreAnalyzers.dll` next to a asmdef, and then all that reference that asmdef will run with ErrorProne.Net.CoreAnalyzers

    Should be described under Scope in https://docs.unity3d.com/Manual/roslyn-analyzers.html
    There are currently work getting done to get that page even better :)
     
  5. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    331
    But imaging we have our N asmdef, and we want to apply the analizers to all those N assemblies. Then we'd have to copy N times de analyzers and place next to the asmdef, isn't it?

    Isn't there a way to exclude folders? Such as package folder and 3rd party folder?

    Thank you
     
  6. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    Luckily not, that would be super bad.
    https://docs.unity3d.com/2022.1/Documentation/Manual/roslyn-analyzers.html

    Read the "Analyzer scope" section, you just have to only reference the asmdef that is next to the analyzer, if you only want to run on part of the assemblies (The ones that reference the asmdef directly)
     
  7. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    331
    Correct me if I am wrong, but this approach shouldn't work for already compiled analyzers, should it?

    At least it doesn't in my tests

    Regards
     
    house-ms likes this.
  8. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
    Hi @HaraldNielsen , I am trying to do what you have described, but I had no luck.
    I have that structure:
    Code (CSharp):
    1.  
    2. - Assets/RoslynAnalyzers/Analyzers.asmdef
    3. - Assets/RoslynAnalyzers/Microsoft.Unity.Analyzers.dll
    4.  
    5. - Assets/Scripts/FeatureA/FeatureA.Runtime.asmdef
    6. - Assets/Scripts/FeatureA/MyFeatureA.cs
    7.  
    8. - Assets/Scripts/FeatureB/FeatureB.Runtime.asmdef
    9. - Assets/Scripts/FeatureB/MyFeatureB.cs
    10.  
    I tried adding `FeatureA.Runtime.asmdef` to `Analyzers.asmdef`
    and adding `Analyzers.asmdef` to `FeatureA.Runtime.asmdef`

    But none of them work.
     
    Last edited: Oct 21, 2022
  9. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    @sandolkakos sorry, I totally missed these messages!
    Your FeatureA.Runtime.asmdef needs to reference Analyzers.asmdef - Do that work?

    @Maeslezo did you get it figured out?
     
  10. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
    No problem @HaraldNielsen, thanks for getting back to us.
    I've tried that and it does not work :(

    In order to make it easy to test, I prepared that GitHub repository with a small Unity project containing:
    • `Assets/RoslynAnalyzers/Microsoft.Unity.Analyzers.dll` with `Analyzers.asmdef` close to it
    • `Assets/Scripts/FeatureA/MyFeatureA.cs` with a `FeatureA.asmdef` close to it (having a reference to `Analyzers.asmdef`)
    • `Assets/Scripts/FeatureB/MyFeatureB.cs` with a `FeatureB.asmdef` close to it (without reference to `Analyzers.asmdef`)
    • `Assets/Scripts/FeatureC/MyFeatureC.cs` without asmdef (meaning it is part of the `Assembly-CSharp.dll`)
    • `Packages/00 - Simple Package/MySimplePackageScript.cs` with an asmdef for testing purposes.
    The `Microsoft.Unity.Analyzers.dll` used in that repo is a compiled version that threats the rule below as `error`:
    - https://github.com/microsoft/Microsoft.Unity.Analyzers/blob/main/doc/UNT0001.md

    Every script described above is a MonoBehaviour with an empty Start method, which should show errors in the Unity Console.

    With the settings described above, the analyzer is not catching anything. But if I just delete the `Analyzers.asmdef` and let the DLL alone, it starts catching all the scripts in the Assets (except the `MyFeatureC` which has no asmdef) and all the Packages (even the ones from Unity).

    I wonder if you could please clone my repo, do some tweaks to make it work, and then tell us where is the mistake.

    @Edit (i forgot the repo link, haha):
    - https://github.com/sandolkakos/unity-roslyn-analyzers

    Thanks a lot.
     
    Last edited: Jan 9, 2023
  11. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    @sandolkakos sure! can you share the repo? I dont see it in this thread :p
     
    sandolkakos likes this.
  12. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
    HaraldNielsen likes this.
  13. joconnormz

    joconnormz

    Joined:
    Jun 8, 2022
    Posts:
    2
    im also seeing the issue, exactly as described by sandolkakos
     
    sandolkakos likes this.
  14. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
    hey @HaraldNielsen, just pinging you to check if you had a chance to check my repo
     
  15. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    Hi @sandolkakos - sorry for the delay!
    So I just took a look, and its because we skip compilation of Asmdef's that dont have any *.cs files. (forgot about this myself)
    If you put a *.cs file in, it works. It started reporting the warnings for me after I did that
     
    McDev02, metal6_6_6 and sandolkakos like this.
  16. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
    Yupiiii, thanks a lot @HaraldNielsen , it works.
    I will push that class to the repo, so that people will know exactly how it should be set.

    upload_2023-3-2_17-57-7.png
     
    HaraldNielsen likes this.
  17. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    You don't even need an empty class, an empty .cs file is enough to get it compiled.
     
  18. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
  19. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    282
    I've noticed what seems to be another bug:

    Analyzers in the Assets folder root will run against embedded packages (packages that use "file://...").

    This is annoying if you're vendoring many of the unity packages as submodules, etc. They're not in Packages, but they should still get excluded.
     
    sandolkakos likes this.
  20. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,052
    @HaraldNielsen I have read that section but there are cases that it does not cover as for example if you import an analyzer through https://github.com/xoofx/UnityNuGet.

    In that case there is no asmdefs involved and it will scan the whole project.

    Isn't there a manual way to indicate what to scan and what not to scan?

    We have performance problems in assembly reloads for this reason...

    Every time we update the version of an analyzer (netanalyzers, sonar, meziantou, ...) it takes about 10 minutes until Unity responds.
     
    sandolkakos likes this.
  21. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    @bdovaz no we dont have a way to deal with that. Im wondering if the UnityNuGet system could add a Asmdef file so it could go under Scoped Analyzers so it does not explode like that. That would not automatically include the analyzers all over, but it would fix the issue where you are blocked for 10min.
     
    sandolkakos likes this.
  22. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,052
    Thanks, I ended up mergeing a PR that adds asmdefs so we can have more control.

    Thank you!
     
    HaraldNielsen likes this.