Skip to content

Commit

Permalink
go_proto_library: drop dependencies on APIv1 WKT packages (#2740)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Jay Conrod committed Jun 28, 2021
1 parent 2e339e9 commit 5d306c4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 39 deletions.
9 changes: 6 additions & 3 deletions README.rst
Expand Up @@ -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")
Expand Down
25 changes: 16 additions & 9 deletions go/dependencies.rst
Expand Up @@ -119,19 +119,23 @@ 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
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
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")
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions proto/BUILD.bazel
Expand Up @@ -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",
Expand Down
34 changes: 9 additions & 25 deletions 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"]),
Expand All @@ -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()
Expand All @@ -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",
Expand All @@ -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 []

Expand Down

0 comments on commit 5d306c4

Please sign in to comment.