beakona / AutoInterface

C# interface-to-member source generator
MIT License
73 stars 9 forks source link

Fixed CS0436 when used in multiple projects and hidden the attribute from the build output #12

Closed zitmen closed 2 years ago

zitmen commented 2 years ago

Hi, it is a nice project you have here. I would like to use it but there are some issues when referenced in a bigger project.

1) The output binaries contain the attribute despite the fact this is a Roslyn component 2) When the nuget is referenced from multiple projects, I get CS0436: Type conflicts with the imported type

This PR fixed both issues.

The first one simply by using System.Diagnostics.ConditionalAttribute

The second by extracting the attribute to a separate dll, which is automagically referenced by the projects as an extra dependency and thus it is not needed to generate the attributes by the generator. Here is some reference for this problem and solution: https://andrewlock.net/creating-a-source-generator-part-7-solving-the-source-generator-marker-attribute-problem-part1/ There is potentially also this package: Code.Generation.Roslyn.Attributes, which is neat, but I assumed you don't want to introduce another dependency, especially when it has a different license.

zitmen commented 2 years ago

Hi @beakona, thanks for accepting the PR. When can I look forward to a new nuget? :)

beakona commented 2 years ago

Hi @zitmen, I've made v1.0.21-preview yesterday.

Here is sequence of events that led me to preview and not actual release, for now.

I have:

  1. read blogpost you mentioned
  2. applied your PR
  3. found compiler warning in both local buildchain and github buildchain
  4. read blog post several times and fix issue after few trials

Then I tried to use new source generator in real project and got error that compiler sees *Attributes twice:

I decided to make preview in order not to break existing projects 'without developer doing nothing'. This kind of reasoning may be incorrect??

Also, there is breaking change. Now you can't use reflection to find which members are autogenerated because *Attributes are erased (because of conditional)

zitmen commented 2 years ago

Then I tried to use new source generator in real project and got error that compiler sees *Attributes twice

This happened to me too, but it is a Visual Studio problem. When you close the VS and then start it again, it works fine. There is no issue because the autogenerated attribute is no longer there unless you are referencing 2 versions of the nuget, which you should not do anyway.

I am ok with preview, I just need to reference something :)

Also, there is breaking change. Now you can't use reflection ...

Interesting, I would not think of such use case. Not sure what it is good for..? But if someone relies on that, then how about introducing a conditional compilation so I can opt in into the attribute removal? Because I am excluding the dlls from runtime as there is no need for it and neither for the attribute (at least in all my use cases). Alternatively there could be a different attribute, but that's probably confusing.

Thanks, Martin