IncrementalGenerator生成的源代码不可用。

huangapple go评论75阅读模式
英文:

IncrementalGenerator Generated Source Not Available

问题

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:

IncrementalGenerator生成的源代码不可用。

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?

英文:

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(() =&gt; Debugger.IsAttached); // Manually attach debugger here

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

		context.RegisterSourceOutput(namesAndContents, (spc, nameAndContent) =&gt;
													   {
														   Dictionary&lt;string, string&gt; 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:

IncrementalGenerator生成的源代码不可用。

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?

答案1

得分: 0

这似乎是一个IDE问题。

清除Rider的缓存并重新启动解决了这个问题。

英文:

This seems to be an IDE issue.

Invalidating Rider's caches and restarting solved the problem.

huangapple
  • 本文由 发表于 2023年6月13日 12:05:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定