gogo.proto:找不到档案

时间:2017-03-26 08:02:17

标签: go protocol-buffers

protoc --gogofaster_out=. image.proto

我收到此错误消息

  

/home/abc/src/github.com/gogo/protobuf/gogoproto/gogo.proto:找不到档案。

     

image.proto:Import" /home/abc/src/github.com/gogo/protobuf/gogoproto/gogo.proto"没找到或有错误。

文件绝对存在。我可以从错误报告的路径中找到它。

我的proto文件如下所示:

package image;

import "/home/abc/src/github.com/gogo/protobuf/gogoproto/gogo.proto";

message Frame {
required bool fragment = 1; 
required int32  fragmentID = 2; 
required bool lastFragment = 3; 
required bytes data = 4;
}

我在原型文件所在的目录中有一个Makefile。看起来像这样:

regenerate:
--proto_path=../../github.com/gogo/protobuf/gogoproto:../../github.com/gogo/protobuf/protobuf/google/protobuf:. --gogofaster_out=. *.proto

如何解决路径错误?

2 个答案:

答案 0 :(得分:1)

作为in this issue,您可以尝试仅导入相对路径:

import "github.com/gogo/protobuf/gogoproto/gogo.proto";

确保GOPATH首先设置为/home/abc

我提到protobuf PR 241,其中makefile定义了要使用的PATH:

regenerate: 
  protoc-min-version --version="3.0.0" 
    --proto_path=$(GOPATH)/src/image:
                 $(GOPATH)/src/github.com/go‌​go/protobuf/protobuf‌​:
                 $(GOPATH)/src/githu‌​b.com/gogo/protobuf/‌​protobuf/google/prot‌​obuf:
                 . --gogo_out=. *.proto 

OP提到this thread,指出:

  

依赖项会调用“google/protobuf/”中的其他依赖项,因此这些依赖项也必须正确地存在于路径中。
  这些错误非常具有误导性

     

使用gogoproto扩展程序时,您应该使用gogofast_out   gofast_out仅适用于您未使用任何扩展程序但需要一些额外速度的情况   gogofast_out / gofast_out没有副作用   gogofaster_out生成大多数字段nullable=false

答案 1 :(得分:0)

  1. 运行 go get -u github.com/gogo/protobuf 将依赖项下载到本地目录 $GOPATH/pkg/mod
  2. 运行 go mod vendor 来打包您的模块依赖项。执行这个命令后,你的项目中应该有一个目录“vendor”,并在Makefile中引用。

Makefile 命令应该是:

regenerate: 
    protoc-min-version --version="3.0.0" \
    --proto_path=$(GOPATH)/src/image \
    -I vendor/github.com/go‌​go/protobuf/protobuf‌​ \
    -I vendor/githu‌​b.com/gogo/protobuf/‌​protobuf/google/prot‌​obuf \
    -I. --gogo_out=. *.proto 

这里,--pro_path 和 -I 是一样的。
如果绝对路径包含变量,则模式应为:

-I=${path_variable}/a/sample \
-I=${path_variable}/b/sample \

希望这可以帮助某人!