C
C#9mo ago
nalka

❔ 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
Thinker
Thinker9mo ago
Can you show your generator code and csproj(s)? Also that tutorial is severely outdated, ISourceGenerator is depricated
nalka
nalka9mo ago
By 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
Thinker
Thinker9mo ago
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.
nalka
nalka9mo ago
IIncrementalGenerator only has Initialize?
Thinker
Thinker9mo ago
yep And using the context it gives you, you register several actions, eventually ending with RegisterSourceOutput $ig
nalka
nalka9mo ago
Alright so the tutorial is definitely 100% irrelevant at least for the source generator's code 😅
Thinker
Thinker9mo ago
yeahhh Although I don't know why it isn't working for you, iirc the csproj setup the tutorial demonstrates should be fine.
nalka
nalka9mo ago
Well I'll try using the "normal" way first, hopefuly it works better this way 😁
Thinker
Thinker9mo ago
Also feel free to come by #roslyn and ask any other questions about SGs, it's a great resource
nalka
nalka8mo ago
Generators csproj looks like that :
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" PrivateAssets="all"/>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" PrivateAssets="all"/>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
</ItemGroup>
</Project>
And the console app csproj looks like that
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Platforms>AnyCPU;x86</Platforms>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SourceGenerators\SourceGenerators.csproj" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Platforms>AnyCPU;x86</Platforms>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SourceGenerators\SourceGenerators.csproj" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
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)
Accord
Accord8mo ago
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.
Want results from more Discord servers?
Add your server
More Posts