Search code examples
visual-studio-2010protocol-bufferscustom-build-step

Visual Studio 2010 and protobuf-csharp-port


We're using Jon Skeet's proto-csharp-port, and I'm running into some difficulties when mixing it with ReSharper in Visual Studio 2010.

We generate the .cs files via a custom MSBuild target, hooked up as follows:

<Target Name="BeforeBuild" DependsOnTargets="CompileProtos" />

The CompileProtos target runs ProtoGen and then adds the generated .cs files to the @(Compile) item group, by using CreateItem. This looks in a defined directory and compiles every .proto file it finds, so they're not listed in the project.

Where it falls down is that ReSharper doesn't recognise the content of the .cs files (because they're not in the project and might not exist yet), so I can't get the solution analysis light to go green.

If I add the .cs files to the project, then I get a build failure, because the .cs file has been added to the Compile item group twice.

I know that Marc's protobuf-net has Visual Studio 2008 goodness in it, and I'm looking for something similar, but for Jon's protobuf-csharp-port and for Visual Studio 2010.

Ideally, I'd like to be able to add the .proto files to the project, have them built correctly, and have Visual Studio and ReSharper know about the generated .cs files, so that IntelliSense and solution analysis work properly.

I'm guessing that something like how .xsd files can implicitly generate .cs files would do the trick.


Solution

  • I solved it by removing the CreateItem from the CompileProtos target, and by defining it as a proper ItemGroup:

    <ItemGroup>
      <Protocols Include="$(ProtocolsPath)\*.proto"/>
    </ItemGroup>
    <ItemGroup>
      <Compile Include="@(Protocols -> '%(Filename).cs')"/>
    </ItemGroup>
    

    This means that Visual Studio (and ReSharper) pick up the .cs files correctly, once they've been built, and ReSharper's full solution analysis stops complaining.

    Unfortunately, Visual Studio has a habit of expanding the ItemGroup into individual Compile entries, but I can check for that before checking anything in.