Protobuf: problem with namespace

Created on 29 Mar 2016  路  6Comments  路  Source: golang/protobuf

Protobuf in go does not support case, when we have more than one file in the same namespace. protoc won't send any message, but generated code cannot be compiled. Some code in C++ works.
// file A.proto:
syntax = "proto3";
message A {};

// file B.proto:
syntax = "proto3";
import "A.proto";
message B {
A a = 1;
};

protoc -I=$SRC_DIR --go_out=$DST_DIR $SRC_DIR/B.proto
(cd $DST_DIR; go build)
can't load package: import cycle not allowed
package .
imports .
import cycle not allowed
package .
imports .

Message is cause by line: import A "."
but delete the line also does not solve the issue - type is always callad as A.A

Most helpful comment

I also tried give both files at once - but although it is recommended:
protoc -I=$SRC_DIR --go_out=$DST_DIR $SRC_DIR/A.proto $SRC_DIR/B.proto
2016/03/29 20:16:32 protoc-gen-go: error:inconsistent package names: B A
--go_out: protoc-gen-go: Plugin failed with status code 1.

All 6 comments

The golang protobuf plugin requires that you pass all proto files in a single go package together on the commandline. Your problem is that you're translating A.proto and B.proto separately.

The plugin assumes that everything passed together is in the same package.

At Square, we're using a wrapper to automatically call protoc with one package at a time: https://github.com/square/goprotowrap

I also tried give both files at once - but although it is recommended:
protoc -I=$SRC_DIR --go_out=$DST_DIR $SRC_DIR/A.proto $SRC_DIR/B.proto
2016/03/29 20:16:32 protoc-gen-go: error:inconsistent package names: B A
--go_out: protoc-gen-go: Plugin failed with status code 1.

It's trying to infer package names for your .proto files, and failing since (I'm guessing) neither declare package names. https://github.com/golang/protobuf/blob/master/protoc-gen-go/generator/generator.go#L734

You could put go_package declarations in your proto files, or use the import_path parameter. I'd recommend the former.

Nice .. thanks a lot. You've solve that. I didn't understand what was the problem - each go file needs a package (in contrast with C++) and when no package/go_package is used it tries to derive package name from file. Great

Also see #39

My Common file is

syntax = "proto3";
package proto;
option go_package = "github.com/stevenkitter/skills/common/proto";

message Resp {
    string message = 1;
    string data = 2;
    int32 code = 3;
}

Sms file is

syntax = "proto3";
package proto;
option go_package = "github.com/stevenkitter/skills/services/sms/proto";
import "common/proto/common.proto";

message SendEmailVerifyReq {
    string email = 1;
}

service SMS {
    rpc SendEmailVerify(SendEmailVerifyReq) returns (proto.Resp);
}

My sh file is

#!/usr/bin/env bash
COMMON_PATH=$PWD/common/proto
protoc -I${COMMON_PATH} ${COMMON_PATH}/*.proto --go_out=plugins=grpc:${GOPATH}/src
SERVER_PATH=$PWD/services
protoc -I${PWD} ${SERVER_PATH}/account/proto/*.proto --go_out=plugins=grpc:${GOPATH}/src
protoc -I${PWD} ${SERVER_PATH}/sms/proto/*.proto --go_out=plugins=grpc:${GOPATH}/src

just generate the files separated. But search path need to ${PWD}

Was this page helpful?
0 / 5 - 0 ratings