简体   繁体   English

从克隆的存储库本地运行和引用源代码生成器库

[英]Running and referencing a source generator library locally from a cloned repository

I'm trying to experiment with a specific source generator library and I wanted to try and modify some parts of it.我正在尝试使用特定的源代码生成器库进行试验,我想尝试修改其中的某些部分。 To do that cloned the repository for that library and referenced two.csproj files in it from my own .NET 7 project.为此,克隆了该库的存储库,并从我自己的 .NET 7 项目中引用了其中的 two.csproj 文件。 This looks at first like it would work, VS Code recognized the imports and doesn't complain, but the actual source generator part does not seem to work.这乍一看好像可行,VS Code 识别了导入并且没有抱怨,但实际的源代码生成器部分似乎不起作用。

The specific library I'm using is Mapperly and I referenced it in my.csproj file as following:我使用的特定库是Mapperly ,我在 my.csproj 文件中引用了它,如下所示:

  <ItemGroup>
    <ProjectReference Include="..\mapperly\src\Riok.Mapperly.Abstractions\Riok.Mapperly.Abstractions.csproj" />
    <ProjectReference Include="..\mapperly\src\Riok.Mapperly\Riok.Mapperly.csproj" />
  </ItemGroup>

The error I get is我得到的错误是

Partial method 'MyMapper.MapToReadModel(MyEntity)' must have an implementation part because it has accessibility modifiers.分部方法“MyMapper.MapToReadModel(MyEntity)”必须具有实现部分,因为它具有可访问性修饰符。

This code compiles if I add the complete nuget package of this library, and it stops working with this error if I remove the nuget package and reference the projects as I've shown.如果我添加此库的完整 nuget package,此代码将编译,如果我删除 nuget package 并引用项目,它会停止工作并出现此错误,如我所示。 My understanding of this error would be that it indicated that the source generator either did not generate any source, or the build process doesn't use the generated source for some reason.我对这个错误的理解是,它表明源代码生成器没有生成任何源代码,或者构建过程出于某种原因没有使用生成的源代码。 The way this library works is that you define partial methods for mapping, and the source generator fills in the actual code, so that last step seems to fail in this case.这个库的工作方式是你为映射定义部分方法,源代码生成器填充实际代码,所以在这种情况下最后一步似乎失败了。

Obviously the way I'm referencing this library makes it behave differently than what happens when I add the full nuget package. The repository has more.csproj file, but those are all tests or samples.显然,我引用这个库的方式使它的行为与我添加完整的 nuget package 时发生的行为不同。存储库有 more.csproj 文件,但这些都是测试或示例。 It also has a.sln file, but it seems I can't reference that directly.它也有一个 .sln 文件,但我似乎不能直接引用它。

I'm also using VS Code while the information I could find on this often assume Visual Studio, not sure if there are any limitations here with VS Code only.我也在使用 VS Code,而我能找到的信息通常假设 Visual Studio,不确定这里是否仅对 VS Code 有任何限制。

What is the proper way to reference a local library that contains a source generator so that it will work properly in VS Code?引用包含源代码生成器的本地库以使其在 VS Code 中正常工作的正确方法是什么? Or am I misunderstanding the problem here and the cause is something else entirely?还是我误解了这里的问题而原因完全是另外一回事?

The library you are referencing is not just regular library but a source generator, you need to reference them in a special way.您引用的库不仅是常规库,而且是源代码生成器,您需要以特殊方式引用它们。 You can see an example in documentation for source generators .您可以在源代码生成器的文档中看到示例。 Also note how authors of this library reference their source generator project in their tests:还要注意这个库的作者如何在他们的测试中引用他们的源代码生成器项目:

<!-- For local development, simply reference the projects. -->
  <ItemGroup Condition="'$(MapperlyNugetPackageVersion)' == ''">
    <ProjectReference Include="..\..\src\Riok.Mapperly\Riok.Mapperly.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    <ProjectReference Include="..\..\src\Riok.Mapperly.Abstractions\Riok.Mapperly.Abstractions.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
  </ItemGroup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM