From 5d306c433cebb1ae8a7b72df2a055be2bacbb12b Mon Sep 17 00:00:00 2001 From: Jay Conrod Date: Mon, 28 Jun 2021 11:52:05 -0700 Subject: [PATCH] go_proto_library: drop dependencies on APIv1 WKT packages (#2740) Since protobuf 3.14, the Well Known Types declare APIv2 package paths, which means generated code that imports them will import APIv2 packages. Fixes #2721 --- README.rst | 9 ++++++--- go/dependencies.rst | 25 ++++++++++++++++--------- proto/BUILD.bazel | 4 ++-- proto/wkt/well_known_types.bzl | 34 +++++++++------------------------- 4 files changed, 33 insertions(+), 39 deletions(-) diff --git a/README.rst b/README.rst index 329cfa484f..3fd2ff79fd 100644 --- a/README.rst +++ b/README.rst @@ -438,9 +438,12 @@ To generate code from protocol buffers, you'll need to add a dependency on http_archive( name = "com_google_protobuf", - sha256 = "9748c0d90e54ea09e5e75fb7fac16edce15d2028d4356f32211cfa3c0e956564", - strip_prefix = "protobuf-3.11.4", - urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.11.4.zip"], + sha256 = "d0f5f605d0d656007ce6c8b5a82df3037e1d8fe8b121ed42e536f569dec16113", + strip_prefix = "protobuf-3.14.0", + urls = [ + "https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz", + "https://github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz", + ], ) load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") diff --git a/go/dependencies.rst b/go/dependencies.rst index c1d677bb40..ce991f8406 100644 --- a/go/dependencies.rst +++ b/go/dependencies.rst @@ -119,9 +119,10 @@ Proto dependencies ------------------ In order to build `proto_library`_ and `go_proto_library`_ rules, you must -add a dependency on ``com_google_protobuf`` (perhaps on a newer version) -in order to build the ``protoc`` compiler. You'll need a C/C++ toolchain for -the execution platform, too. +add a dependency on ``com_google_protobuf`` in order to build the ``protoc`` +compiler. Version 3.14.0 is the minimum supported version, but higher minor +versions shoudl work. You'll need a C/C++ toolchain for the execution platform +to build ``protoc``. .. code:: bzl @@ -129,9 +130,12 @@ the execution platform, too. http_archive( name = "com_google_protobuf", - sha256 = "9748c0d90e54ea09e5e75fb7fac16edce15d2028d4356f32211cfa3c0e956564", - strip_prefix = "protobuf-3.11.4", - urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.11.4.zip"], + sha256 = "d0f5f605d0d656007ce6c8b5a82df3037e1d8fe8b121ed42e536f569dec16113", + strip_prefix = "protobuf-3.14.0", + urls = [ + "https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz", + "https://github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz", + ], ) load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") @@ -225,9 +229,12 @@ For example, this is how you would override ``com_github_golang_protobuf``: http_archive( name = "com_google_protobuf", - sha256 = "9748c0d90e54ea09e5e75fb7fac16edce15d2028d4356f32211cfa3c0e956564", - strip_prefix = "protobuf-3.11.4", - urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.11.4.zip"], + sha256 = "d0f5f605d0d656007ce6c8b5a82df3037e1d8fe8b121ed42e536f569dec16113", + strip_prefix = "protobuf-3.14.0", + urls = [ + "https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz", + "https://github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz", + ], ) load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") diff --git a/proto/BUILD.bazel b/proto/BUILD.bazel index 6b5894d1f4..9bb7d343e8 100644 --- a/proto/BUILD.bazel +++ b/proto/BUILD.bazel @@ -20,14 +20,14 @@ go_proto_compiler( go_proto_compiler( name = "go_proto", visibility = ["//visibility:public"], - deps = PROTO_RUNTIME_DEPS + WELL_KNOWN_TYPE_RULES.values() + WELL_KNOWN_TYPES_APIV2, + deps = PROTO_RUNTIME_DEPS + WELL_KNOWN_TYPES_APIV2, ) go_proto_compiler( name = "go_grpc", options = ["plugins=grpc"], visibility = ["//visibility:public"], - deps = PROTO_RUNTIME_DEPS + WELL_KNOWN_TYPE_RULES.values() + WELL_KNOWN_TYPES_APIV2 + [ + deps = PROTO_RUNTIME_DEPS + WELL_KNOWN_TYPES_APIV2 + [ "@org_golang_google_grpc//:go_default_library", "@org_golang_google_grpc//codes:go_default_library", "@org_golang_google_grpc//status:go_default_library", diff --git a/proto/wkt/well_known_types.bzl b/proto/wkt/well_known_types.bzl index 60333bc77d..725043a954 100644 --- a/proto/wkt/well_known_types.bzl +++ b/proto/wkt/well_known_types.bzl @@ -1,13 +1,12 @@ load("//go:def.bzl", "GoArchive", "GoLibrary", "GoSource") -load("//proto:def.bzl", "go_proto_library") load("//proto:compiler.bzl", "GoProtoCompiler") _proto_library_suffix = "proto" _go_proto_library_suffix = "go_proto" -# TODO: this should be private. Make sure nothing depends on it, then rename it. -# TODO: remove after protoc 3.14 is the minimum supported version. The WKTs -# changed their import paths in that version. +# NOTE: since protobuf 3.14, the WKTs no longer use these paths. They're only +# used by gogo below. Not clear if that actually works or if we should +# continue supporting gogo. WELL_KNOWN_TYPE_PACKAGES = { "any": ("github.com/golang/protobuf/ptypes/any", []), "api": ("google.golang.org/genproto/protobuf/api", ["source_context", "type"]), @@ -32,6 +31,7 @@ GOGO_WELL_KNOWN_TYPE_REMAPS = [ "Mgoogle/protobuf/compiler_plugin.proto=github.com/gogo/protobuf/protoc-gen-gogo/plugin", ] +# NOTE: only used by gogo. WELL_KNOWN_TYPE_RULES = { wkt: "@io_bazel_rules_go//proto/wkt:{}_{}".format(wkt, _go_proto_library_suffix) for wkt in WELL_KNOWN_TYPE_PACKAGES.keys() @@ -45,17 +45,13 @@ PROTO_RUNTIME_DEPS = [ "@org_golang_google_protobuf//runtime/protoimpl:go_default_library", ] -# TODO(#2721): after com_google_protobuf 3.14 is the minimum supported version, -# drop the implicit dependencies on WELL_KNOWN_TYPE_RULES.values() from -# //proto/wkt:go_proto and //proto/wkt:go_grpc. -# # In protobuf 3.14, the 'option go_package' declarations were changed in the -# Well Known Types to point to the APIv2 packages. Consequently, generated +# Well Known Types to point to the APIv2 packages below. Consequently, generated # proto code will import APIv2 packages instead of the APIv1 packages, even -# when the APIv1 compiler is used (which is still the default). We shouldn't -# need the APIv1 dependencies with protobuf 3.14, but we don't know which -# version is in use at load time. The extra packages will be compiled but not -# linked. +# when the APIv1 compiler is used (which is still the default). +# +# protobuf 3.14 is now the minimum supported version, so we no longer depend +# on the APIv1 packages. WELL_KNOWN_TYPES_APIV2 = [ "@org_golang_google_protobuf//types/descriptorpb", "@org_golang_google_protobuf//types/known/anypb", @@ -71,18 +67,6 @@ WELL_KNOWN_TYPES_APIV2 = [ "@org_golang_google_protobuf//types/pluginpb", ] -def gen_well_known_types(): - for wkt, rule in WELL_KNOWN_TYPE_RULES.items(): - (go_package, deps) = WELL_KNOWN_TYPE_PACKAGES[wkt] - go_proto_library( - name = rule.rsplit(":", 1)[1], - compilers = ["@io_bazel_rules_go//proto:go_proto_bootstrap"], - importpath = go_package, - proto = "@com_google_protobuf//:{}_{}".format(wkt, _proto_library_suffix), - visibility = ["//visibility:public"], - deps = [WELL_KNOWN_TYPE_RULES[dep] for dep in deps], - ) - def _go_proto_wrapper_compile(go, compiler, protos, imports, importpath): return []