title: Understanding protobuf import and output relative paths
body: <p>I am fairly certain this is operator error and I am at the point I am not thinking clearly.</p>
  
  <p>Here is the setup:</p>
  
  <pre><code>$GOPATH/src/github.com/&lt;company&gt;/&lt;service a&gt;/proto/a.proto
  $GOPATH/src/github.com/&lt;company&gt;/&lt;service b&gt;/proto/b.proto
  </code></pre>
  
  <p>etc.</p>
  
  <p>Now in the proto file I am using imports similar to go (perhaps the issue) such that <code>a.proto</code> has:
  <code>import "github.com/&lt;company&gt;/&lt;service b&gt;/b.proto"</code></p>
  
  <p>I have possibly two separate issues.</p>
  
  <ol>
  <li>I cannot get the import to compile properly using <code>go:generate protoc</code></li>
  <li>I cannot get the output <code>a.pb.go</code> file to be placed in the <code>$GOPATH/src/github.com/&lt;company&gt;/&lt;service a&gt;/proto/</code> path.</li>
  </ol>
  
  <p>I have attempted multiple configurations probably not in the correct combination.
  <code>Using option go_package = "github.com/&lt;company&gt;/&lt;service b&gt;/proto"</code> in each <code>.proto</code> file</p>
  
  <p>Multiple variations of go generate;</p>
  
  <pre><code>go:generate protoc --proto_path=.:$GOPATH/src --go_out=$GOPATH/src a.proto
  go:generate protoc --proto_path=.:$GOPATH/src --go_out=. a.proto 
  go:generate protoc --go_out=import_prefix=github.com/&lt;company&gt;/:. api.proto
  </code></pre>
  
  <p>I clearly have a poor understanding on how protoc looks at import paths and file outputs. Anyone point me in the direction of what I am doing wrong?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Update #1</strong>
  In a.proto;</p>
  
  <pre><code>option go_package = "github.com/&lt;company&gt;/&lt;service a&gt;/proto";
  
  import "github.com/&lt;company&gt;/&lt;service b&gt;/proto/b.proto";
  </code></pre>
  
  <p>and the go generate;</p>
  
  <pre><code>//go:generate protoc --proto_path=$GOPATH/src --go_out=$GOPATH/src/github.com/&lt;company&gt;/&lt;service a&gt;/proto a.proto
  </code></pre>
  
  <p>Which is called from a go file in the proto directory with the a.proto.</p>
  
  <p>I received the error;
  a.proto: File does not reside within any path specified using --proto_path (or -I).  You must specify a --proto_path ch encompasses this file.  Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).</p>
  
  <p>I have confirmed $GOPATH is to the expected location. </p>
  
  <p><strong>Solution</strong></p>
  
  <p>Thanks to Shivam Jindal for pointing me in the correct direction. The import is exactly as described in his solution. The output was a problem of my misusing both --go_out and option go_package. I used the go_package to specify the output location and --go_out to specify the root similar to --proto_path. Now everything works.</p>
  
  <pre><code>option go_package = "github.com/&lt;company&gt;/&lt;service a&gt;/proto";
  </code></pre>
  
  <p>and</p>
  
  <pre><code>//go:generate protoc --proto_path=$GOPATH/src/ --go_out=$GOPATH/src/ $GOPATH/src/github.com/&lt;company&gt;/&lt;service a&gt;/proto/a.proto
  </code></pre>
  
  <p>Thanks!</p>