Skip to content

Commit 1d52819

Browse files
meteorcloudycopybara-github
authored andcommitted
Support proto_library targets in @bazel_tools//src/main/protobuf (bazelbuild#28428)
This addresses bazelbuild#28400. Bazel 9 breaks usage of `proto_library` targets in `@bazel_tools//src/main/protobuf` because the `BUILD` file there loads rules and depends on repositories (like `@grpc-java`, `@com_github_grpc_grpc`, etc.) that are not visible from the `@bazel_tools` repository. This change introduces `src/main/protobuf/BUILD.tools`, which will be used as the `BUILD` file for `@bazel_tools//src/main/protobuf`. This version: 1. Only defines the `proto_library` targets, avoiding problematic loads and cross-repo dependencies. 2. Adds `alias` targets for the language-specific targets (e.g., `_java_proto`) that were previously defined in the `BUILD` file. 3. Points these aliases to a dedicated target that fails with a clear and helpful error message, explaining that language-specific proto targets are not available in `@bazel_tools` and users should generate their own code from the `proto_library`. This ensures that users can still depend on the `proto_library` targets while providing a better experience for those attempting to use the unsupported language-specific targets. Closes bazelbuild#28428. PiperOrigin-RevId: 862360134 Change-Id: I55461f5bf08a4695f6148a5f8c0640e12d8d290e
1 parent 06f4839 commit 1d52819

File tree

5 files changed

+267
-0
lines changed

5 files changed

+267
-0
lines changed

.bazelci/postsubmit.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ tasks:
286286
- "-//src/test/py/bazel:bazel_lockfile_test"
287287
- "-//src/test/py/bazel:bazel_overrides_test"
288288
- "-//src/test/py/bazel:bazel_repo_mapping_test"
289+
- "-//src/test/py/bazel:bazel_tools_proto_test"
289290
- "-//src/test/py/bazel:bazel_yanked_versions_test"
290291
- "-//src/test/py/bazel:bzlmod_query_test"
291292
- "-//src/test/py/bazel:mod_command_test"

.bazelci/presubmit.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ tasks:
263263
- "-//src/test/py/bazel:bazel_lockfile_test"
264264
- "-//src/test/py/bazel:bazel_overrides_test"
265265
- "-//src/test/py/bazel:bazel_repo_mapping_test"
266+
- "-//src/test/py/bazel:bazel_tools_proto_test"
266267
- "-//src/test/py/bazel:bazel_yanked_versions_test"
267268
- "-//src/test/py/bazel:bzlmod_query_test"
268269
- "-//src/test/py/bazel:mod_command_test"

src/main/protobuf/BUILD.tools

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
FILES = [
6+
"action_cache",
7+
"bazel_flags",
8+
"builtin",
9+
"crash_debugging",
10+
"crosstool_config",
11+
"deps",
12+
"desugar_deps",
13+
"execution_statistics",
14+
"extra_actions_base",
15+
"java_compilation",
16+
"memory_pressure",
17+
"strategy_policy",
18+
"test_status",
19+
"worker_protocol",
20+
"execution_graph",
21+
"file_invalidation_data",
22+
]
23+
24+
[proto_library(
25+
name = s + "_proto",
26+
srcs = [s + ".proto"],
27+
) for s in FILES]
28+
29+
proto_library(
30+
name = "build_proto",
31+
srcs = ["build.proto"],
32+
deps = [":stardoc_output_proto"],
33+
)
34+
35+
proto_library(
36+
name = "analysis_v2_proto",
37+
srcs = ["analysis_v2.proto"],
38+
deps = [":build_proto"],
39+
)
40+
41+
proto_library(
42+
name = "command_server_proto",
43+
srcs = ["command_server.proto"],
44+
deps = [
45+
":failure_details_proto",
46+
"@com_google_protobuf//:any_proto",
47+
],
48+
)
49+
50+
proto_library(
51+
name = "failure_details_proto",
52+
srcs = ["failure_details.proto"],
53+
deps = ["@com_google_protobuf//:descriptor_proto"],
54+
)
55+
56+
proto_library(
57+
name = "invocation_policy_proto",
58+
srcs = ["invocation_policy.proto"],
59+
deps = [":strategy_policy_proto"],
60+
)
61+
62+
proto_library(
63+
name = "option_filters_proto",
64+
srcs = ["option_filters.proto"],
65+
)
66+
67+
proto_library(
68+
name = "command_line_proto",
69+
srcs = ["command_line.proto"],
70+
deps = [":option_filters_proto"],
71+
)
72+
73+
proto_library(
74+
name = "cache_salt_proto",
75+
srcs = ["cache_salt.proto"],
76+
)
77+
78+
proto_library(
79+
name = "remote_scrubbing_proto",
80+
srcs = ["remote_scrubbing.proto"],
81+
)
82+
83+
proto_library(
84+
name = "spawn_proto",
85+
srcs = ["spawn.proto"],
86+
deps = [
87+
"@com_google_protobuf//:duration_proto",
88+
"@com_google_protobuf//:timestamp_proto",
89+
],
90+
)
91+
92+
proto_library(
93+
name = "stardoc_output_proto",
94+
srcs = ["stardoc_output.proto"],
95+
)
96+
97+
proto_library(
98+
name = "xcode_proto",
99+
srcs = ["xcode_config.proto"],
100+
)
101+
102+
# The following targets are not available in @bazel_tools
103+
# to give a clear error message if someone tries to use them.
104+
105+
UNSUPPORTED_LANGUAGE_SPECIFIC_TARGETS = [
106+
"build_java_proto",
107+
"build_java_proto_srcs",
108+
"analysis_v2_py_proto",
109+
"analysis_v2_java_proto",
110+
"analysis_v2_java_proto_srcs",
111+
"any_java_proto",
112+
"wrappers_java_proto",
113+
"command_server_java_proto",
114+
"command_server_java_proto_srcs",
115+
"failure_details_java_proto",
116+
"failure_details_java_proto_srcs",
117+
"invocation_policy_java_proto",
118+
"invocation_policy_java_proto_srcs",
119+
"option_filters_java_proto",
120+
"option_filters_java_proto_srcs",
121+
"command_line_java_proto",
122+
"command_line_java_proto_srcs",
123+
"desugar_deps_cc_proto",
124+
"worker_protocol_cc_proto",
125+
"command_server_java_grpc",
126+
"command_server_cc_proto",
127+
"command_server_cc_grpc",
128+
"build_pb_py",
129+
"profile_java_proto_srcs",
130+
"execution_statistics_cc_proto",
131+
"remote_execution_log_proto",
132+
"remote_execution_log_java_proto",
133+
"remote_execution_log_java_proto_srcs",
134+
"cache_salt_java_proto",
135+
"cache_salt_java_proto_srcs",
136+
"remote_scrubbing_java_proto",
137+
"remote_scrubbing_java_proto_srcs",
138+
"bazel_output_service_java_proto",
139+
"bazel_output_service_java_proto_srcs",
140+
"bazel_output_service_cc_proto",
141+
"bazel_output_service_rev2_java_proto",
142+
"bazel_output_service_rev2_java_proto_srcs",
143+
"bazel_output_service_rev2_cc_proto",
144+
"bazel_output_service_java_grpc",
145+
"bazel_output_service_cc_grpc",
146+
"spawn_java_proto",
147+
"spawn_java_proto_srcs",
148+
"stardoc_output_java_proto",
149+
"stardoc_output_java_proto_srcs",
150+
"xcode_java_proto",
151+
"xcode_cc_proto",
152+
"xcode_java_proto_srcs",
153+
]
154+
155+
[alias(
156+
name = s + "_java_proto",
157+
actual = ":ERROR_language_specific_proto_library_is_unsupported_in_bazel_tools",
158+
) for s in FILES]
159+
160+
[alias(
161+
name = s + "_java_proto_srcs",
162+
actual = ":ERROR_language_specific_proto_library_is_unsupported_in_bazel_tools",
163+
) for s in FILES]
164+
165+
[alias(
166+
name = name,
167+
actual = ":ERROR_language_specific_proto_library_is_unsupported_in_bazel_tools",
168+
) for name in UNSUPPORTED_LANGUAGE_SPECIFIC_TARGETS]
169+
170+
genrule(
171+
name = "ERROR_language_specific_proto_library_is_unsupported_in_bazel_tools",
172+
outs = ["unused.txt"],
173+
cmd = "echo '\n\nLanguage specific proto targets are not available in @bazel_tools. Please use the proto_library and generate your own code.\n\n' >&2 && exit 1",
174+
)
175+

src/test/py/bazel/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ py_test(
253253
deps = [":test_base"],
254254
)
255255

256+
py_test(
257+
name = "bazel_tools_proto_test",
258+
size = "medium",
259+
srcs = ["bazel_tools_proto_test.py"],
260+
deps = [":test_base"],
261+
)
262+
256263
py_library(
257264
name = "bzlmod_test_utils",
258265
srcs = ["bzlmod/test_utils.py"],
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# pylint: disable=g-bad-file-header
2+
# Copyright 2026 The Bazel Authors. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from src.test.py.bazel import test_base
17+
18+
19+
class BazelToolsProtoTest(test_base.TestBase):
20+
21+
def testProtoLibraryInBazelTools(self):
22+
self.AddBazelDep('protobuf')
23+
self.ScratchFile(
24+
'BUILD',
25+
[
26+
'load("@protobuf//bazel:proto_library.bzl", "proto_library")',
27+
'proto_library(',
28+
' name = "my_proto",',
29+
' srcs = ["my.proto"],',
30+
(
31+
' deps ='
32+
' ["@bazel_tools//src/main/protobuf:action_cache_proto"],'
33+
),
34+
')',
35+
],
36+
)
37+
self.ScratchFile(
38+
'my.proto',
39+
[
40+
'syntax = "proto3";',
41+
'import "src/main/protobuf/action_cache.proto";',
42+
'package my;',
43+
'message MyMessage {',
44+
' blaze.ActionCacheStatistics stats = 1;',
45+
'}',
46+
],
47+
)
48+
49+
# This should succeed because we fixed the visibility/load issues in
50+
# @bazel_tools//src/main/protobuf:BUILD
51+
self.RunBazel(['build', '//:my_proto'])
52+
53+
def testUnsupportedLanguageSpecificProtoTargets(self):
54+
self.AddBazelDep('protobuf')
55+
self.ScratchFile(
56+
'BUILD',
57+
[
58+
'alias(',
59+
' name = "bad_alias",',
60+
(
61+
' actual ='
62+
' "@bazel_tools//src/main/protobuf:action_cache_java_proto",'
63+
),
64+
')',
65+
],
66+
)
67+
68+
exit_code, _, stderr = self.RunBazel(
69+
['build', '//:bad_alias'], allow_failure=True
70+
)
71+
self.AssertExitCode(exit_code, 1, stderr)
72+
self.assertIn(
73+
'Language specific proto targets are not available in @bazel_tools',
74+
''.join(stderr),
75+
)
76+
self.assertIn(
77+
'Please use the proto_library and generate your own code',
78+
''.join(stderr),
79+
)
80+
81+
82+
if __name__ == '__main__':
83+
test_base.absltest.main()

0 commit comments

Comments
 (0)