1
votes

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>
2
Hi,any update? If this issue persists, could you please share a csproj file or simple sample with similar issue here.?LoLance
Hi @LanceLi-MSFT, the suggested solution did not work. I am not able to provide the whole project but have provided the complete csproj file. Please advise what could be the issue that deletion isnt working as expected.serah
I also replaced the relative path syntax with the complete path to the folder, that also did not help with deleting the file.I see the messages getting printed in the console logs but that is about it, no file deletion takes place.serah
Even the complete path can't work? Please update your vs version to latest one, and in complete path use the '\' instead of '/'.LoLance
If you get some message to show the specified files in the log, then it's strange that you can't find the delete task(in target), make sure you run it within VS IDE. And what's your vs version, I'm not sure if the delete task is supported in your msbuild version.LoLance

2 Answers

1
votes

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

0
votes

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+Fand type the Target name you will find the details about delete process:

enter image description here

Hope it makes some help for your trouble-shooting.