I am writing a code generator to add functions to existing interfaces and that sort of thing. But I can't even get the project to launch.
To be clear, it builds, compiles, goes through all the motions, but if I start off Initialize with Debugger.Launch() I never get the option to break in.
I'm running in debug mode.
I have this config:
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<!--NugetPackages-->
<ItemGroup>
<ProjectReference Include="..\..\SourceGeneration\SourceGenerator\SourceGenerator.csproj"
OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
In my target project (that is, the project I want to augment).
My source generating project looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<OutputItemType>Analyzer</OutputItemType>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<IsRoslynComponent>true</IsRoslynComponent>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.13.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SourceGeneratorShared\SourceGeneratorShared.csproj" />
</ItemGroup>
</Project>
And my initialization code looks like this:
[Generator]
public class InterfaceStuff : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
Debugger.Launch();
Debugger.Break();
var provider = context.SyntaxProvider.CreateSyntaxProvider(
predicate: static (node, _) => IsTargetClass(node),
transform: static (ctx, _) => GetTargetInfo(ctx))
.Where(static info => info is not null).Collect();
context.RegisterSourceOutput(provider, (ctx, collected) =>
{
foreach (var item in collected)
{
var source = GenerateSource(item!);
ctx.AddSource($"{item.ClassName}.g.cs", SourceText.From(source, Encoding.UTF8));
}
});
}
}
A few more details:
- Generated folders get generated in the expected locations, but don't contain anything
- My approach to getting the code to rebuild is to change code in both projects, clean the entire solution, and rebuild the target project. I have gone so far as to commit all of my code, delete everything, and rebuild.
If you need anything else, please let me know in the comments.
.net compiler SDKWorkload installed, you can test run the generators against one of the testprojects