| 1 |
"""This module defines the go_googleapis_compatibility_hack repository rule. |
| 2 |
|
| 3 |
This file is copied almost verbatim from the Skia Infrastructure repository: |
| 4 |
https://skia.googlesource.com/buildbot/+/d32a5c6f592dfd0668001e57b0f4bb55523629f5/bazel/external/go_googleapis_compatibility_hack.bzl |
| 5 |
""" |
| 6 |
|
| 7 |
def _go_googleapis_compatibility_hack_impl(ctx): |
| 8 |
if ctx.name != "go_googleapis": |
| 9 |
fail("This rule should be named \"go_googleapis\" for this compatibility hack to work.") |
| 10 |
|
| 11 |
# The visibility of the below aliases should be limited to @com_github_bazelbuild_remote_apis |
| 12 |
# to prevent accidental usages from other dependencies or from our own code. |
| 13 |
|
| 14 |
# Aliases @go_googleapis//google/api:annotations_go_proto. |
| 15 |
ctx.file("google/api/BUILD.bazel", """ |
| 16 |
# GENERATED FILE. See //bazel/external/go_googleapis_compatibility_hack.bzl in the Skia Buildbot |
| 17 |
# repository. |
| 18 |
|
| 19 |
alias( |
| 20 |
name = "annotations_go_proto", |
| 21 |
actual = "@org_golang_google_genproto_googleapis_api//annotations", |
| 22 |
visibility = ["@com_github_bazelbuild_remote_apis//:__subpackages__"], |
| 23 |
) |
| 24 |
""") |
| 25 |
|
| 26 |
# Aliases @go_googleapis//google/longrunning:longrunning_go_proto". |
| 27 |
ctx.file("google/longrunning/BUILD.bazel", """ |
| 28 |
# GENERATED FILE. See //bazel/external/go_googleapis_compatibility_hack.bzl in the Skia Buildbot |
| 29 |
# repository. |
| 30 |
|
| 31 |
alias( |
| 32 |
name = "longrunning_go_proto", |
| 33 |
actual = "@org_golang_google_genproto//googleapis/longrunning", |
| 34 |
visibility = ["@com_github_bazelbuild_remote_apis//:__subpackages__"], |
| 35 |
) |
| 36 |
""") |
| 37 |
|
| 38 |
# Aliases @go_googleapis//google/rpc:status_go_pro |
| 39 |
ctx.file("google/rpc/BUILD.bazel", """ |
| 40 |
# GENERATED FILE. See //bazel/external/go_googleapis_compatibility_hack.bzl in the Skia Buildbot |
| 41 |
# repository. |
| 42 |
|
| 43 |
alias( |
| 44 |
name = "status_go_proto", |
| 45 |
actual = "@org_golang_google_genproto_googleapis_rpc//status", |
| 46 |
visibility = ["@com_github_bazelbuild_remote_apis//:__subpackages__"], |
| 47 |
) |
| 48 |
""") |
| 49 |
|
| 50 |
go_googleapis_compatibility_hack = repository_rule( |
| 51 |
doc = """Hack to make github.com/bazelbuild/remote-apis work with rules_go v0.41.0. |
| 52 |
|
| 53 |
Starting with version v0.41.0, rules_go no longer ships with the @go_googleapis external |
| 54 |
repository: https://github.com/bazelbuild/rules_go/releases/tag/v0.41.0. As per the release notes, |
| 55 |
Go code imported generated Google API .pb.go files should use the @org_golang_google_genproto |
| 56 |
repository instead (https://github.com/googleapis/go-genproto). |
| 57 |
|
| 58 |
As of 2023-05-26, the github.com/googleapis/go-genproto Go module has been broken into smaller |
| 59 |
submodules as a usability improvements for customers that do not depend on Google Cloud: |
| 60 |
https://github.com/googleapis/go-genproto/issues/1015. This means some former @go_googleapis |
| 61 |
dependencies are now found in Go modules such as google.golang.org/genproto/googleapis/api and |
| 62 |
google.golang.org/genproto/googleapis/rpc. In Bazel terms, the corresponding Gazelle-generated |
| 63 |
external repositories will be @org_golang_google_genproto_googleapis_api and |
| 64 |
@com_github_bazelbuild_remote_apis, respectively. |
| 65 |
|
| 66 |
Unfortunately, as of 2023-10-30 the https://github.com/bazelbuild/remote-apis Go module is still |
| 67 |
distributed with BUILD files that reference the @go_googleapis repository: |
| 68 |
https://github.com/bazelbuild/remote-apis/blob/6c32c3b917cc5d3cfee680c03179d7552832bb3f/build/bazel/remote/execution/v2/go/BUILD#L13. |
| 69 |
|
| 70 |
As a compatibility hack, this repository rule provides a fake @go_googleapis repository that |
| 71 |
aliases the Bazel labels required by https://github.com/bazelbuild/remote-apis to point to the |
| 72 |
correct targets within @org_golang_google_genproto, @org_golang_google_genproto_googleapis_api, |
| 73 |
etc. |
| 74 |
|
| 75 |
We should delete this rule if/when https://github.com/bazelbuild/remote-apis is updated to work |
| 76 |
with more recent versions of rules_go. |
| 77 |
""", |
| 78 |
implementation = _go_googleapis_compatibility_hack_impl, |
| 79 |
) |