源生成器正在间接关联的项目上运行。

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

Source Generator Running on Indirectly Associated Projects

问题

我已经构建了一个增量源代码生成器,并将其打包成一个NuGet包,以便被另一个解决方案使用。我已经在我的“Models”项目中引用了它。问题是,“Models”项目被我的解决方案中许多其他项目引用。出于某种原因,源代码生成器在引用我的模型项目的这些项目中的每一个上运行,即使它们没有引用包含我的源代码生成器的NuGet包。我想知道为什么会这样,并且如何阻止它发生。

英文:

I have built an incremental source generator and packaged it into a nuget package to be consumed by a different solution. I have referenced it in my 'Models' project. The issue is that the Models project is referenced by a lot of other projects in my solution. For some reason the source generator is running on each of these projects that reference my models project even though they do not reference the nuget package with my source generator. I want to know why this is, and how to stop it from happening.

答案1

得分: 3

我已找到答案,非常简单。将PrivateAssets="all"添加到您的包引用中。在我的情况下,它看起来像这样:

<PackageReference Include="MapGenerator.Generator" Version="0.3.5-beta" PrivateAssets="all"/>

我看到有几个地方说,如果您将源代码生成器打包为NuGet包,就不需要考虑其他问题。但在我看来,这是一个相当重要的问题。

总之,如果您希望源代码生成器仅在引用它的项目上运行,那么必须添加PrivateAssets属性。

英文:

I have found the answer which is pretty simple. Add PrivateAssets="all" to your package reference. In my case it looks like:

<PackageReference Include="MapGenerator.Generator" Version="0.3.5-beta" PrivateAssets="all"/>

I saw several places that said if you were packing a source generator as a nuget package, there weren't any additional considerations that needed to be made. This in my mind was a pretty big one though.

In conclusion, if you want the source generator to run ONLY on the project that is referencing it, then you must add the PrivateAssets attribute.

huangapple
  • 本文由 发表于 2023年7月12日 23:47:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672382.html
匿名

发表评论

匿名网友

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

确定