3

Beginner to Roslyn Source Generators here.

I'm following the documentation on Source Generators by Microsoft. I have created a .NET Standard 2.0 Class Library, and set the following as the contents of the csproj file:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
      
      <!-- Property below is required for [GeneratorAttribute] -->
      <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
  </PropertyGroup>

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

</Project>

Now, based on the sample code in the documentation, I created a new class with [Generator] attribute, that follows the ISourceGenerator interface:

[Generator]
public class Sample : ISourceGenerator
{
    public void Execute(GeneratorExecutionContext context)
    {
    }

    public void Initialize(GeneratorInitializationContext context)
    {
    }
}

But, when I add context.AddSource method invocation in the Execute method:

[Generator]
public class Sample : ISourceGenerator
{
    public void Execute(GeneratorExecutionContext context)
    {
        context.AddSource("Foo.g.cs", "public class Bar { }");
    }

    public void Initialize(GeneratorInitializationContext context)
    {
    }
}

then I see this syntax error on context.AddSource method invocation:

RS1035 The symbol 'GeneratorExecutionContext' is banned for use by analyzers: Non-incremental source generators should not be used, implement IIncrementalGenerator instead

How can I solve this issue?

2
  • Afaik the incremental source generator has a different api,no? Commented Oct 19, 2024 at 15:56
  • If you want an example of how to write Incremental generators, check out this link. It contains a launchsettings file that allows you to debug your generator. It uses a minimum of boilerplate code Commented Mar 22 at 19:03

2 Answers 2

8

Based on some now-deleted comments, I figured out that the exact equivalent of this:

[Generator]
public class Sample : ISourceGenerator
{
    public void Execute(GeneratorExecutionContext context)
    {
        context.AddSource("Foo.g.cs", "public class Bar { }");
    }

    public void Initialize(GeneratorInitializationContext context)
    {
    }
}

would be:

[Generator(LanguageNames.CSharp)]
public class Sample : IIncrementalGenerator
{
    public void Initialize(IncrementalGeneratorInitializationContext context)
    {
        string sourceCode = ...;
        context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
                "FileName.g.cs", SourceText.From(sourceCode, Encoding.UTF8)));
    }
}

It seems like the previous source generators are now deprecated in a way that we cannot use them anymore. The only type of source generators we can use today are incremental source generators. The exact equivalent works because it's an incremental source generator.

Additionally, see this markdown file on GitHub and this article by Andrew Lock.

Note: Links in answer were also provided by comments which are now deleted.

Sign up to request clarification or add additional context in comments.

Comments

-1

Following the syntax error, I decided to actually implement IIncrementalGenerator: [...]

However, even then, the same exact error is still reported.

Yes, of course, because you are still using GeneratorExecutionContext. The error told you in no uncertain terms that it is verboten to use GeneratorExecutionContext.

How can I solve this issue?

Do not use GeneratorExecutionContext.

1 Comment

@winscripter, well, to be honest, does the answer really need to state the obvious, to familiarize oneself with and use IncrementalGeneratorInitializationContext, but i believe you don't need to be told that, as you are already attempting to use IncrementalGeneratorInitializationContext, right? (github.com/dotnet/roslyn/blob/main/docs/features/…, github.com/dotnet/roslyn/blob/main/docs/features/…). You asked about an error, and i precisely answered how to avoid/solve this error.

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.