C# dotnet使用源代码生成器进行构建

6

我在我的 .NET 5.0 项目中有一个源代码生成器。在 Visual Studio 中一切都正常,但是当我尝试使用 dotnet cli 构建应用程序时,源代码生成器不会运行:

error CS1061: 'CentralSystemResponseSender' does not contain a definition for 'SendResponseAsync' and no accessible extension method 'SendResponseAsync' accepting a first argument of type 'CentralSystemResponseSender' could be found (are you missing a using directive or an assembly reference?)

MSBuild版本:Microsoft (R) Build引擎版本16.9.0+57a23d249,用于.NET


编辑

在Visual Studio开发人员PowerShell中运行msbuild -t:Rebuild构建成功,但我无法在Gitlab CI/CD中使用。

项目结构:

-- Project.Api
     /-- SourceGenerator.A
  /-- Project.Domain
     /-- SourceGenerator.B

dotnet build "Project.Domain.csproj" 成功。
dotnet build "Project.Api.csproj" 抛出错误,因为 SourceGenerator.B 没有生成代码。
构建整个解决方案也可行,但是 dotnet publish 再次抛出错误,因为 SourceGenerator.B 没有生成代码。

Dockerfile:


FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY . .
RUN dotnet restore "generators/SourceGenerator.A/SourceGenerator.A.csproj"
RUN dotnet restore "generators/SourceGenerator.B/SourceGenerator.B.csproj"
RUN dotnet restore "src/Project.Domain/Anaconda.Domain.csproj"
RUN dotnet restore "src/Project.Api/Project.Api.csproj"
RUN dotnet build -c Release -o /app/build

FROM build AS publish
RUN dotnet publish -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Project.Api.dll"]

Project.Api.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <DockerfileContext>..\..</DockerfileContext>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.9.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\generators\Project.Generator.A\Project.Generator.A.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    <ProjectReference Include="..\Project.Domain\Anaconda.Domain.csproj" />
  </ItemGroup>
</Project>

Project.Domain.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\generators\Project.Generator.B\Project.Generator.B.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  </ItemGroup>
</Project>

嗨,祝贺您在StackOverflow上提出的第一个问题!您是在使用公开可用的生成器还是有一个能够重现此问题的最小可复现示例?详细的构建日志或者二进制日志(警告:包含路径、环境变量和项目文件内容)可能会有所帮助。 - undefined
1
你也可以尝试使用开发者命令提示符通过 msbuild -t:Rebuild 进行构建,以查看是否是生成器的问题,因为 dotnet cli 在 .NET 5 上运行生成器,而 VS 和 MSBuild 在 .NET Framework 上运行生成器。(但在这一点上只是猜测) - undefined
源代码生成器在Visual Studio中工作正常,我认为问题出在构建命令上,因为dotnet publish忽略了依赖项中的生成器。 - undefined
@MartinUllrich 我更新了问题,有什么想法吗? - undefined
2
嗨@Daniel,我刚刚将“Microsoft.Net.Compilers.Toolset”包添加到Project.Domain.csproj中,问题解决了。 - undefined
显示剩余4条评论
1个回答

0
希望这个答案能在两年后帮助到其他人。
我们遇到了同样的问题,但正如Vincent在评论中所说,通过将"Microsoft.Net.Compilers.Toolset"添加到引用源生成器项目的分析器的项目的.csproj文件中,问题得到了解决。
    <ItemGroup>
        ...
        <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.8.0">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
    </ItemGroup>

    <ItemGroup>
        <ProjectReference Include="..\DocumentationGenerator\DocumentationGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" TargetFramework="netstandard2.0" />
        ...
    <ItemGroup>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接