1

Following this answer I'm trying to reimplement my ISourceGenerator as an IIncrementalGenerator following along with these docs.

Now, I've got my main initializer method here:

    public void Initialize(IncrementalGeneratorInitializationContext context)
    {
        SpinWait.SpinUntil(() => Debugger.IsAttached); // Manually attach debugger here

        IncrementalValuesProvider<AdditionalText> extraTexts = context.AdditionalTextsProvider.Where(f => f.Path.EndsWith(".fluid.yml"));
        IncrementalValuesProvider<(string Name, string Content)> namesAndContents = extraTexts.Select((text, cancellationToken)
                                                                                                          => (Name: Path.GetFileNameWithoutExtension(text.Path),
                                                                                                              Content: text.GetText(cancellationToken)!.ToString()));

        context.RegisterSourceOutput(namesAndContents, (spc, nameAndContent) =>
                                                       {
                                                           Dictionary<string, string> generatedSource = _generatorService.Generate(nameAndContent.Content);
                                                           foreach ((string fileName, string source) in generatedSource)
                                                           {
                                                               spc.AddSource(fileName, source);
                                                           }
                                                       });
    }

If I drop a breakpoint on the spc.AddSource() line, I can see that the contents of my generatedSource dictionary looks like:

Debugger Window

ICanLockOrEnter.fluid.g.cs:

namespace SuperFluid.Tests.Cars;

public interface ICanLockOrEnter
{
    public ICanUnlockOrInitialize Lock();
    public ICanStartOrExit Enter();
}

Which all looks fine to me.

If I add EmitCompilerGeneratedFiles to my .csproj, I can even see that the files are correctly generated within the obj folder.

However, despite this, I can't reference any of these interfaces in my test project that's using the analyzer.

Am I missing a trick here? The analyzer seems to be running, if I debug it, the sources look fine, so why can't I reference them?

ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81
  • When you say you can’t reference the sources, is it that your IDE is saying it can’t resolve them, or is the compiler not resolving them? I.e. if you use them anyway (despite your IDE telling you otherwise), does it compile? – brads3290 Jun 18 '23 at 08:36
  • @brads3290 - This particular issue seemed to be an IDE one, I'll write up an answer. Sorry I'd forgotten to already do this – ScottishTapWater Jun 18 '23 at 17:31

1 Answers1

0

This seems to be an IDE issue.

Invalidating Rider's caches and restarting solved the problem.

ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81