k94ll13nn3 / AutoConstructor

C# source generator that generates a constructor from readonly fields/properties in a class or struct
MIT License
50 stars 4 forks source link

Use across assemblies that use `[assembly: InternalsVisibleTo(...)]` produces warnings #93

Closed rickbrew closed 9 months ago

rickbrew commented 9 months ago

Let's say you have assembly A and assembly B, with B referencing A.

Assembly A declares [assembly: InternalsVisibleTo("B")]. Both assemblies make use of [AutoConstructor].

You'll get this warning when compiling assembly B:

warning CS0436: The type 'AutoConstructorAttribute' in 'AutoConstructor.Generator\AutoConstructor.Generator.AutoConstructorGenerator\AutoConstructorAttribute.cs' conflicts with the imported type 'AutoConstructorAttribute' in 'A, Version=1.2.3.4, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'AutoConstructor.Generator\AutoConstructor.Generator.AutoConstructorGenerator\AutoConstructorAttribute.cs'.

I compile with warnings-as-errors so this actually prevents me from compiling. Thankfully you can suppress this warning, which also prevents it from turning into an error, via .editorconfig (or elsewhere) and then the compiler is happy:

# CS0436: Type conflicts with imported type
# Ensures that per-assembly source-generated attributes, e.g. [AutoConstructor], will not raise errors
dotnet_diagnostic.CS0436.severity = none

@Sergio0694 just fixed this exact issue in a new generator by emitting the suppression automatically and only for the type in question.

Sergio0694 commented 9 months ago

@k94ll13nn3 if it helps, you should be able to pretty much just copy this, minus some tweaks 🙂

k94ll13nn3 commented 9 months ago

Thanks for the report and for the sample code. Fixed in 5.2.0, should be out on NuGet shortly.