【问题标题】:Error generating code during dotnet builddotnet 构建期间生成代码时出错
【发布时间】:2018-09-14 21:54:31
【问题描述】:

我正在尝试使用 dotnet core 2.1 在 dotnet 构建过程中运行 protobuf 的 protoc(这似乎或多或少适用于 2.0,但我们希望利用 2.1 的功能)。 .csproj 文件有一个被盗的 sn-p 来运行它:

  <!-- https://github.com/protocolbuffers/protobuf/issues/3820#issuecomment-399538660 -->
  <PropertyGroup>

    <protoc_linux64>tools\linux_x64\protoc</protoc_linux64>
    <protoc_linux86>tools\linux_x86\protoc</protoc_linux86>
    <protoc_macosx64>tools\macosx_x64\protoc</protoc_macosx64>
    <protoc_macosx86>tools\macosx_x86\protoc</protoc_macosx86>
    <protoc_windows64>tools\windows_x64\protoc.exe</protoc_windows64>
    <protoc_windows86>tools\windows_x86\protoc.exe</protoc_windows86>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(MSBuildRuntimeType)' == 'Core' or&#xD;&#xA;                 ('$(MSBuildVersion)' != '' and&#xD;&#xA;                 $([System.Version]::Parse($(MSBuildVersion))) &gt;= $([System.Version]::Parse(15.3))) ">
    <protoc Condition="'$([MSBuild]::IsOsPlatform(Linux))' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X64'">$(protoc_linux64)</protoc>
    <protoc Condition="'$([MSBuild]::IsOsPlatform(Linux))' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X86'">$(protoc_linux86)</protoc>
    <protoc Condition="'$([MSBuild]::IsOsPlatform(OSX))' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X64'">$(protoc_macosx64)</protoc>
    <protoc Condition="'$([MSBuild]::IsOsPlatform(OSX))' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X86'">$(protoc_macosx86)</protoc>
    <protoc Condition="'$([MSBuild]::IsOsPlatform(Windows))' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X64'">$(protoc_windows64)</protoc>
    <protoc Condition="'$([MSBuild]::IsOsPlatform(Windows))' And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X86'">$(protoc_windows86)</protoc>
  </PropertyGroup>

  <Target Name="BuildProto" BeforeTargets="Compile;Restore">
    <ItemGroup>
      <Protos Include="$(ProjectDir)Protobuf\Protos\*.proto" />
    </ItemGroup>
    <Message Importance="high" Text="protobuffing" />
    <Exec Command="$(NuGetPackageRoot)google.protobuf.tools\3.6.1\$(protoc) --proto_path=$(ProjectDir) --csharp_out=$(ProjectDir)Protobuf\ --csharp_opt=file_extension=.g.cs @(Protos, ' ')" />
  </Target>

当它运行时,它会起作用。生成了.g.cs 文件,一切都很好。但是,如果我从一个干净的构建开始,然后针对解决方案运行构建,则不会生成 .g.cs 文件。之后构建显然会失败,因为无法找到生成的所有内容。

git clean -dxf < /dev/null ; dotnet build MySolution.sln -m:1

当我在输出中搜索 &lt;Message.. 文本(“protobuffing”)时,它不存在。但是,如果我再次运行dotnet build,那么它会在所有“恢复完成”输出之后立即运行“protobuffing”,然后一切正常。 (&lt; /dev/null 的原因是我在 Windows 上运行它,有时 Visual Studio 会在 .vs 目录中锁定一些东西,这在这里应该是无害的,所以它被忽略了。)

我想我可以将 CI 构建服务器设置为运行 dotnet build 两次,但这对我来说似乎是一个 hack。我只是在寻找一种方法,让dotnet build 在 net core 2.1 下运行这个目标,然后再为这个程序集构建 C# 代码。

【问题讨论】:

    标签: .net-core .net-core-2.1


    【解决方案1】:

    我认为问题出在:&lt;Target Name="BuildProto" BeforeTargets="Compile;Restore"&gt;。 您在“恢复”之前使用了目标“BuildProto”,并且第一次运行时不存在原型工具。 我删除了目标“恢复”并执行了构建,没有任何问题。

    【讨论】:

    • 我已经尝试过一百万 (*) 个 beforetargets 和名称的组合......而且我确信我之前尝试过。然而,现在当我尝试你的建议时,它似乎奏效了。始终如一。我将把它推送到我们的构建服务器,如果它仍然在那里工作,我现在希望,我会标记你的答案是正确的。谢谢!
    • TeamCity 似乎还不喜欢这个。我不确定我在我的机器上运行的和 TC 运行的有什么区别。
    • 真的很奇怪的情况...我在 TeamCity 中使用不同的步骤进行恢复、生成、构建,并且效果很好。 1 步 - dotnet restore 2 步 - 使用命令行工具生成 3 步 - dotnet build 也许它会帮助你。
    • 现在这可能是一个不同的问题。如果我使用完整路径而不是仅在当前目录中使用它来指定解决方案,则会失败。如果我指定--configuration Release,而不是它使用的任何默认值,它就会失败。如果我通过将目录更改为 sln 路径但从不同目录运行的 shell 脚本运行命令,则会失败。如果我在 bash 命令行运行“dotnet build MySolution.sln //m:1”,它工作得很好。
    • 我的其余问题已在其他问题中解决,因此鉴于原始问题,我将其标记为正确。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 2023-03-21
    • 2019-01-29
    • 1970-01-01
    相关资源
    最近更新 更多