0

I am trying to import a Proto file into another one from a different folder and haven't been successful in doing so. Here's the scenario:

I have a .Proto in folder ....\abc\protos\ProtoA.proto and another one in folder ....\def\protos\ProtoB.proto.

I need ProtoA to import ProtoB but it's in a different folder and using Import "....\def\protos\ProtoB.proto" doesn't work because it doesn't like "...." in the path.

What are the steps i need to follow to import the file in correctly from a different path?

Amy
  • 185
  • 15
  • Does [this answer](https://stackoverflow.com/a/53128417/11810946) your question? You can either set `proto_path` to the common base folder or specify `proto_path` multiple times. – Brits Oct 21 '21 at 20:29

1 Answers1

1

It's confusing and I'm unsure whether it's effectively explained in the docs.

Proto file imports are absolute to the proto package and the package structure must be preserved in the filing system structure.

However, the absolute disk location is only important when using protoc per @Brits comment so that the compiler can find the protos.

So.... Your import for ....\def\protos\ProtoB.proto should reflect the specific package and service or method or message name that you're importing not its disk location (which is what you're using).

Then, when you protoc, you should --proto_path and give (I think absolute not relative) paths to the filing system locations that contain the protos needed to be imported.

Have a look at Any by way of example.

In a proto, you import "google.protobuf.Any", it's package plus the message name.

When you protoc it, Any is often already in the include path but, if it weren't, you'd need to --proto_path=/path/to/foo if foo is the root directory containing google/protobuf/any.proto; the proto file must be in a directory called protobuf in a directory called google for the import to work.

If you're familiar with Golang and GOPATH, this mirrors how Go packages are named by their directory (not file) name and referenced locally by their location being in the GOPATH; it's now different with Go Modules.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • So, I'm doing all this in C# project. Isn't protoc specifically for Java/C++? I have a proto file stored in my solution and it's being referenced in a .NET Standard project. The other proto file that I am bringing in is stored in a relative path elsewhere and doing this does not help: syntax = "proto3"; import "..\..\Document Managemet\Protos\PrintTargetType.proto"; import "google/protobuf/timestamp.proto"; import "google/protobuf/wrappers.proto"; option csharp_namespace = "Instec.Services.Forms.Messages.Grpc"; package instec.services.formsmessages; – Amy Oct 22 '21 at 12:45
  • Some other language implementations do implement `protoc` directly. IIRC C# does this too. Unfortunately, I don't use Visual Studio (?) and so am unable to help resolve configuration issues. I think there must be somewhere in the project configuration that you can enumerate the directories contain the packages that contain your protobufs. Have a look at this question: https://stackoverflow.com/questions/59422186/importing-proto-files-from-another-project – DazWilkin Oct 22 '21 at 15:20