Rules_go: custom new_http_archive for googleapis common protos doesn't work

Created on 25 Jan 2018  路  3Comments  路  Source: bazelbuild/rules_go

I'm using the following method to build my protos that depend on annotations.proto:

WORKSPACE

new_http_archive(
    name = "googleapi",
    url = "https://github.com/googleapis/googleapis/archive/common-protos-1_3_1.zip",
    strip_prefix = "googleapis-common-protos-1_3_1/",
    build_file="BUILD.googleapi"
)

BUILD.googleapi

package(default_visibility=['//visibility:public'])

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

proto_library(
    name = 'http_proto',
    srcs = ['google/api/http.proto']
)

proto_library(
    name = 'annotations_proto',
    srcs = ['google/api/annotations.proto'],
    deps = [
            ":http_proto",
            "@com_google_protobuf//:descriptor_proto"
        ],
)

go_proto_library(
    name = "annotations_go_proto",
    importpath = "google/api",
    proto = ":annotations_proto",
    visibility = ["//visibility:public"],
    deps = [
        "@com_github_golang_protobuf//protoc-gen-go/descriptor:go_default_library",
    ],
)

go_library(
    name = "go_default_library",
    embed = [":annotations_go_proto"],
    importpath = "google/api",
    visibility = ["//visibility:public"],
)

When I try to compile I get the following error:

ERROR: /home/chris/.cache/bazel/_bazel_user/1568f635c686948d7428fc5fdf7009b8/external/googleapi/BUILD.bazel:45:1: GoCompile external/googleapi/linux_amd64_stripped/go_default_library~/google/api.a failed (Exit 1)
2018/01/24 17:07:00 missing strict dependencies:
    bazel-out/k8-fastbuild/bin/external/googleapi/linux_amd64_stripped/annotations_go_proto~/google/api/annotations.pb.go: import of google/api, which is not a direct dependency

This has got me really stumped because I'm not even sure what the google/api dependency means. I noticed that my annotations.pb.go file differs from Google's file at https://github.com/google/go-genproto/blob/master/googleapis/api/annotations/annotations.pb.go by only 2 lines: 1) an extra import of google/api and 2) *HttpRule instead of *google_api.HttpRule

...
package api

import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import google_api "google/api" # This import doesn't exist in Google's version
import google_protobuf "github.com/golang/protobuf/protoc-gen-go/descriptor"

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package

var E_Http = &proto.ExtensionDesc{
        ExtendedType:  (*google_protobuf.MethodOptions)(nil),
        ExtensionType: (*google_api.HttpRule)(nil), # Google's version is "ExtensionType: (*HttpRule)(nil),"
        Field:         72295728,
        Name:          "google.api.http",
        Tag:           "bytes,72295728,opt,name=http",
        Filename:      "google/api/annotations.proto",
}

func init() {
        proto.RegisterExtension(E_Http)
}
...

My guess is that I'm either not passing in the correct parameters to the go_proto_library method for annotations or there is some way to resolve the google/api dependency that I'm not seeing.

question

Most helpful comment

I made a couple changes to your build file. Try using this one instead:

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

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

proto_library(
    name = "api_proto",
    srcs = [
        "google/api/http.proto",
        "google/api/annotations.proto",
    ],
    deps = ["@com_google_protobuf//:descriptor_proto"],
)

go_proto_library(
    name = "api_go_proto",
    importpath = "google/api",
    proto = ":api_proto",
    deps = [
        "@com_github_golang_protobuf//protoc-gen-go/descriptor:go_default_library",
    ],
)

Changes made:

  • Consolidated http_proto and annotations_proto into a single api_proto rule. These files are in the same proto package, so they can be compiled together. Compiling them separately causes the generated Go package to import itself, which is why you saw that error.
  • Renamed annotations_go_proto to api_go_proto. In general, you need one go_proto_library rule for each proto_library that is part of your build. The dependencies should correspond.
  • Dropped the go_default_library rule. Gazelle generates these, but they aren't strictly necessary unless you want to add additional .go files or dependencies.

All 3 comments

I made a couple changes to your build file. Try using this one instead:

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

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

proto_library(
    name = "api_proto",
    srcs = [
        "google/api/http.proto",
        "google/api/annotations.proto",
    ],
    deps = ["@com_google_protobuf//:descriptor_proto"],
)

go_proto_library(
    name = "api_go_proto",
    importpath = "google/api",
    proto = ":api_proto",
    deps = [
        "@com_github_golang_protobuf//protoc-gen-go/descriptor:go_default_library",
    ],
)

Changes made:

  • Consolidated http_proto and annotations_proto into a single api_proto rule. These files are in the same proto package, so they can be compiled together. Compiling them separately causes the generated Go package to import itself, which is why you saw that error.
  • Renamed annotations_go_proto to api_go_proto. In general, you need one go_proto_library rule for each proto_library that is part of your build. The dependencies should correspond.
  • Dropped the go_default_library rule. Gazelle generates these, but they aren't strictly necessary unless you want to add additional .go files or dependencies.

In #1163 they are trying to add grpc_gateway_support (my use case as well) and this issue was one of the main blockers

Forgot to mention that this worked at the time, but it may be related to a new issue I opened up in bazel-gazelle. https://github.com/bazelbuild/bazel-gazelle/issues/225

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jarreds picture jarreds  路  7Comments

blico picture blico  路  4Comments

steeve picture steeve  路  5Comments

dlorenc picture dlorenc  路  7Comments

apesternikov picture apesternikov  路  3Comments