Skip to content

Commit 8f09b96

Browse files
authored
bazel: Use jarjar to avoid xds deps (#12243)
Avoiding so many deps will allow us to upgrade the protos without being forced to upgrade to protobuf-java 4.x. It also removes the remaining non-bzlmod dependencies. It'd be really easy to get this wrong, so we do two things 1) mirror the gradle configuration as much as possible, as that sees a lot of testing, and 2) run the fake control plane with the _results_ of jarjar. There's lots of classes that we could mess up, but that at least kicks the tires. XdsTestUtils.buildRouteConfiguration() was moved to ControlPlaneRule to stop the unnecessary circular dependency between the classes and to avoid the many dependencies of XdsTestUtils. I'm totally hacking java_grpc_library to improve the dependency situation. Long-term, I think we will stop building Java libraries with Bazel and require users to rely entirely on Maven Central. That seems to be the direction Bazel is going and it will greatly simplify the problems we've seen with protobuf having a single repository for many languages. So while the hack isn't too bad, I hope we won't have to live with it long-term.
1 parent 42e1829 commit 8f09b96

File tree

13 files changed

+380
-188
lines changed

13 files changed

+380
-188
lines changed

.github/workflows/testing.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ jobs:
102102
- name: Run bazel build
103103
run: bazelisk build //... --enable_bzlmod=${{ matrix.bzlmod }}
104104

105+
- name: Run bazel test
106+
run: bazelisk test //... --enable_bzlmod=${{ matrix.bzlmod }}
107+
105108
- name: Run example bazel build
106109
run: bazelisk build //... --enable_bzlmod=${{ matrix.bzlmod }}
107110
working-directory: ./examples

MODULE.bazel

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,16 @@ IO_GRPC_GRPC_JAVA_ARTIFACTS = [
4646
]
4747
# GRPC_DEPS_END
4848

49+
bazel_dep(name = "bazel_jar_jar", version = "0.1.7")
4950
bazel_dep(name = "bazel_skylib", version = "1.7.1")
5051
bazel_dep(name = "googleapis", repo_name = "com_google_googleapis", version = "0.0.0-20240326-1c8d509c5")
51-
52-
# CEL Spec may be removed when cncf/xds MODULE is no longer using protobuf 27.x
53-
bazel_dep(name = "cel-spec", repo_name = "dev_cel", version = "0.15.0")
54-
bazel_dep(name = "grpc", repo_name = "com_github_grpc_grpc", version = "1.56.3.bcr.1")
5552
bazel_dep(name = "grpc-proto", repo_name = "io_grpc_grpc_proto", version = "0.0.0-20240627-ec30f58")
5653
bazel_dep(name = "protobuf", repo_name = "com_google_protobuf", version = "23.1")
5754
bazel_dep(name = "rules_cc", version = "0.0.9")
5855
bazel_dep(name = "rules_java", version = "5.3.5")
59-
bazel_dep(name = "rules_go", repo_name = "io_bazel_rules_go", version = "0.46.0")
6056
bazel_dep(name = "rules_jvm_external", version = "6.0")
6157
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
6258

63-
non_module_deps = use_extension("//:repositories.bzl", "grpc_java_repositories_extension")
64-
65-
use_repo(
66-
non_module_deps,
67-
"com_github_cncf_xds",
68-
"envoy_api",
69-
)
70-
71-
grpc_repo_deps_ext = use_extension("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_repo_deps_ext")
72-
73-
use_repo(
74-
grpc_repo_deps_ext,
75-
"com_envoyproxy_protoc_gen_validate",
76-
"opencensus_proto",
77-
)
78-
7959
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
8060

8161
maven.install(
@@ -202,7 +182,3 @@ maven.override(
202182
coordinates = "io.grpc:grpc-util",
203183
target = "@io_grpc_grpc_java//util",
204184
)
205-
206-
switched_rules = use_extension("@com_google_googleapis//:extensions.bzl", "switched_rules")
207-
208-
switched_rules.use_languages(java = True)

WORKSPACE

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,19 @@ load("//:repositories.bzl", "grpc_java_repositories")
2222

2323
grpc_java_repositories()
2424

25+
load("@bazel_jar_jar//:jar_jar.bzl", "jar_jar_repositories")
26+
27+
jar_jar_repositories()
28+
2529
load("@com_google_protobuf//:protobuf_deps.bzl", "PROTOBUF_MAVEN_ARTIFACTS")
2630
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
2731

2832
protobuf_deps()
2933

30-
load("@envoy_api//bazel:repositories.bzl", "api_dependencies")
31-
32-
api_dependencies()
33-
3434
load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language")
3535

3636
switched_rules_by_language(
3737
name = "com_google_googleapis_imports",
38-
java = True,
3938
)
4039

4140
maven_install(

compiler/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ cc_binary(
1818

1919
java_library(
2020
name = "java_grpc_library_deps__do_not_reference",
21+
visibility = ["//xds:__pkg__"],
2122
exports = [
2223
"//api",
2324
"//protobuf",

examples/MODULE.bazel

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ local_path_override(
1111
path = "..",
1212
)
1313

14-
switched_rules = use_extension("@com_google_googleapis//:extensions.bzl", "switched_rules")
15-
16-
switched_rules.use_languages(java = True)
17-
1814
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
1915

2016
use_repo(maven, "maven")

examples/WORKSPACE

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,21 @@ load("@io_grpc_grpc_java//:repositories.bzl", "grpc_java_repositories")
2828

2929
grpc_java_repositories()
3030

31+
load("@bazel_jar_jar//:jar_jar.bzl", "jar_jar_repositories")
32+
33+
jar_jar_repositories()
34+
3135
# Protobuf now requires C++14 or higher, which requires Bazel configuration
3236
# outside the WORKSPACE. See .bazelrc in this directory.
3337
load("@com_google_protobuf//:protobuf_deps.bzl", "PROTOBUF_MAVEN_ARTIFACTS")
3438
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
3539

3640
protobuf_deps()
3741

38-
load("@envoy_api//bazel:repositories.bzl", "api_dependencies")
39-
40-
api_dependencies()
41-
4242
load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language")
4343

4444
switched_rules_by_language(
4545
name = "com_google_googleapis_imports",
46-
java = True,
4746
)
4847

4948
maven_install(

java_grpc_library.bzl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,33 @@ _java_grpc_library = rule(
147147
implementation = _java_rpc_library_impl,
148148
)
149149

150+
# A copy of _java_grpc_library, except with a neverlink=1 _toolchain
151+
INTERNAL_java_grpc_library_for_xds = rule(
152+
attrs = {
153+
"srcs": attr.label_list(
154+
mandatory = True,
155+
allow_empty = False,
156+
providers = [ProtoInfo],
157+
),
158+
"deps": attr.label_list(
159+
mandatory = True,
160+
allow_empty = False,
161+
providers = [JavaInfo],
162+
),
163+
"_toolchain": attr.label(
164+
default = Label("//xds:java_grpc_library_toolchain"),
165+
),
166+
},
167+
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
168+
fragments = ["java"],
169+
outputs = {
170+
"jar": "lib%{name}.jar",
171+
"srcjar": "lib%{name}-src.jar",
172+
},
173+
provides = [JavaInfo],
174+
implementation = _java_rpc_library_impl,
175+
)
176+
150177
_java_lite_grpc_library = rule(
151178
attrs = {
152179
"srcs": attr.label_list(

repositories.bzl

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -87,38 +87,11 @@ IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS = {
8787
"io.grpc:grpc-util": "@io_grpc_grpc_java//util",
8888
}
8989

90-
def grpc_java_repositories(bzlmod = False):
90+
def grpc_java_repositories():
9191
"""Imports dependencies for grpc-java."""
92-
if not bzlmod and not native.existing_rule("dev_cel"):
93-
http_archive(
94-
name = "dev_cel",
95-
strip_prefix = "cel-spec-0.15.0",
96-
sha256 = "3ee09eb69dbe77722e9dee23dc48dc2cd9f765869fcf5ffb1226587c81791a0b",
97-
urls = [
98-
"https://github.com/google/cel-spec/archive/refs/tags/v0.15.0.tar.gz",
99-
],
100-
)
101-
if not native.existing_rule("com_github_cncf_xds"):
102-
http_archive(
103-
name = "com_github_cncf_xds",
104-
strip_prefix = "xds-2ac532fd44436293585084f8d94c6bdb17835af0",
105-
sha256 = "790c4c83b6950bb602fec221f6a529d9f368cdc8852aae7d2592d0d04b015f37",
106-
urls = [
107-
"https://github.com/cncf/xds/archive/2ac532fd44436293585084f8d94c6bdb17835af0.tar.gz",
108-
],
109-
)
110-
if not bzlmod and not native.existing_rule("com_github_grpc_grpc"):
111-
http_archive(
112-
name = "com_github_grpc_grpc",
113-
strip_prefix = "grpc-1.46.0",
114-
sha256 = "67423a4cd706ce16a88d1549297023f0f9f0d695a96dd684adc21e67b021f9bc",
115-
urls = [
116-
"https://github.com/grpc/grpc/archive/v1.46.0.tar.gz",
117-
],
118-
)
119-
if not bzlmod and not native.existing_rule("com_google_protobuf"):
92+
if not native.existing_rule("com_google_protobuf"):
12093
com_google_protobuf()
121-
if not bzlmod and not native.existing_rule("com_google_googleapis"):
94+
if not native.existing_rule("com_google_googleapis"):
12295
http_archive(
12396
name = "com_google_googleapis",
12497
sha256 = "49930468563dd48283e8301e8d4e71436bf6d27ac27c235224cc1a098710835d",
@@ -127,25 +100,14 @@ def grpc_java_repositories(bzlmod = False):
127100
"https://github.com/googleapis/googleapis/archive/ca1372c6d7bcb199638ebfdb40d2b2660bab7b88.tar.gz",
128101
],
129102
)
130-
if not bzlmod and not native.existing_rule("io_bazel_rules_go"):
131-
http_archive(
132-
name = "io_bazel_rules_go",
133-
sha256 = "ab21448cef298740765f33a7f5acee0607203e4ea321219f2a4c85a6e0fb0a27",
134-
urls = [
135-
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.32.0/rules_go-v0.32.0.zip",
136-
"https://github.com/bazelbuild/rules_go/releases/download/v0.32.0/rules_go-v0.32.0.zip",
137-
],
138-
)
139103
if not native.existing_rule("io_grpc_grpc_proto"):
140104
io_grpc_grpc_proto()
141-
if not native.existing_rule("envoy_api"):
105+
if not native.existing_rule("bazel_jar_jar"):
142106
http_archive(
143-
name = "envoy_api",
144-
sha256 = "ecf71817233eba19cc8b4ee14e126ffd5838065d5b5a92b2506258a42ac55199",
145-
strip_prefix = "data-plane-api-0bc95493c5e88b7b07e62758d23b39341813a827",
146-
urls = [
147-
"https://github.com/envoyproxy/data-plane-api/archive/0bc95493c5e88b7b07e62758d23b39341813a827.tar.gz",
148-
],
107+
name = "bazel_jar_jar",
108+
sha256 = "3117f913c732142a795551f530d02c9157b9ea895e6b2de0fbb5af54f03040a5",
109+
strip_prefix = "bazel_jar_jar-0.1.6",
110+
url = "https://github.com/bazeltools/bazel_jar_jar/releases/download/v0.1.6/bazel_jar_jar-v0.1.6.tar.gz",
149111
)
150112

151113
def com_google_protobuf():
@@ -166,8 +128,3 @@ def io_grpc_grpc_proto():
166128
strip_prefix = "grpc-proto-4f245d272a28a680606c0739753506880cf33b5f",
167129
urls = ["https://github.com/grpc/grpc-proto/archive/4f245d272a28a680606c0739753506880cf33b5f.zip"],
168130
)
169-
170-
def _grpc_java_repositories_extension(_):
171-
grpc_java_repositories(bzlmod = True)
172-
173-
grpc_java_repositories_extension = module_extension(implementation = _grpc_java_repositories_extension)

testing-proto/BUILD.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
load("//:java_grpc_library.bzl", "java_grpc_library")
2+
3+
proto_library(
4+
name = "simpleservice_proto",
5+
srcs = ["src/main/proto/io/grpc/testing/protobuf/simpleservice.proto"],
6+
strip_import_prefix = "src/main/proto/",
7+
)
8+
9+
java_proto_library(
10+
name = "simpleservice_java_proto",
11+
visibility = ["//xds:__pkg__"],
12+
deps = [":simpleservice_proto"],
13+
)
14+
15+
java_grpc_library(
16+
name = "simpleservice_java_grpc",
17+
srcs = [":simpleservice_proto"],
18+
visibility = ["//xds:__pkg__"],
19+
deps = [":simpleservice_java_proto"],
20+
)

0 commit comments

Comments
 (0)