4

I'm using Google.Protobuf.Tools in a C# .NET Standard project MyProject. I have multiple proto definitions.

MyProject/Protos/Requests/SampleRequest.proto

syntax = "proto3";

option csharp_namespace = "My.Projects.Protos.Requests";


package sampleRequests;

message SampleRequest {
   int32 requestNumber =1;
   SubRequest request =2;
}


MyProject/Protos/Requests/SubRequest.proto

syntax = "proto3";

option csharp_namespace = "My.Projects.Protos.Requests";

package subRequests;

message SubRequest {
   int32 SubId =1;
   string requestText =2;
}


Now, this code would not compile and the error shown is SubRequest is unknown.

I would like to know how to import a different proto file so that I can achieve this. I don't really understand --proto_path cli options and could not find any clear documentation around this.

Any help is much appreciated. Thanks in advance. Thanks in advance.

Jins Peter
  • 2,368
  • 1
  • 18
  • 37

1 Answers1

2

When evaluation an import, it is tested againsts each --proto_path to see if a suitable match can be found. For example, you could have the files side by side in the Requests folder, use --proto_path to give the location of the Protos folder, and talk relative to there inside the proto:

import "Requests/SubRequest.proto";

Note also that you need to think about packages; they aren't in the same package, so you can qualify it explicitly by starting with a .:

syntax = "proto3";
import "Requests/SubRequest.proto";

option csharp_namespace = "My.Projects.Protos.Requests";


package sampleRequests;

message SampleRequest {
   int32 requestNumber =1;
   .subRequests.SubRequest request =2;
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Is the `--proto_path` thing something I need to do or is it something the compiler does? – Jins Peter Sep 07 '20 at 16:23
  • Thanks for the answer. If the above is something I need to do when handling a C# project using proto please direct me to some kind of documentation – Jins Peter Sep 07 '20 at 16:26
  • @JinsPeter which compiler? `--proto_path` is literally an argument to the Google protobuf schema compiler, (aka "protoc") so if you mean that: yes. If you're using protobuf-net: it: the optional command line tools ("protogen") mimic "protoc". The C# compiler has no interest in .proto files whatsoever. You might be talking about the build add-in? if so, you might be looking for https://github.com/grpc/grpc/blob/master/src/csharp/BUILD-INTEGRATION.md ? – Marc Gravell Sep 08 '20 at 06:39