Roslyn source generator is "ignored"?
Asked Answered
H

1

12

I'm creating a simple Roslyn Source Generator. The generator seems to work well in debug, correctly generating the desired class. However, when referencing it from another project in the solution, a red icon is shown near it with the "Ignored" tooltip and no output is produced:

Ignored!

The generator code is very simple:

namespace CompetencesSourceGenerator
{
    using Microsoft.CodeAnalysis;

    [Generator]
    public class CompetenceNamesGenerator: CompetenceGeneratorBase
    {
        public override void Execute(GeneratorExecutionContext context)
        {
            context.AddSource(
                "CompetenceNames.g.cs",
                @"namespace Foo { public class Bar { } }"
            );
        }
    }
}

I checked the following:

  • Generator project targets .NET Standard 2.0
  • Referencing project targets .NET 6
  • Both Microsoft.CodeAnalysis.CSharp and Microsoft.CodeAnalysis.Analyzers in generator project are up to date
  • VS is up to date (17.4.4)
  • .NET Compiler Platform SDK is installed

Why does it not work?

Haver answered 16/1, 2023 at 11:18 Comment(4)
That "ignored" flag is for source control: any generated files are not added/stored. The generator itself should work, unless there are compiler errorsAshtonashtonunderlyne
what does the csproj look like in the code that expects the generator to appy? note that project-to-project references aren't very reliable - it works much better as a package reference; however, something like <ProjectReference Include="..\relativepathhere\CompetencesSourceGenerator.csproj" PrivateAssets="all" ReferenceOutputAssembly="false" OutputItemType="Analyzer" /> might work? emphasis on the extra attributes thereHaga
Also note that once you load a source generator from a local project, VS really doesn't like unloading it (e.g. if you change it and recompile it). You'll need to close VS (and makes sure that all of the associated OOP processes are dead). See hereImmethodical
After tweaking something here and there, the generator seems to be running now. However, the .g.cs file is not updated and you cannot actually see the current compilable source. Is this a known issue?Haver
H
6

So apparently, the issue was caused by three factors, the combination of which resulted in a very confusing UX:

  • The "Ignored" icon does indeed relate to the files emitted by the generator, not the generator itself. It would have made much more sense to display it next to each file, I have no idea why it works the way it does.
  • The contents of the generated files does not update in the UI. The generator runs, so you will see compilation errors when you emit incorrect code "right away". To view the actual source, you need to close VS altogether, reopen it, rebuild the project and only then view the source.
  • Referencing the generator from a project in the same solution is kinda wonky, because sometimes it does not run on "Rebuild all".

Worth mentioning that in Rider everything works just as expected, so I hope they fix this behavior in VS.

Haver answered 17/1, 2023 at 14:25 Comment(2)
The ignored icon is the source control bug, as mentioned at github.com/dotnet/project-system/issues/8202. It has nothing to do with whether we're ignoring the generator for the purposes of running it.Zeralda
Could you share the content of both .csproj files? I am asking because I use Rider, the file (".g.cs") is generated, but ignored.Icebreaker

© 2022 - 2025 — McMap. All rights reserved.