Depending on google/api/http.proto

2,413 views
Skip to first unread message

Peter McAlpine

unread,
Feb 1, 2017, 11:57:04 AM2/1/17
to bazel-discuss
Hello Bazel Discuss,

I am writing a simple golang service that implements an API described in a proto file. I would like to:
import "google/api/annotations.proto"; // (source: https://github.com/googleapis/googleapis)

This depends on:
  • google/api/http.proto, and
  • google/protobuf/descriptor.proto
I am using rules_go/proto:go_proto_library.bzl to do this.

How can I do this? I have tried a variety of things, including:
  1. depending on the already generated https://github.com/google/go-genproto and https://github.com/golang/protobuf. Fails because of missing go_default_library_protos filegroups. I believe this is because the generated go code does not have the proto files living beside them (as they do for the "well-known" ptypes/* protos).
  2. depending on the source of the protos themselves (https://github.com/googleapis/googleapis and https://github.com/google/protobuf). I gave up on this because I don't want to own writing BUILD files for these large third party projects, but I suspect the issues would be the same as in:
  3. copying the protos into my project and attempting to build them locally. This fails with errors because outputs 'google/api/annotations.pb.go' and "google/api/http.pb.go' were not created.
  4. Importing the code into my project as git submodules, which I stopped working on when I realied I'd need to modify the protos I depend on.
Has anyone successfully dependended on google/api/http.proto?

-Peter

Peter McAlpine

unread,
Feb 1, 2017, 8:42:39 PM2/1/17
to bazel-discuss
FWIW, I've found a way to do this, although I'd appreciate hearing if there's a cleaner way.

-Peter

# BUILD
load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_library")

go_proto_library(
    name = "go_default_library",
    importmap = {
        "google/api/annotations.proto": "github.com/google/go-genproto/googleapis/api/annotations",
    },
    imports = [
        "external/com_github_google_protobuf/src",
        "external/com_github_googleapis_googleapis",
    ],
    inputs = [
        "@com_github_google_protobuf//:well_known_protos",
        "@com_github_googleapis_googleapis//:well_known_protos",
    ],
    protos = ["my.proto"],
    visibility = ["//visibility:public"],
    with_grpc = True,
    deps = [
        "@org_golang_google_genproto//googleapis/api/annotations:go_default_library",
    ],
)

# WORKSPACE
git_repository(
    name = "io_bazel_rules_go",
    commit = "7828452850597b52b49ec603b23f8ad2bcb22aed",
)

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

go_repositories()

git_repository(
    name = "org_pubref_rules_protobuf",
    tag = "v0.7.1",
)

load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_repositories")

go_proto_repositories()

new_go_repository(
    name = "com_github_golang_glog",
    commit = "23def4e6c14b4da8ac2ed8007337bc5eb5007998",
    importpath = "github.com/golang/glog",
)

new_go_repository(
    name = "com_github_golang_protobuf",
    commit = "8ee79997227bf9b34611aee7946ae64735e6fd93",
    importpath = "github.com/golang/protobuf",
)

new_go_repository(
    name = "org_golang_google_genproto",
    commit = "b3e7c2fb04031add52c4817f53f43757ccbf9c18",
    importpath = "github.com/google/go-genproto",
)

new_go_repository(
    name = "org_golang_google_grpc",
    importpath = "google.golang.org/grpc",
    tag = "v1.0.4",
)

GOOGLEAPIS_BUILD_FILE = """
package(default_visibility = ["//visibility:public"])

WELL_KNOWN_PROTOS = [
    "google/api/http.proto",
    "google/api/annotations.proto",
]

filegroup(
    name = "well_known_protos",
    srcs = WELL_KNOWN_PROTOS,
    visibility = ["//visibility:public"],
)
"""

new_git_repository(
    name = "com_github_googleapis_googleapis",
    build_file_content = GOOGLEAPIS_BUILD_FILE,
    commit = "13ac2436c5e3d568bd0e938f6ed58b77a48aba15",
)


--
You received this message because you are subscribed to a topic in the Google Groups "bazel-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bazel-discuss/vA504VqSiCw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/47a5bab7-d071-4ad5-83e9-6480409dbb2a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Johnston

unread,
Feb 3, 2017, 9:52:37 PM2/3/17
to bazel-discuss
Nice.  A possibly cleaner solution is to encapsulate this configuration in a proto_language rule, making it more reusable.  Pass the language definition label into the go_proto_library rule, overriding the default configuration.

# BUILD
load("@org_pubref_rules_protobuf//protobuf:rules.bzl", "proto_language")
load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_library")

proto_language(
    name = "go-google-api",
    pb_plugin_name = "go",
    pb_file_extensions = [".pb.go"],
    pb_plugin = "@com_github_golang_protobuf//protoc-gen-go",
    pb_plugin_implements_grpc = True,
    prefix = "//:go_prefix",
    importmap = {
        "google/api/annotations.proto": "github.com/google/go-genproto/googleapis/api/annotations",
    },
    pb_inputs = [
        "@com_github_google_protobuf//:well_known_protos",
        "@com_github_googleapis_googleapis//:well_known_protos",
    ],
    pb_imports = [
        "external/com_github_google_protobuf/src",
        "external/com_github_googleapis_googleapis",
    ],
)

go_proto_library(
    name = "api",
    langs = [":go-google-api"],
    protos = ["api.proto"],
    deps = ["@com_github_google_go_genproto//googleapis/api/annotations:go_default_library"],
)


Probably would be good to put this type of configuration in the repo so other can use consume the google apis without having to figure this out again.

Thanks,
Paul
To unsubscribe from this group and all its topics, send an email to bazel-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages