Skip to content

Commit 455dcaa

Browse files
authored
feat: support incompatible_enable_proto_toolchain_resolution (#608)
* feat: support incompatible_enable_proto_toolchain_resolution See bazelbuild/rules_proto#213 * Also upgrade rules_proto for WORKSPACE tests * fix: install bazel_features explicitly This seems like a bug in rules_proto that the initializer call can't bootstrap because it hits a bazel_features load
1 parent c77baf0 commit 455dcaa

File tree

6 files changed

+29
-13
lines changed

6 files changed

+29
-13
lines changed

MODULE.bazel

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ bazel_dep(name = "aspect_rules_js", version = "1.34.0")
1212
bazel_dep(name = "bazel_skylib", version = "1.4.1")
1313
bazel_dep(name = "platforms", version = "0.0.5")
1414

15-
# Only needed because rules_proto doesn't provide the protoc toolchain yet.
16-
# TODO(alex/sahin): remove in the future
15+
# TODO(4.x): remove support for non-toolchainized protoc
1716
bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf")
1817

1918
# Similar to rules_python/MODULE.bazel, see https://github.com/bazelbuild/rules_python/pull/832
2019
# These are loaded only when using ts_proto_library
21-
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
20+
bazel_dep(name = "rules_proto", version = "6.0.0")
2221

2322
####### Dev dependencies ########
2423

WORKSPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ npm_repositories()
102102
# bazel run -- @npm_typescript//:tsc --version
103103
rules_ts_dependencies(ts_version_from = "@npm//examples:typescript/resolved.json")
104104

105+
load("@bazel_features//:deps.bzl", "bazel_features_deps")
106+
107+
bazel_features_deps()
108+
105109
###########################################
106110
# Protobuf rules to test ts_proto_library
107111
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

e2e/bzlmod/.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import %workspace%/../../.aspect/bazelrc/performance.bazelrc
99
### YOUR PROJECT SPECIFIC OPTIONS GO HERE ###
1010

1111
common --@aspect_rules_ts//ts:skipLibCheck=honor_tsconfig
12+
# NB: comment this line out to test the legacy path to @com_google_protobuf//:protoc
13+
common --incompatible_enable_proto_toolchain_resolution
1214

1315
# Load any settings & overrides specific to the current user from `.aspect/bazelrc/user.bazelrc`.
1416
# This file should appear in `.gitignore` so that settings are not shared with team members. This

e2e/bzlmod/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local_path_override(
88
bazel_dep(name = "aspect_rules_js", version = "1.34.0", dev_dependency = True)
99
bazel_dep(name = "bazel_skylib", version = "1.4.1", dev_dependency = True)
1010
bazel_dep(name = "rules_proto", version = "5.3.0-21.7", dev_dependency = True)
11+
bazel_dep(name = "toolchains_protoc", version = "0.2.4", dev_dependency = True)
1112

1213
npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm", dev_dependency = True)
1314
npm.npm_translate_lock(

internal_deps.bzl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ def rules_ts_internal_deps():
3737
urls = ["https://github.com/bazelbuild/stardoc/releases/download/0.6.2/stardoc-0.6.2.tar.gz"],
3838
)
3939

40+
http_archive(
41+
name = "bazel_features",
42+
sha256 = "2cd9e57d4c38675d321731d65c15258f3a66438ad531ae09cb8bb14217dc8572",
43+
strip_prefix = "bazel_features-1.11.0",
44+
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.11.0/bazel_features-v1.11.0.tar.gz",
45+
)
46+
4047
http_archive(
4148
name = "rules_proto",
42-
sha256 = "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd",
43-
strip_prefix = "rules_proto-5.3.0-21.7",
44-
urls = [
45-
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz",
46-
],
49+
sha256 = "303e86e722a520f6f326a50b41cfc16b98fe6d1955ce46642a5b7a67c11c0f5d",
50+
strip_prefix = "rules_proto-6.0.0",
51+
url = "https://github.com/bazelbuild/rules_proto/releases/download/6.0.0/rules_proto-6.0.0.tar.gz",
4752
)
4853

4954
http_archive(

ts/private/ts_proto_library.bzl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "COPY_FILE_TO_BIN_TOOLCHAINS")
44
load("@aspect_rules_js//js:libs.bzl", "js_lib_helpers")
55
load("@aspect_rules_js//js:providers.bzl", "JsInfo", "js_info")
66
load("@rules_proto//proto:defs.bzl", "ProtoInfo", "proto_common")
7+
load("@rules_proto//proto:proto_common.bzl", proto_toolchains = "toolchains")
8+
9+
_PROTO_TOOLCHAIN_TYPE = "@rules_proto//proto:toolchain_type"
710

811
# buildifier: disable=function-docstring-header
912
def _protoc_action(ctx, proto_info, outputs, options = {
@@ -43,8 +46,9 @@ def _protoc_action(ctx, proto_info, outputs, options = {
4346

4447
args.add_all(proto_info.direct_sources)
4548

49+
proto_toolchain_enabled = len(proto_toolchains.use_toolchain(_PROTO_TOOLCHAIN_TYPE)) > 0
4650
ctx.actions.run(
47-
executable = ctx.executable.protoc,
51+
executable = ctx.toolchains[_PROTO_TOOLCHAIN_TYPE].proto.proto_compiler if proto_toolchain_enabled else ctx.executable.protoc,
4852
progress_message = "Generating .js/.d.ts from %{label}",
4953
outputs = outputs,
5054
inputs = inputs,
@@ -107,7 +111,7 @@ def _ts_proto_library_impl(ctx):
107111

108112
ts_proto_library = rule(
109113
implementation = _ts_proto_library_impl,
110-
attrs = {
114+
attrs = dict({
111115
"deps": attr.label_list(
112116
providers = [JsInfo],
113117
doc = "Other ts_proto_library rules. TODO: could we collect them with an aspect",
@@ -129,7 +133,6 @@ ts_proto_library = rule(
129133
providers = [ProtoInfo],
130134
mandatory = True,
131135
),
132-
"protoc": attr.label(default = "@com_google_protobuf//:protoc", executable = True, cfg = "exec"),
133136
"protoc_gen_es": attr.label(
134137
doc = "protoc plugin to generate messages",
135138
mandatory = True,
@@ -146,6 +149,8 @@ ts_proto_library = rule(
146149
executable = True,
147150
cfg = "exec",
148151
),
149-
},
150-
toolchains = COPY_FILE_TO_BIN_TOOLCHAINS,
152+
}, **proto_toolchains.if_legacy_toolchain({
153+
"protoc": attr.label(default = "@com_google_protobuf//:protoc", executable = True, cfg = "exec"),
154+
})),
155+
toolchains = COPY_FILE_TO_BIN_TOOLCHAINS + proto_toolchains.use_toolchain(_PROTO_TOOLCHAIN_TYPE),
151156
)

0 commit comments

Comments
 (0)