C#源生成器:如何在生成的代码中调试编译器错误?

发布于 2025-02-01 08:03:20 字数 313 浏览 3 评论 0原文

我正在尝试

但是,从根本上讲,我在生成的代码中调试错误遇到了很大的麻烦。当我在源生成器中的模板中犯了一个错误并尝试编译时,我可能会在生成的文件中遇到“方法必须具有返回类型”之类的错误。但是,当我双击错误时,它不会带我进入生成的代码。这使得很难看到它出了什么问题。

有一个技巧吗?是否有任何方法可以检查生成的代码时无法编译的代码?更一般而言,在生成代码可用于IntelliSense时以及 时的控制权?

我正在使用Visual Studio Professional 2022版本17.1.6和Resharper 2022.1。

提前致谢!

I am experimenting with C# source generators. I have spent about a day on it, and I find it a very frustrating and painful experience. IntelliSense is extremely unreliable. It occasionally works, but most often it does not, and I have not been able to figure out any system to it. (Restarting Visual Studio does not help.)

But more fundamentally, I have great trouble debugging errors in the generated code. When I make a mistake in the template in the source generator and try to compile, I might get errors like "Method must have a return type" in the generated file. But when I double-click on the error, it doesn't take me to the generated code. That makes it extremely hard to see what is wrong with it.

Is there a trick to it? Is there any way to inspect the generated code when it fails to compile? And more generally, what governs when the generated code is available for IntelliSense and when it is not?

I am using Visual Studio Professional 2022 version 17.1.6 and ReSharper 2022.1.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木緿 2025-02-08 08:03:20

将以下内容添加到您的代码生成器构造函数代码:

#if DEBUG 
    if (!Debugger.IsAttached) 
    { 
        Debugger.Launch(); 
    } 
#endif

现在您可以在Visual Studio中使用断点。

IntelliSense确实是与代码生成器的VS中的垃圾。您需要关闭VS,删除 /bin和 /obj文件夹,重新打开VS,然后重建。每当您进行更改时,都必须完成此操作。 VS将在您更改某些内容后显示旧版本,IntelliSense将标记新事物是错误的,但是它仍然会编译和运行...

为了获得更好的体验,我建议使用Rider,该骑手为代码生成器提供更好的集成体验。

Add the following to your code generator constructor code:

#if DEBUG 
    if (!Debugger.IsAttached) 
    { 
        Debugger.Launch(); 
    } 
#endif

Now you can use breakpoints in Visual Studio.

Intellisense is indeed garbage in VS with code generators. You need to close VS, delete /bin and /obj folders, reopen VS, then rebuild. This has to be done everytime you make changes. VS will show the old version after you changed something, intellisense will mark new things as error, yet it will still compile and run...

For a better experience I recommend using Rider, which has a much better integration experience for code generators.

飞烟轻若梦 2025-02-08 08:03:20

我找到了我问题的部分解决方案。

首先,在使用源发电机时,请勿在Visual Studio 中使用Resharper Builder。它没有提供正确的错误消息。而是从命令行运行dotnet build。您会收到更好的错误消息。

实际上,我的问题的一部分可能是由于我的错误或限制造成的(尽管我没有调查过这一点,所以这是纯粹的猜测)。

其次,您可以在不在编译时间上运行源生成的情况下测试源发电机的内部。。

  1. 重构源生成器并提取辅助类别(我们可以称其为 sourceGeneratorHelper.cs ),该类别吸收您的输入(例如XML)并产生输出(例如A StringBuilder )。
  2. 制作一个单独的C#测试项目 sourceGeneratorHelpertest.csproj 用于测试类 sourceGeneratorHelper.cs 。在此项目中,将您的源生成器项目引用,就好像它是一个常规项目一样 - 即,请勿在您的项目参考中使用outputIteMtype =“ Analyszer”
  3. 现在,您可以单元测试源生成器的内部。您也可以将生成的C#代码转移到文件中并进行检查。

此外,正如NightOWL888所指出的那样,我可以将其添加到引用项目中,以使其吐出生成的文件到磁盘(

<PropertyGroup>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

我尚未解决Intellisense问题,但是现在我至少可以忍受使用该生成器的工作。

I have found a partial solution to my problem.

First, do not use the ReSharper builder in Visual Studio when working with source generators. It does not give proper error messages. Instead, run dotnet build from a command line. You'll get much better error messages that way.

In fact, part of my problem may be due to bugs or limitations in ReSharper (though I have not investigated this, so that is pure speculation).

Second, you can test the innards of your source generator without running the source generation at compile time.

  1. Refactor your source generator and extract a helper class (we can call it SourceGeneratorHelper.cs) that takes your input (e.g. XML) and produces output (e.g. a StringBuilder).
  2. Make a separate C# test project SourceGeneratorHelperTest.csproj for testing the class SourceGeneratorHelper.cs. In this project, reference your source generator project as if it were a regular project - i.e., DO NOT USE OutputItemType="Analyzer" in your project reference.
  3. Now you can unit test the innards of your source generator. You can also dump the generated C# code to a file and inspect it.

Moreover, as NightOwl888 pointed out, I can add this to the referencing project in order to get it to spit out the generated files to disk (
as explained in this article):

<PropertyGroup>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

I have not solved the IntelliSense problem, but now I can at least endure working with this generator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文