简体   繁体   中英

how to remove auto generated proto files from generated folder

I have the below MSBuild to generate .cs files from my proto files. The build works fine until I do a rebuild where it complains of Source file 'generated-proto-output/Trade.cs# specified multiple times.

How do I delete my .cs files before building/rebuilding everytime?

Error

Severity Code Description Project File Line Suppression State Warning CS2002 Source file 'generated-proto-output\\ErrorTrade.cs' specified multiple times MyComp.Trade.Model C:\\dev\\workspaces\\trade-model-workspace\\model\\csharp\\MyComp.Trade.Model

build snippet in csproj file

    <ItemGroup>
        <Protobuf Remove="%(RelativePath)generated-proto-output/**/*.cs" />
        <Protobuf Include="../../proto/**/*.proto" ProtoRoot="../../proto/" OutputDir="%(RelativePath)generated-proto-output/" GrpcServices="None" />
        <Protobuf Update="../../proto/**/*Service.proto" GrpcServices="Both" />
      </ItemGroup>

UPDATE - Complete CSProj file (as requested by Lance)

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

  <PropertyGroup>
    <PackageId>TradeStore.Model</PackageId>
    <ProtoIncludes>.;../../proto</ProtoIncludes>
    <OutputType>Library</OutputType>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Protobuf_NoWarnMissingExpected>true</Protobuf_NoWarnMissingExpected>
  </PropertyGroup>  

  <ItemGroup>
    <PackageReference Include="Google.Protobuf" Version="3.6.1" />    
    <PackageReference Include="Grpc" Version="1.19.0" />
    <PackageReference Include="Grpc.Tools" Version="1.19.0" PrivateAssets="All" />
  </ItemGroup>

  <ItemGroup>
    <FilesToDelete Include="%(RelativePath)generated-proto-output/*.cs" />
  </ItemGroup>

  <Target Name="DeleteSpecificFiles" BeforeTargets="Build">    
    <Message Text="Specific Files: @(FilesToDelete)"/>
    <Message Text ="Beginning to delete specific files before build or rebuild..."/>
    <Delete Files="@(FilesToDelete)"/>
  </Target>

    <ItemGroup>    
      <Protobuf Include="../../proto/**/*.proto" ProtoRoot="../../proto/" OutputDir="%(RelativePath)generated-proto-output/" GrpcServices="None" />
      <Protobuf Update="../../proto/**/*Service.proto" GrpcServices="Both" />
    </ItemGroup>

</Project>

How do I delete my .cs files before building/rebuilding everytime?

Try the following script with BeforeTargets below:

<Project...>
...
  <ItemGroup>
    <FilesToDelete Include="MyPath/*.cs" />
  </ItemGroup>
  <Target Name="DeleteSpecificFiles" BeforeTargets="build">
    <Message Text="Specific Files: @(FilesToDelete)"/>
    <Message Text ="Beginning to delete specific files before build or rebuild..."/>
    <Delete Files="@(FilesToDelete)"/>
  </Target>
</Project>

In addition:

Not seeing the whole content of your .csproj, so I can't figure out why the build snippet you use can't work. But a message task may help output some message whether the engine finds the files by your given path.

In visual studio, if you go Tools=>Options=>Project and Solutions=>Build and Run to change the build out verbosity to Detailed , you will see detailed output message after every build and rebuild. Ctrl+F and type the Target name you will find the details about delete process:

在此输入图像描述

Hope it makes some help for your trouble-shooting.

Try adding CompileOutputs="false" to the directive. This will suppress the warning and won't require you to delete files before building csharp protobuf build integration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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