C
C#11mo ago
wtq40cy3n

What's the proper way to build source generator?

Problem I've just started my source generator with minimal example. When I made change to C.Text, and run dotnet build or dotnet clean && dotnet build for generator project, the change doesn't apply to the console app references this generator(sometimes it may work) Console project
using MyGenerator;

Console.WriteLine(C.Text);
using MyGenerator;

Console.WriteLine(C.Text);
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../MySourceGenerator/MySourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../MySourceGenerator/MySourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
## Generator project
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
</ItemGroup>

</Project>
No description
2 Replies
Thinker
Thinker11mo ago
If you're asking about the proper way to build source generators, then the proper way is to write unit tests for the generator. It's generally a pain to debug and test generators by running them from the command-line, and tests are a much more reliable method of testing them. On another note, this generator is quite obviously a work-in-progress, but you should never do context.CompilationProvider.Combine(...); as passing Compilations through your incremental pipeline completely breaks incrementality.
wtq40cy3n
wtq40cy3nOP11mo ago
I simply followed the introduction from dotnet channel on youtube, I can't find documentation of IIncrementalGenerator with unit testing, they're almost all about ISourceGenerator. Currently I don't really understand what incrementality is, is a there any recommended doc or video to follow?
Want results from more Discord servers?
Add your server