部署包含对其他私有项目的引用的 C# 源生成器项目
我想发布一个源生成器包,并且我想包含对源生成器项目的私有项目依赖项。
例如,假设我的项目是 A.SourceGenerator
和 A.CodeAnalysis.Core
。我希望 A.SourceGenerator
依赖于 A.CodeAnalysis.Core
,但使用 ProjectReference
而不是 PackageReference
。
这意味着我希望在我的 A.SourceGenerator.csproj
文件中包含以下行或类似的内容:
<None Include="$(OutputPath)\A.CodeAnalysis.Core.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
问题是这个或一些类似的变体不起作用,并且会产生源生成错误,在 CS8785 编译器警告下,出现以下错误消息:
Generator BenchmarkSourceGenerator
无法生成源代码。它不会对输出产生影响,因此可能会出现编译错误。
异常类型为 FileNotFoundException
,并带有消息:
无法加载文件或程序集 A.CodeAnalysis.Core、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null
或其中之一它的依赖项。系统找不到指定的文件。
显示上述错误是因为项目的内容需要本地复制到源生成器所在的目录上。使用 PackageReference
时有一个技巧,如 此处,但它显然不适用于 ProjectReference
的情况。
我宁愿避免创建源生成器所依赖的全新 NuGet 包,而只是为了包含依赖项。另一种解决方案是不将其抽象化,并且只有一个包含所有源生成器的源生成器项目。
I want to release a source generator package, and I want to include a private project dependency on the source generator project.
For example, assume that my projects are A.SourceGenerator
and A.CodeAnalysis.Core
. I want A.SourceGenerator
to depend on A.CodeAnalysis.Core
, but use ProjectReference
instead of PackageReference
.
This means that I want the following line or something similar in my A.SourceGenerator.csproj
file:
<None Include="$(OutputPath)\A.CodeAnalysis.Core.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
The issue is that this, or some similar variations, won't work and will produce a source generation error, under the CS8785 compiler warning, with the following error message:
Generator BenchmarkSourceGenerator
failed to generate source. It will not contribute to the output and compilation errors may occur as a result.
Exception was of type FileNotFoundException
with message:
Could not load file or assembly A.CodeAnalysis.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
or one of its dependencies. The system cannot find the file specified.
The above error is shown because the project's contents need to be locally copied on the directory that the source generator lies on. There is a trick for that when using PackageReference
, as shown here, but it clearly does not apply for the case of ProjectReference
.
I would prefer to avoid creating a brand new NuGet package that the source generator depends on, just to include the dependencies. The other solution is to not abstract that away and only have one source generator project including all source generators.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对项目文件了解不多,但至少我可以分享我的项目文件的一些内容。
我必须参考这样的其他项目:
我不记得在哪里找到这个,但我希望它有帮助。
更新:
有人向我解释了这一点,我希望我能解释清楚:
基本上为了不干扰包的其余部分,
OutputItemType="Analyzer"
导致 C# 分析器和 SG 打包在analyzers/dotnet/cs
下。但在那里它将无法找到它的正常依赖项。
因此,您添加
PackagePath="analyzers/dotnet/cs"
将包依赖项与您的分析器/SG 一起打包。为了将项目依赖项打包到正确的路径,您可以添加
OutputItemType="Analyzer"
,即使它们可能不是分析器。I don't have much knowledge about project files, but at least i can share some contents of my project file.
I had to reference other projects like this:
I don't remember where i found this, but i hope it helps.
Update:
I had someone explain this to me and I hope I'll get it right:
Basically in order to not interfere with the rest of the Package,
OutputItemType="Analyzer"
causes C# Analyzers and SGs to be packed underanalyzers/dotnet/cs
.But there it won't be able to locate it's normal dependencies.
So you add
PackagePath="analyzers/dotnet/cs"
to pack your package dependencies alongside your Analyzer/SG.And to get your project dependencies packed to the right path, you can add
OutputItemType="Analyzer"
even though they might not be analyzers.