8

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?

Impworks
  • 2,647
  • 3
  • 25
  • 53
  • 1
    That "ignored" flag is for source control: any generated files are not added/stored. The generator itself should work, unless there are compiler errors – Hans Kesting Jan 16 '23 at 11:21
  • 1
    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 `` might work? emphasis on the extra attributes there – Marc Gravell Jan 16 '23 at 11:21
  • 1
    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 here](https://github.com/dotnet/roslyn/issues/48083) – canton7 Jan 16 '23 at 11:34
  • 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? – Impworks Jan 16 '23 at 14:08

1 Answers1

3

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.

Impworks
  • 2,647
  • 3
  • 25
  • 53
  • 2
    The ignored icon is the source control bug, as mentioned at https://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. – Jason Malinowski Jan 17 '23 at 20:30
  • Could you share the content of both .csproj files? I am asking because I use Rider, the file (".g.cs") is generated, but ignored. – greenoldman Jun 22 '23 at 20:02