Search code examples
c#.netroslynsourcegenerators

Error RS1035 "The symbol is banned for use by analyzers" while using GeneratorExecutionContext.AddSource method


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

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

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

    public void Initialize(IncrementalGeneratorInitializationContext context)
    {
    }
}

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

How can I solve this issue?


Solution

  • 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 old 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, not an "old 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.