Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Bug Roslyn Analyzer/Generator Scope Works Unexpectedly

Discussion in 'Experimental Scripting Previews' started by doublelift41, Nov 28, 2022.

  1. doublelift41

    doublelift41

    Joined:
    Aug 21, 2019
    Posts:
    7
    I tried to use source generator example in this unity documentation. This works correctly but I getting CS0436 error in console. Its normal or is there something wrong?
    Code (CSharp):
    1. Assets\Example.cs(7,16): warning CS0436: The type 'ExampleSourceGenerated' in 'UnitySourceGenerator\UnitySourceGenerator.ExampleSourceGenerator\exampleSourceGenerator.cs' conflicts with the imported type 'ExampleSourceGenerated' in 'Unity.EditorCoroutines.Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'UnitySourceGenerator\UnitySourceGenerator.ExampleSourceGenerator\exampleSourceGenerator.cs'.
     
    Last edited: Nov 28, 2022
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,882
    Sounds like the class you are generating uses the same class name as the script that generates it (which presumably is defined in Example.cs).
     
  3. doublelift41

    doublelift41

    Joined:
    Aug 21, 2019
    Posts:
    7
    No they are not same name.
    I decompiled Unity assemblies, for instance a UnityEngine.UI and I saw that source generator generates seperate code for every different assembly. Then I guess the meaning of the CS0436 warning is you are calling current Assembly's A.B.C() method but there are same declerated method in another assembly.
     
    Last edited: Nov 28, 2022
  4. doublelift41

    doublelift41

    Joined:
    Aug 21, 2019
    Posts:
    7
    Documentation says "Unity applies analyzers to all assemblies in your project’s Assets folder". But its generate code for assemblies in the Library folder too. According to the documentation this should not be the case.
    For instance UnityEngine.UI contains analyzers.

     
    Last edited: Nov 28, 2022
  5. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    Yes, the issue here is that you are generating public types with same namespace and class name for every Assembly.
    You can either make the name namespace/class more specialized to not get those errors. Alternatively make the generated class internal also helps
     
  6. doublelift41

    doublelift41

    Joined:
    Aug 21, 2019
    Posts:
    7
    Thanks, yes there is some ways to fix that. But I wondered that. It is the some kind of bug or documentations is misleading?
     
  7. AdamBebko

    AdamBebko

    Joined:
    Apr 8, 2016
    Posts:
    168

    Making the generated attribute class internal fixed it for me. Thanks.

    Code (CSharp):
    1.  
    2.  
    3.      public string Source => @"
    4. using System;
    5.  
    6. namespace MyGenerators
    7. {
    8.    [AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
    9.    internal class SomeGeneratedAttribute : Attribute
    10.    {
    11.    
    12.    }
    13. }";
     
    HaraldNielsen likes this.