在 dotnet 构建期间生成代码时出错
Error generating code during dotnet build
我正在尝试 运行 在使用 dotnet 核心 2.1 的 dotnet 构建过程中使用 protobuf 的协议(这似乎或多或少适用于 2.0,但我们想利用 2.1 的功能)。 .csproj
文件有一个被盗的片段 运行 这个:
<!-- 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
 ('$(MSBuildVersion)' != '' and
 $([System.Version]::Parse($(MSBuildVersion))) >= $([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.6.1$(protoc) --proto_path=$(ProjectDir) --csharp_out=$(ProjectDir)Protobuf\ --csharp_opt=file_extension=.g.cs @(Protos, ' ')" />
</Target>
当这个 运行s 时,它起作用了。 .g.cs
文件已生成,一切都很好。但是,如果我从一个干净的构建开始,并且 运行 针对解决方案构建,则不会生成 .g.cs
文件。之后构建显然失败了,因为无法找到生成的所有内容。
git clean -dxf < /dev/null ; dotnet build MySolution.sln -m:1
当我在输出中搜索 <Message..
文本 ("protobuffing") 时,它不存在。但是,如果我第二次 运行 dotnet build
,那么在所有 "Restore completed" 输出之后 运行 就是 "protobuffing",然后一切正常。 (< /dev/null
的原因是我在 Windows 上 运行 宁此,有时 Visual Studio 有东西锁定在 .vs 目录中,这在这里应该是无害的,所以它被忽略了。)
我想我可以将 CI 构建服务器设置为 运行 dotnet build 两次,但这对我来说似乎是一个 hack。在为此程序集构建 C# 代码之前,我只是在寻找一种方法让 dotnet build
在 net core 2.1 下达到 运行 这个目标。
我认为问题出在这里:<Target Name="BuildProto" BeforeTargets="Compile;Restore">
。
您在 'Restore' 之前使用了目标 'BuildProto',并且最初 运行 不存在原型工具。
我删除了目标 'Restore' 并且构建没有任何问题地执行。
我正在尝试 运行 在使用 dotnet 核心 2.1 的 dotnet 构建过程中使用 protobuf 的协议(这似乎或多或少适用于 2.0,但我们想利用 2.1 的功能)。 .csproj
文件有一个被盗的片段 运行 这个:
<!-- 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
 ('$(MSBuildVersion)' != '' and
 $([System.Version]::Parse($(MSBuildVersion))) >= $([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.6.1$(protoc) --proto_path=$(ProjectDir) --csharp_out=$(ProjectDir)Protobuf\ --csharp_opt=file_extension=.g.cs @(Protos, ' ')" />
</Target>
当这个 运行s 时,它起作用了。 .g.cs
文件已生成,一切都很好。但是,如果我从一个干净的构建开始,并且 运行 针对解决方案构建,则不会生成 .g.cs
文件。之后构建显然失败了,因为无法找到生成的所有内容。
git clean -dxf < /dev/null ; dotnet build MySolution.sln -m:1
当我在输出中搜索 <Message..
文本 ("protobuffing") 时,它不存在。但是,如果我第二次 运行 dotnet build
,那么在所有 "Restore completed" 输出之后 运行 就是 "protobuffing",然后一切正常。 (< /dev/null
的原因是我在 Windows 上 运行 宁此,有时 Visual Studio 有东西锁定在 .vs 目录中,这在这里应该是无害的,所以它被忽略了。)
我想我可以将 CI 构建服务器设置为 运行 dotnet build 两次,但这对我来说似乎是一个 hack。在为此程序集构建 C# 代码之前,我只是在寻找一种方法让 dotnet build
在 net core 2.1 下达到 运行 这个目标。
我认为问题出在这里:<Target Name="BuildProto" BeforeTargets="Compile;Restore">
。
您在 'Restore' 之前使用了目标 'BuildProto',并且最初 运行 不存在原型工具。
我删除了目标 'Restore' 并且构建没有任何问题地执行。