0

Importing proto files from google such as Struct is pretty much straightforward as shown below:

syntax = "proto3";
package messages;

import "google/protobuf/struct.proto";

message UnaryRequest{
    google.protobuf.Struct data = 1;
}

I would like to replicate same flow with my team such that instead of import "google/protobuf/struct.proto" we will have:

syntax = "proto3";
package messages;

import "myorg/protobuf/unary.proto"; //Notice difference here

message UnaryRequest{
    myorg.protobuf.UnaryData data = 1; //Notice difference here
}

Where import "myorg/protobuf/unary.proto" is expected to be retrieved from my orgs utility npm package which is reusable across internal microservices.

How can this be done?

Any ideas would be really appreciated.

ololo
  • 1,326
  • 2
  • 14
  • 47

1 Answers1

1

I don't think there's an automatic way for protoc to pull the npm package for you, but if you know where your orgs protos are gonna be after you pull them with npm, then you can include that path via --proto_path <path> option on protoc. The import ... statements will then resolve relative to all paths specified with that flag.

Not sure what build system you're using, but I bet you can automate this set up in most of them.

jeanluc
  • 1,608
  • 1
  • 14
  • 28
  • This is correct. `protoc` doesn't have a mechanism to pull imports. Imports must be locally installed. `protoc` doesn't pull Google's Well-Known Types, they're bundled with it and this is how it finds them. The only solution is to ensure that any imports are pulled before running `protoc` generally done by a build system. – DazWilkin Nov 03 '22 at 22:17