twitchtv / twirp

A simple RPC framework with protobuf service definitions
https://twitchtv.github.io/twirp/docs/intro.html
Apache License 2.0
7.13k stars 327 forks source link

Question: importing .proto messages #121

Closed pcruz7 closed 6 years ago

pcruz7 commented 6 years ago

Hello,

First of all thank you for this framework and all your work in it!

This issue is only to find out how to go about with the following issue: I have 3 proto files, related in terms of use case (let's call them: Proto A, Proto B and Proto C).

Both Proto A and Proto B make use of a message format in Proto C, so in both files I included the following line:

import "<golang_path>/assets/rpc/C/C.proto";

Which generates this output:

import _ "<golang_path>/assets/rpc/C/C.proto"

the problem is that these proto files are also generated for JS. Which in turn return the following error:

<golang_path>/assets/rpc/C/C.proto: File not found.

I've tried setting the --proto_path parameter and removing the golang_path, but to no avail since the generation of the go import only mirrors the import in the proto file.

Is there a workaround for this?

spenczar commented 6 years ago

Yep, you can use option go_package to set the import path that generated go code should use when referencing your Proto C file.

This is laid out in https://github.com/twitchtv/twirp/blob/master/protoc-gen-twirp/generator.go#L280-L290:

importPath := path.Dir(goFileName(def.File))

The goFileName function will prefer to use the go_package option's value for the import path, and will fall back to using the path to the file on disk (as you found).

The right way to do this is for Proto C to include a line like this:

syntax = "proto3";
package pcruz7.protoc; // or whatever

// replace with whatever import path you want:
option go_package = "github.com/pcruz7/somerepo/protoc"; 

This will at least decouple the import lines in generated .twirp.go and .pb.go files from how you invoke protoc and how you write imports in .proto files, so you can maybe work things into better shape for the JS generator.