Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to build protobufs with gogoproto ? #1364

Closed
hawkingrei opened this issue Mar 5, 2018 · 11 comments
Closed

how to build protobufs with gogoproto ? #1364

hawkingrei opened this issue Mar 5, 2018 · 11 comments
Labels

Comments

@hawkingrei
Copy link
Contributor

please update the document about Protobuf rules , I cannot know how to configure the bazel to build protobufs with gogoproto. Hope to solve as soon as possible.

@hawkingrei
Copy link
Contributor Author

hawkingrei commented Mar 5, 2018

Now, i meet this error

ERROR: /root/.jenkins/workspace/go-common/net/http/blademaster/render/BUILD.bazel:4:1: Generating Descriptor Set proto_library //net/http/blademaster/render:render_proto failed (Exit 1) ERROR: /Users/bilibili/go/src/go-common/net/http/blademaster/render/BUILD.bazel:4:1: Generating Descriptor Set proto_library //net/http/blademaster/render:render_proto failed (Exit 1) google/protobuf/any.proto: File not found. net/http/blademaster/render/pb.proto: Import "google/protobuf/any.proto" was not found or had errors. net/http/blademaster/render/pb.proto:12:9: "google.protobuf.Any" is not defined.

use under command to generate golang code

protoc --proto_path=.:$GOPATH/src/github.com/gogo/protobuf --gogo_out=Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types:. *.proto

how to configure it in the go_proto_compiler ?

@jayconrod
Copy link
Contributor

Sorry for the lack of proto documentation. I'm hoping to get to that this week.

In the mean time, here's a quick example:

hello.proto

syntax = "proto3";

import "google/protobuf/any.proto";

message Foo {
};

BUILD.bazel

load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

proto_library(
    name = "hello_proto",
    srcs = ["hello.proto"],
    visibility = ["//visibility:public"],
    deps = ["@com_google_protobuf//:any_proto"],
)

go_proto_library(
    name = "hello_go_proto",
    compilers = ["@io_bazel_rules_go//proto:gogo_proto"],
    importpath = "example.com/repo",
    proto = ":hello_proto",
    visibility = ["//visibility:public"],
)

Some notes:

  • proto_library is built into Bazel. Building it produces a descriptor binary, but you can't depend on it directly from language-specific rules like go_library.
  • proto_library does not know about Well Known Types, so you need to depend on them explicitly.
  • go_proto_library references a proto_library via the proto attribute. You need also need to reference other go_proto_library rules for each proto_library your proto_library depends on. You don't need to explicitly depend on Well Known Types though; that happens automatically.
  • You can use gogo or other plugins by setting the compilers attribute of go_proto_library. See proto/BUILD.bazel for the plugins we support directly.

@hawkingrei
Copy link
Contributor Author

hawkingrei commented Mar 6, 2018

@jayconrod I have solved the problem by your help. Thank you very much.

@jayconrod
Copy link
Contributor

Glad to hear it. I'll close this issue now. The tracking issue for documenting go_proto_library is #1282.

@hawkingrei
Copy link
Contributor Author

sorry, i meet new problem. I need to put gogo/protobuf into vendor. a proto in my project use gogo/protobuf/gogoproto/gogo.proto

so i write BUILD as follow:

proto_library(
    name = "identify_proto",
    srcs = ["accessRes.proto"],
    visibility = ["//visibility:public"],
    deps = [
        "//vendor/github.com/gogo/protobuf/gogoproto:gogoproto_proto",
    ],
)

go_proto_library(
    name = "identify_go_proto",
    compilers = ["@io_bazel_rules_go//proto:gogo_proto"],
    importpath = "go-common/business/client/identify",
    proto = ":identify_proto",
    visibility = ["//visibility:public"],
    deps = [
        "//vendor/github.com/gogo/protobuf/gogoproto:go_default_library",
        "//vendor/github.com/gogo/protobuf/proto:go_default_library",
        "//vendor/github.com/gogo/protobuf/protoc-gen-gogo/descriptor:go_default_library",
        "//vendor/github.com/gogo/protobuf/sortkeys:go_default_library",
        "//vendor/github.com/gogo/protobuf/types:go_default_library",
        "//vendor/github.com/golang/protobuf/ptypes/any:go_default_library",
    ],
)

and /vendor/github.com/gogo/protobuf/gogoproto/BUILD as follow

load("@io_bazel_rules_go//go:def.bzl", "go_library")

filegroup(
    name = "go_default_library_protos",
    srcs = ["gogo.proto"],
    visibility = ["//visibility:public"],
)

go_library(
    name = "go_default_library",
    srcs = [
        "doc.go",
        "gogo.pb.go",
        "helper.go",
    ],
    importpath = "github.com/gogo/protobuf/gogoproto",
    visibility = ["//visibility:public"],
    deps = [
        "//vendor/github.com/gogo/protobuf/proto:go_default_library",
        "//vendor/github.com/gogo/protobuf/protoc-gen-gogo/descriptor:go_default_library",
    ],
)

proto_library(
    name = "gogoproto_proto",
    srcs = ["gogo.proto"],
    visibility = ["//visibility:public"],
    deps = ["@com_google_protobuf//:descriptor_proto"],
)

filegroup(
    name = "package-srcs",
    srcs = glob(["**"]),
    tags = ["automanaged"],
    visibility = ["//visibility:public"],
)

filegroup(
    name = "all-srcs",
    srcs = [":package-srcs"],
    tags = ["automanaged"],
    visibility = ["//visibility:public"],
)

but it meet a error as follow

ERROR: /Users/bilibili/go/src/go-common/business/client/identify/BUILD:18:1: Generating Descriptor Set proto_library //business/client/identify:identify_proto failed (Exit 1)
github.com/gogo/protobuf/gogoproto/gogo.proto: File not found.
business/client/identify/accessRes.proto: Import "github.com/gogo/protobuf/gogoproto/gogo.proto" was not found or had errors.

Have i configured gogoproto_proto?

@jayconrod
Copy link
Contributor

There are a couple problems with vendoring protos.

First, Bazel requires that protos are imported with paths relative to the root of a repository. This means you would have to import "vendor/github.com/gogo/protobuf/gogoproto/gogo.proto". Because of this, vendored protos can't import other vendored protos without modification. Bazel folks are working on adding an attribute to proto_library that would let you set the source root (tracking issue bazelbuild/bazel#4544), but that missed the March release. Hopefully it will be in the April release.

Second, go_proto_compiler automatically adds dependencies on go_proto_library rules for the Well Known Types. The gogo_proto rule adds some other additional dependencies. So you may see conflicts when you add these dependencies yourself explicitly.

@jayconrod jayconrod reopened this Mar 7, 2018
@hawkingrei
Copy link
Contributor Author

i have another question.

i have some code as follow

subject.go

type stime int64

dm.proto

message DM {
	int64 Ctime = 9 [(gogoproto.jsontag) = "ctime", (gogoproto.casttype) = "stime"];
	int64 Mtime = 10 [(gogoproto.jsontag) = "mtime", (gogoproto.casttype) = "stime"];
}

BUILD

load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
    name = "go_default_library",
    srcs = [
        "action.go",
        "dm.go",
        "dm_seg.go",
        "subject.go",
        "transfer.go",
    ],
    embed = [":model_go_proto"],
    importpath = "go-common/business/job/dm2/model",
    visibility = ["//visibility:public"],
)

filegroup(
    name = "package-srcs",
    srcs = glob(["**"]),
    tags = ["automanaged"],
    visibility = ["//visibility:private"],
)

filegroup(
    name = "all-srcs",
    srcs = [":package-srcs"],
    tags = ["automanaged"],
    visibility = ["//visibility:public"],
)

proto_library(
    name = "model_proto",
    srcs = ["dm.proto"],
    visibility = ["//visibility:public"],
    deps = [
        "@gogo_special_proto//github.com/gogo/protobuf/gogoproto",
    ],
)

go_proto_library(
    name = "model_go_proto",
    compilers = ["@io_bazel_rules_go//proto:gogo_proto"],
    importpath = "go-common/business/job/dm2/model",
    proto = ":model_proto",
    visibility = ["//visibility:public"],
    deps = ["@com_github_gogo_protobuf//gogoproto:go_default_library","//time:go_default_library",],
)

it will meet a error

dm.pb.go:46: undefined: stime
dm.pb.go:47: undefined: stime

@jayconrod
Copy link
Contributor

What target hits the error exactly? model_go_proto won't be buildable on its own if it needs extra files. go_default_library should be buildable though. You can add tags = ["manual"] to model_go_proto to prevent Bazel from building it when a wildcard pattern is given, e.g., bazel build //....

@aleimu
Copy link

aleimu commented Apr 24, 2019

我是搜go-common/net/http/blademaster找到这里的,路过打扰了....

@prestonvanloon
Copy link
Contributor

I also struggled with this recently. The gogo proto is not intuitively located in @com_github_gogo_protobuf//. Instead it is located at @gogo_special_proto//github.com/gogo/protobuf/gogoproto.

@joesonw
Copy link

joesonw commented Nov 11, 2019

For those who are still struggling. Here is one simple trick

# gazelle:resolve proto github.com/gogo/protobuf/gogoproto/gogo.proto @gogo_special_proto//github.com/gogo/protobuf/gogoproto
# gazelle:resolve proto go github.com/gogo/protobuf/gogoproto/gogo.proto @com_github_gogo_protobuf//gogoproto:go_default_library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants