0

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.

8
  • Your <!--NugetPackages--> is probably out-of-date Commented May 8 at 15:24
  • 3
    Have you tried throwing an exception in your generator? It is crude but should let you know for sure whether or not it is running since that will fail a build. Also, the Debugger calls will only work if you have a just-in-time debugger installed, which pretty much means Windows and within Windows I am not sure which debuggers other than Visual Studio register as JIT debuggers. Commented May 8 at 15:26
  • I've written some source generators here, you need to set the generator project as startup project, then if you have the .net compiler SDK Workload installed, you can test run the generators against one of the testprojects Commented May 8 at 15:48
  • Throwing an exception didn't work, but after updating VS to the latest available, reverting the CodeAnalysis Nugets to 4.8, deleting everything and rebuilding, the debugger finally triggered! There's definitely something to deleting the files and changing the source project that helps it run. Thanks for your help. Commented May 8 at 16:50
  • 2
    That would have explained it then -- if you're referencing the 4.13 versions of Roslyn, that means you were referencing a higher version than was actually in your VS (which is 4.12). You should have gotten an error in your error list in that case.... Commented May 10 at 0:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.