❔ Can't get the source generators tutorial to work
As the title says, I'm following the source generators tutorial that's here : https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview
But when I launch my console app (.net 7) nothing is displayed, I'm most likely stuck with step 7 (linking the source generator with my console app) but at this point I really don't see what could be wrong given I have no errors nor warnings...
Like displayed on step 9, I have SourceGenerator in my console app's analyzers but there's nothing below it (so no SourceGenerator.HelloSourceGenerator and Program.cs that appear on the tutorial's screenshot)
Source Generators - C#
Source Generators is a C# compiler feature that lets C# developers inspect user code as it is being compiled. Source generators create new C# source files on the fly that are added to the user's compilation.
12 Replies
Can you show your generator code and csproj(s)?
Also that tutorial is severely outdated,
ISourceGenerator
is depricatedBy depreciated, do you mean it is marked with ObsoleteAttribute?
My generator's code is just a copy paste of the tutorial's generator, about the csproj gimme a minute
It's not but you should pretend it is.
context.Compilation.GetEntryPoint(context.CancellationToken)
This isn't something you should be writing in real source generators.
Unfortunately there's a real lack of proper documentation around source generators, although the thing you should be using is IIncrementalGenerator
.
Difference is essentially that ISourceGenerator
will try to regenerate the code for each keystroke and/or change in the code, while with IIncrementalGenerator
you build up an incremental pipeline so the compiler can efficiently cache relevant data and only regenerate the code when something relevant has changed.IIncrementalGenerator only has Initialize?
yep
And using the context it gives you, you register several actions, eventually ending with
RegisterSourceOutput
$igAlright so the tutorial is definitely 100% irrelevant at least for the source generator's code 😅
yeahhh
Although I don't know why it isn't working for you, iirc the csproj setup the tutorial demonstrates should be fine.
Well I'll try using the "normal" way first, hopefuly it works better this way 😁
Also feel free to come by #roslyn and ask any other questions about SGs, it's a great resource
Generators csproj looks like that :
And the console app csproj looks like that
And after switching to the IIncrementalGenerator way, i still have the same issue (now my source generators project appears referenced as a project reference in my console app's dependencies)
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.