dansiegel / CodeGenHelpers

The CodeGenHelpers is meant to help anyone who is working on C# CodeGenerator
MIT License
93 stars 17 forks source link

Ship V2 as a source generator instead of a library #67

Open Blackclaws opened 3 months ago

Blackclaws commented 3 months ago

Adding libraries to source generators as dependencies is notoriously hard:

https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md#use-functionality-from-nuget-packages

And it gets worse if multiple generators use the same library but in different versions as roslyn does not currently load them into different domains.

Instead I would suggest shipping V2 as a generator in its own right that simply adds these files post initialization so they are compiled into the source generator directly without people needing to copy things manually.

MichaelHochriegl commented 3 months ago

Nice idea, but AFAIK stacking source gens on top of each other should not be done, as execution order can vary. Or did this change somehow and can now be done safely?

Blackclaws commented 3 months ago

Stacking source generators is a difficult topic and by default not even supported unless you take a direct dependency on the other source generator and then call it manually as source generators cannot see the output of other source generators.

That is however not relevant here, as I am not proposing packaging this project as an additional source generator to be applied to the final target but instead to use this project as a source generator for a source generator. Meaning the generated source will be compiled into the generator and just be part of it as if you copied the files in.

This is not stacking source generators at all.

MichaelHochriegl commented 2 months ago

Ah, sorry, I must have misunderstood your approach than. I played around with SGs a little more and get your solution now. Thanks for clarification.