views:

212

answers:

2

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.

A: 

I've attempted to get this working by implementing a custom tool for code generation, but I've run into a seemingly insurmountable hurdle:

protoc takes a directory full of .proto files and generates a .protobin file. This is then fed to ProtoGen which spits out a .cs file for each protocol definition. Unfortunately, it appears that the .protobin file needs to contain all of the definitions, otherwise you get Error: Unable to resolve all dependencies.

Since the custom tool model in Visual Studio assumes a single input file and a single output file (i.e. foo.proto -> foo.cs), it doesn't look like this can be made to work.

At least, not without finding some way to include all of foo.proto's imported .proto files in foo.protobin, anyway.

Roger Lipscombe
A: 

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.

Roger Lipscombe

related questions