Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Other 'MonoScriptInfoGenerator/AssemblyMonoScriptTypes.gen.cs' not ignored by analyzer

Discussion in 'Scripting' started by Chiv, Dec 15, 2022.

  1. Chiv

    Chiv

    Joined:
    Mar 6, 2014
    Posts:
    10
    Hi,

    after upgrading my 2019.3 project to 2022.2, the new included `Unity.SourceGenerators` are executed for all my assemblies, in embedded packages or otherwise.
    To maintain e.g. consistent styling, I use several Roslyn analyzers. Those are in `Assets/Analyzer/sub/folders/analyzer.dll`, properly tagged with `RoslynAnalyzer`.
    Because they reside in the `Assets` folder, all script in every assembly are analyzed. To prevent that, I have a `Default.ruleset` which disables all rules. For my own embedded packages, I have another ruleset, which enables the rules I want.
    With that, only the scripts in my packages are analyzed, while all others are ignored.
    This worked fine until 2022.2.

    The included `Unity.SourceGenerators` are now executed for my packages, which results in created source _in context of the package/assembly_. This generated source files get analyzed, and produce warnings, e.g. [1].

    Normally, to exclude files from the analyzers, one could mark them as generated via an `.editorconfig`

    [*.gen.cs]
    generated_code = true

    However, Unity's build process is as far as I understand not based on the one which respects .editorconfig-files - hence why I had to use rulesets in the first place, instead of configuring it all via .editorconfig.
    Therefore, I am at a loss on how to exclude those generated files from the analyzers.
    (The generated files are ending in `*.gen.cs`, according to Microsoft, files ending with `*.generated.cs` are excluded automatically - though I am unsure if this would be true for the compilation process Unity uses.)
    I have a build pipeline, which compiles the project with the additional compiler argument `-warnaserrors+`, to make sure there are no analyzer warnings left.
    Now, with the generated files and the warnings they are producing, the pipeline of course fails.

    To summarise, how do I exclude the generated files by `Unity.SourceGenerators/Unity.MonoScriptGenerator.MonoScriptInfoGenerator` from being analyzed by Roslyn analyzers?

    - My `Default.ruleset`, which disables everything, is getting ignored (files are generated in context of my embedded packages/assemblies, which have their own rulesets).
    - .editorconfig is getting ignored.
    - Removing the analyzers works, but is of course no solution.
    - Removing the analyzers from the `Asset` folder and putting them into all the embedded packages I want to analyze, does not work (generator runs in assembly context, using custom ruleset, same warnings).
    - Removing the analyzers from the `Asset` folder and putting them into all the embedded packages I want to analyze _and_ using the `Default.ruleset` does work (all rules are disabled; not what I want).

    [1]:
    Code (CSharp):
    1. Unity.SourceGenerators/Unity.MonoScriptGenerator.MonoScriptInfoGenerator/AssemblyMonoScriptTypes.gen.cs(1,1): warning SA1517: Code should not contain blank lines at start of file
    2. Unity.SourceGenerators/Unity.MonoScriptGenerator.MonoScriptInfoGenerator/AssemblyMonoScriptTypes.gen.cs(24,16): warning SA1025: Code should not contain multiple whitespace characters in a row
    3. Unity.SourceGenerators/Unity.MonoScriptGenerator.MonoScriptInfoGenerator/AssemblyMonoScriptTypes.gen.cs(26,20): warning SA1025: Code should not contain multiple whitespace characters in a row
    4. Unity.SourceGenerators/Unity.MonoScriptGenerator.MonoScriptInfoGenerator/AssemblyMonoScriptTypes.gen.cs(24,1): warning SA1516: Elements should be separated by blank line
    5. Unity.SourceGenerators/Unity.MonoScriptGenerator.MonoScriptInfoGenerator/AssemblyMonoScriptTypes.gen.cs(23,29): warning SA1201: A method should not follow a struct
    6. Unity.SourceGenerators/Unity.MonoScriptGenerator.MonoScriptInfoGenerator/AssemblyMonoScriptTypes.gen.cs(11,25): warning SA1307: Field 'isPartial' should begin with upper-case letter
    7. Unity.SourceGenerators/Unity.MonoScriptGenerator.MonoScriptInfoGenerator/AssemblyMonoScriptTypes.gen.cs(10,27): warning SA1307: Field 'className' should begin with upper-case letter
    8. Unity.SourceGenerators/Unity.MonoScriptGenerator.MonoScriptInfoGenerator/AssemblyMonoScriptTypes.gen.cs(9,27): warning SA1307: Field 'nameSpace' should begin with upper-case letter
    9. Unity.SourceGenerators/Unity.MonoScriptGenerator.MonoScriptInfoGenerator/AssemblyMonoScriptTypes.gen.cs(18,27): warning SA1307: Field 'filePath' should begin with upper-case letter
    10. Unity.SourceGenerators/Unity.MonoScriptGenerator.MonoScriptInfoGenerator/AssemblyMonoScriptTypes.gen.cs(19,37): warning SA1307: Field 'monoScriptInfos' should begin with upper-case letter
    11. [...]
     
    Last edited: Dec 16, 2022
    okoUser1 and agate-pris like this.
  2. okoUser1

    okoUser1

    Joined:
    Jan 9, 2020
    Posts:
    7
    Hey Chiv

    I had the same problem and found a possible solution in another thread:
    https://forum.unity.com/threads/vs-...r-updating-to-tech-stream-2022-2-0f1.1372701/

    It's a bit hacky, but it basically removes the source generators from the analyzers in the .csproj when generating it.

    Edit: I used it in our project and it sadly still doesn't seem to work as intended. It does remove the lines referring to the Source Generators though from the .csproj, but it doesn't get rid of the warnings. Would also love to see a solution here!


    Code (CSharp):
    1.     public class FixWarningPostprocessor : AssetPostprocessor
    2.     {
    3.         private static string OnGeneratedCSProject(string path, string content)
    4.         {
    5.             var document = XDocument.Parse(content);
    6.             if (document.Root == null)
    7.             {
    8.                 return content;
    9.             }
    10.          
    11.             document.Root.Descendants()
    12.                 .Where(x => x.Name.LocalName == "Analyzer")
    13.                 .Where(x => x.Attribute("Include")!.Value.Contains("Unity.SourceGenerators"))
    14.                 .Remove();
    15.  
    16.             return document.Declaration + System.Environment.NewLine + document.Root;
    17.         }
    18.     }
     
    Last edited: Jan 16, 2023
  3. NoTuxNoBux

    NoTuxNoBux

    Joined:
    Oct 2, 2020
    Posts:
    33
    I've been taking 2022.2.3 for a test drive on our existing 2021 projects and we are now also experiencing this problem. Some analyzer rules I could disable to get the errors from the generated code out, but others I can't without affecting code quality.

    Has anyone reported an issue on the Unity bug tracker about this yet?

    I also noticed the "Enable Roslyn Analyzers" option has disappear entirely in 2022.2, so we are now being blocked from entering play mode - we only use analyzers in OmniSharp through Unity, and don't need Unity to log their messages. I was able to work around this by scoping our analyzers properly, though.

    EDIT: I'm reporting an issue to the Unity tracker now.
     
    Last edited: Jan 26, 2023
  4. okoUser1

    okoUser1

    Joined:
    Jan 9, 2020
    Posts:
    7
    Do you have a link to your issue on the bug tracker so we can vote as well?
     
  5. NoTuxNoBux

    NoTuxNoBux

    Joined:
    Oct 2, 2020
    Posts:
    33
    Yes, but I noticed that all issues I report through the editor now end up in their (private) Atlassian service desk first. The link is https://unity3d.atlassian.net/servicedesk/customer/portal/2/IN-30020 but it only allows access to invited people and cannot be set to public, it seems.

    I think that once it's confirmed, it automatically syncs to issuetracker.unity3d.com at some point, but at this point I can't find the post yet.

    For future reference: the internal incident ID is IN-30020 and the title of the issue is "Code generated by source generators not ignored by Roslyn analyzers" (so someone can find it if it does show up).
     
    okoUser1 likes this.
  6. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    Also running into this issue. This really needs to get fixed.
     
  7. NielsTerHeijden

    NielsTerHeijden

    Joined:
    Mar 4, 2014
    Posts:
    20
    Having the same issues, also can't find the bug report on the issuetracker.unity3d.com website
     
  8. NoTuxNoBux

    NoTuxNoBux

    Joined:
    Oct 2, 2020
    Posts:
    33
    FYI, I haven't received any updates from Unity yet on the bug report .
     
  9. NoTuxNoBux

    NoTuxNoBux

    Joined:
    Oct 2, 2020
    Posts:
    33
    I've just had confirmation of this issue. It seems that the issue is now publicly visible here.
     
    okoUser1 likes this.
  10. Tallek

    Tallek

    Joined:
    Apr 21, 2014
    Posts:
    34
    This issue is labeled as fixed in the issue tracker (2022.2.20) however I am still experiencing a problem with it. The warnings from the log are gone and my project now compiles rather than hangs, but compilation is taking around 3.5 minutes for one of my assemblies rather than the 2 seconds it was taking before. If I disable all StyleCop rules in my ruleset, compilation returns to the 2 second time it was taking before.

    It appears as if the warnings/errors are being ignored now, but the analyzers are still running on the generated code causing compilation times to get absurd (at least in my case).
     
  11. NoTuxNoBux

    NoTuxNoBux

    Joined:
    Oct 2, 2020
    Posts:
    33
    Yes, as I mentioned above, the "Enable Roslyn Analyzers" option seems to have been removed in Unity 2022, so they are now always enabled. We also disabled it before because we only care about seeing these messages in our IDE, not in Unity. They now always slow down iteration times, and analyzer errors now also block entering play mode whilst they are not compiler errors.

    There is an additional annoyance that analyzers such as Roslynator use editorconfig to configure some rules, which tools such as OmniSharp pass along file, but Unity doesn't read them, so you get different behaviour in Unity for these options than inside the editor.

    FWIW, in the original support ticket I briefly mentioned both, and they just said you'd have to log a new, separate ticket for them.
     
  12. NoTuxNoBux

    NoTuxNoBux

    Joined:
    Oct 2, 2020
    Posts:
    33
    Update: I reported the issue where configuration in .editorconfig files are not passed to Roslyn analyzers when they run in the Unity Editor upstream. I haven't gotten confirmation (nor, as a result, a public link) yet.
     
  13. NoTuxNoBux

    NoTuxNoBux

    Joined:
    Oct 2, 2020
    Posts:
    33