bazelbuild / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.18k stars 373 forks source link

Adding Google Cloud pubsub causes "Inconsistent definition for type field_mask.FieldMask" #201

Closed cdelguercio closed 6 years ago

cdelguercio commented 6 years ago

I'm trying to build a library I wrote to deal with Google Cloud pubsub, but I've ran into a strange error. When I run Bazel I get:

ERROR: /home/.../external/com_google_cloud_go/pubsub/BUILD.bazel:3:1: GoCompile external/com_google_cloud_go/pubsub/linux_amd64_stripped/go_default_library~/cloud.google.com/go/pubsub.a failed (Exit 1) bazel-out/k8-fastbuild/bin/external/io_bazel_rules_go/proto/wkt/linux_amd64_stripped/field_mask_go_proto~/google.golang.org/genproto/protobuf/field_mask/field_mask.pb.go:21:0: inconsistent definition for type field_mask.FieldMask during import struct { Paths []string "protobuf:\"bytes,1,rep,name=paths\" json:\"paths,omitempty\"" } (in "cloud.google.com/go/pubsub/apiv1") struct { Paths []string "protobuf:\"bytes,1,rep,name=paths\" json:\"paths,omitempty\""; XXX_NoUnkeyedLiteral struct {} "json:\"-\""; XXX_unrecognized []byte "json:\"-\""; XXX_sizecache int32 "json:\"-\"" } (in "google.golang.org/genproto/protobuf/field_mask") GoCompile: error running subcommand: exit status 1

Here's my BUILD.bazel

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

go_library(
    name = "go_default_library",
    srcs = [
        "google_pubsub.go",
    ],
    importpath = "path/to/my/lib/pubsub",
    visibility = ["//visibility:public"],
    deps = [
        "@com_google_cloud_go//pubsub:go_default_library",
    ],
)

And the relevant parts of my WORKSPACE

# ------------------------------------------------------------------------------
http_archive(
    name = "com_google_protobuf",
    sha256 = "4ffd420f39f226e96aebc3554f9c66a912f6cad6261f39f194f16af8a1f6dab2",
    strip_prefix = "protobuf-3.5.2",
    urls = ["https://github.com/google/protobuf/archive/v3.5.2.tar.gz"],
)

http_archive(
    name = "io_bazel_rules_go",
    url = "https://github.com/bazelbuild/rules_go/releases/download/0.12.0/rules_go-0.12.0.tar.gz",  # May 8, 2018
    sha256 = "c1f52b8789218bb1542ed362c4f7de7052abcf254d865d96fb7ba6d44bc15ee3",
)
http_archive(
    name = "bazel_gazelle",
    url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.12.0/bazel-gazelle-0.12.0.tar.gz",
    sha256 = "ddedc7aaeb61f2654d7d7d4fd7940052ea992ccdb031b8f9797ed143ac7e8d43",
)
load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains()
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
gazelle_dependencies()

go_repository(
    name = "com_github_golang_protobuf",
    importpath = "github.com/golang/protobuf",
    # commit = "e09c5db296004fbe3f74490e84dcd62c3c5ddb1b",  # March 28, 2018
    tag = "v1.1.0",  # April 30, 2018
)

go_repository(
    name = "com_google_cloud_go",
    importpath = "cloud.google.com/go",
    tag = "v0.22.0",  # May 9, 2018
)

# needed by com_google_cloud_go
go_repository(
    name = "org_golang_google_api",
    importpath = "google.golang.org/api",
    commit = "aedc137bffcaca05f836e35c6d182fcb7a68b1c0",  # May 17, 2018
)

go_repository(
    name = "org_golang_x_sync",
    importpath = "golang.org/x/sync",
    commit = "1d60e4601c6fd243af51cc01ddf169918a5407ca",  # March 14, 2018
)

go_repository(
    name = "org_golang_x_oauth2",
    importpath = "golang.org/x/oauth2",
    commit = "cdc340f7c179dbbfa4afd43b7614e8fcadde4269",  # May 2, 2018
)

go_repository(
    name = "io_opencensus_go",
    importpath = "go.opencensus.io",
    tag = "v0.9.0",  # May 1, 2018
)

go_repository(
    name = "com_github_googleapis_gax_go",
    importpath = "github.com/googleapis/gax-go",
    tag = "v2.0.0",  # September 14, 2017
)

Do I have two conflicting repositories somehow?

jayconrod commented 6 years ago

I think you have conflicting packages for the Well Known Types getting linked into your build. Gazelle resolves Go imports of WKTs to @io_bazel_rules_go//proto/wkt, but this is quite recent, and there are a lot of build files that reference the old targets out there.

Try using rules_go ca213b3006c8eed6b3f1ea649cab36b817901b46 (currently tip of master). That introduces aliases for WKT libraries so that fewer conflicts are possible. You may also need to avoid declaring com_github_golang_protobuf explicitly; also watch out for org_golang_google_genproto, though it doesn't look like you're declaring that.

cdelguercio commented 6 years ago

Forgot to mention that your suggestion completely worked. Thanks!

adieu commented 6 years ago

I had the same issue. Solved it by replacing the deps //protobuf/field_mask:go_default_library to @io_bazel_rules_go//proto/wkt:field_mask_go_proto in @org_golang_google_genproto//googleapis/pubsub/v1/BUILD.bazel

dadrian commented 6 years ago

I had the same issue, solved by explicitly depending on the latest org_golang_google_genproto before I called go_rules_dependencies().