Skip to content

Commit 21f70db

Browse files
authored
Toolchainize twitter_scrooge (#1693)
* Toolchainize twitter_scrooge This is the last of the toolchain to receive the "toolchainization" treatment prior to Bzlmodification, and moves `twitter_scrooge()` to `twitter_scrooge/toolchain/toolchain.bzl` for `rules_java` 8 compatibility. Part of #1482 and #1652. * Use twitter_scrooge args dict, fix propagation As requested by @simuons in #1693, `scala_toolchains` now receives `twitter_scrooge` options as a `dict`. Since all of these options are for alternative toolchain dependencies, I've called this new `dict` argument `twitter_scrooge_deps`. However, after looking closely at how the options propagated, I realized they never made it to the toolchain generated by `scala_toolchains`. So this change also passes these options all the way through to the generated `BUILD` file and through the `setup_scala_toolchain()` call. * Retry ./test_reproducibility.sh on macOS Seemingly spurious failure during `git checkout`: - https://buildkite.com/organizations/bazel/pipelines/rules-scala-scala/builds/5350/jobs/0194f660-94eb-464a-a81d-9d1c576c2968/log ```txt cd /Users/buildkite/builds/bk-macos-intel-ggrd/bazel/rules-scala-scala $ cd /usr/local/var/bazelbuild ⚠️ Warning: Checkout failed! getting/updating git mirror: setting remote URL: exit status 71 (Attempt 1/3 Retrying in 2s) $ cd /Users/buildkite/builds/bk-macos-intel-ggrd/bazel/rules-scala-scala $ cd /usr/local/var/bazelbuild ⚠️ Warning: Checkout failed! getting/updating git mirror: setting remote URL: exit status 71 (Attempt 2/3 Retrying in 2s) $ cd /Users/buildkite/builds/bk-macos-intel-ggrd/bazel/rules-scala-scala $ cd /usr/local/var/bazelbuild ⚠️ Warning: Checkout failed! getting/updating git mirror: setting remote URL: exit status 71 (Attempt 3/3) $ cd /Users/buildkite/builds/bk-macos-intel-ggrd/bazel/rules-scala-scala 🚨 Error: getting/updating git mirror: setting remote URL: exit status 71 ```
1 parent 21d6ed0 commit 21f70db

File tree

10 files changed

+297
-240
lines changed

10 files changed

+297
-240
lines changed

WORKSPACE

+1-4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ scala_toolchains(
5454
scala_proto = True,
5555
scalafmt = True,
5656
testing = True,
57+
twitter_scrooge = True,
5758
)
5859

5960
register_toolchains(
@@ -62,10 +63,6 @@ register_toolchains(
6263
"@io_bazel_rules_scala_toolchains//...:all",
6364
)
6465

65-
load("//twitter_scrooge:twitter_scrooge.bzl", "twitter_scrooge")
66-
67-
twitter_scrooge()
68-
6966
# needed for the cross repo proto test
7067
local_repository(
7168
name = "proto_cross_repo_boundary",

scala/toolchains.bzl

+34-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ load("//scalatest:scalatest.bzl", "scalatest_artifact_ids")
1515
load("//specs2:specs2.bzl", "specs2_artifact_ids")
1616
load("//specs2:specs2_junit.bzl", "specs2_junit_artifact_ids")
1717
load("//third_party/repositories:repositories.bzl", "repositories")
18+
load(
19+
"//twitter_scrooge/toolchain:toolchain.bzl",
20+
"twitter_scrooge_artifact_ids",
21+
_TWITTER_SCROOGE_DEPS = "TOOLCHAIN_DEPS",
22+
)
1823
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")
1924

25+
def _get_unknown_entries(entries, allowed_entries):
26+
return [e for e in entries if e not in allowed_entries]
27+
2028
def scala_toolchains(
2129
maven_servers = default_maven_server_urls(),
2230
overridden_artifacts = {},
@@ -33,7 +41,9 @@ def scala_toolchains(
3341
scalafmt_default_config_path = ".scalafmt.conf",
3442
scala_proto = False,
3543
scala_proto_enable_all_options = False,
36-
jmh = False):
44+
jmh = False,
45+
twitter_scrooge = False,
46+
twitter_scrooge_deps = {}):
3747
"""Instantiates @io_bazel_rules_scala_toolchains and all its dependencies.
3848
3949
Provides a unified interface to configuring rules_scala both directly in a
@@ -86,7 +96,23 @@ def scala_toolchains(
8696
toolchain with all options enabled; `scala_proto` must also be
8797
`True` for this to take effect
8898
jmh: whether to instantiate the jmh toolchain
99+
twitter_scrooge: whether to instantiate the twitter_scrooge toolchain
100+
twitter_scrooge_deps: dictionary of string to Label containing overrides
101+
for twitter_scrooge toolchain dependency providers with keys:
102+
libthrift
103+
scrooge_core
104+
scrooge_generator
105+
util_core
106+
util_logging
89107
"""
108+
unknown_ts_deps = _get_unknown_entries(
109+
twitter_scrooge_deps,
110+
_TWITTER_SCROOGE_DEPS,
111+
)
112+
113+
if unknown_ts_deps:
114+
fail("unknown twitter_scrooge_deps:", ", ".join(unknown_ts_deps))
115+
90116
scala_repositories(
91117
maven_servers = maven_servers,
92118
# Note the internal macro parameter misspells "overriden".
@@ -130,6 +156,11 @@ def scala_toolchains(
130156
id: False
131157
for id in jmh_artifact_ids()
132158
})
159+
if twitter_scrooge:
160+
artifact_ids_to_fetch_sources.update({
161+
id: False
162+
for id in twitter_scrooge_artifact_ids(**twitter_scrooge_deps)
163+
})
133164

134165
for scala_version in SCALA_VERSIONS:
135166
version_specific_artifact_ids = {}
@@ -168,6 +199,8 @@ def scala_toolchains(
168199
scala_proto = scala_proto,
169200
scala_proto_enable_all_options = scala_proto_enable_all_options,
170201
jmh = jmh,
202+
twitter_scrooge = twitter_scrooge,
203+
twitter_scrooge_deps = twitter_scrooge_deps,
171204
)
172205

173206
def scala_register_toolchains():

scala/toolchains_repo.bzl

+39
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ def _generate_testing_toolchain_build_file_args(repo_attr):
3535
"specs2_junit": framework_deps.get("specs2_junit"),
3636
}
3737

38+
_TWITTER_SCROOGE_ARGS = [
39+
"libthrift",
40+
"scrooge_core",
41+
"scrooge_generator",
42+
"util_core",
43+
"util_logging",
44+
]
45+
46+
def _stringify_template_args(args, arg_names):
47+
return {
48+
arg: ("\"%s\"" % value if type(value) == "string" else value)
49+
for arg, value in {name: args.get(name) for name in arg_names}.items()
50+
}
51+
3852
def _scala_toolchains_repo_impl(repository_ctx):
3953
repo_attr = repository_ctx.attr
4054
format_args = {
@@ -49,6 +63,12 @@ def _scala_toolchains_repo_impl(repository_ctx):
4963
toolchains["scala_proto"] = _SCALA_PROTO_TOOLCHAIN_BUILD
5064
if repo_attr.jmh:
5165
toolchains["jmh"] = _JMH_TOOLCHAIN_BUILD
66+
if repo_attr.twitter_scrooge:
67+
toolchains["twitter_scrooge"] = _TWITTER_SCROOGE_TOOLCHAIN_BUILD
68+
format_args.update(_stringify_template_args(
69+
repo_attr.twitter_scrooge_deps,
70+
_TWITTER_SCROOGE_ARGS,
71+
))
5272

5373
testing_build_args = _generate_testing_toolchain_build_file_args(repo_attr)
5474
if testing_build_args != None:
@@ -81,6 +101,9 @@ _scala_toolchains_repo = repository_rule(
81101
"scala_proto": attr.bool(),
82102
"scala_proto_enable_all_options": attr.bool(),
83103
"jmh": attr.bool(),
104+
"twitter_scrooge": attr.bool(),
105+
# attr.string_keyed_label_dict isn't available in Bazel 6
106+
"twitter_scrooge_deps": attr.string_dict(),
84107
},
85108
)
86109

@@ -210,3 +233,19 @@ load("@@{rules_scala_repo}//jmh/toolchain:toolchain.bzl", "setup_jmh_toolchain")
210233
211234
setup_jmh_toolchain(name = "jmh_toolchain")
212235
"""
236+
237+
_TWITTER_SCROOGE_TOOLCHAIN_BUILD = """
238+
load(
239+
"@@{rules_scala_repo}//twitter_scrooge/toolchain:toolchain.bzl",
240+
"setup_scrooge_toolchain",
241+
)
242+
243+
setup_scrooge_toolchain(
244+
name = "scrooge_toolchain",
245+
libthrift = {libthrift},
246+
scrooge_core = {scrooge_core},
247+
scrooge_generator = {scrooge_generator},
248+
util_core = {util_core},
249+
util_logging = {util_logging},
250+
)
251+
"""

test_version.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ run_in_test_repo() {
4646

4747
if [[ -n "$TWITTER_SCROOGE_VERSION" ]]; then
4848
local version_param="version = \"$TWITTER_SCROOGE_VERSION\""
49-
scrooge_ws="scrooge_repositories($version_param)"
49+
scrooge_ws="$version_param"
5050
fi
5151

5252
sed -e "s%\${twitter_scrooge_repositories}%${scrooge_ws}\n%" \

test_version/WORKSPACE.template

+5-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ scala_config(enable_compiler_dependency_tracking = True)
5454

5555
load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains")
5656

57+
load(":scrooge_repositories.bzl", "scrooge_repositories")
58+
59+
scrooge_repositories(${twitter_scrooge_repositories})
60+
5761
scala_toolchains(
5862
fetch_sources = True,
5963
scala_proto = True,
@@ -63,11 +67,6 @@ scala_toolchains(
6367

6468
register_toolchains(
6569
"@io_bazel_rules_scala//scala:unused_dependency_checker_error_toolchain",
66-
"@io_bazel_rules_scala//testing:testing_toolchain",
70+
"@twitter_scrooge_test_toolchain//...:all",
6771
"@io_bazel_rules_scala_toolchains//...:all",
6872
)
69-
70-
load(":scrooge_repositories.bzl", "scrooge_repositories")
71-
${twitter_scrooge_repositories}
72-
load("@io_bazel_rules_scala//twitter_scrooge:twitter_scrooge.bzl", "twitter_scrooge")
73-
twitter_scrooge()

test_version/version_specific_tests_dir/scrooge_repositories.bzl

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1+
load(
2+
"@io_bazel_rules_scala//scala:scala_cross_version.bzl",
3+
"default_maven_server_urls",
4+
)
15
load(
26
"@io_bazel_rules_scala//scala:scala_maven_import_external.bzl",
37
_scala_maven_import_external = "scala_maven_import_external",
48
)
59
load(
6-
"@io_bazel_rules_scala//scala:scala_cross_version.bzl",
7-
"default_maven_server_urls",
10+
"@io_bazel_rules_scala//scala:toolchains_repo.bzl",
11+
"scala_toolchains_repo",
12+
)
13+
load(
14+
"@io_bazel_rules_scala//twitter_scrooge/toolchain:toolchain.bzl",
15+
"twitter_scrooge",
816
)
917

1018
def _import_external(id, artifact, sha256, deps = [], runtime_deps = []):
1119
_scala_maven_import_external(
1220
name = id,
21+
generated_rule_name = id,
1322
artifact = artifact,
1423
artifact_sha256 = sha256,
1524
licenses = ["notice"],
@@ -20,8 +29,11 @@ def _import_external(id, artifact, sha256, deps = [], runtime_deps = []):
2029
fetch_sources = False,
2130
)
2231

23-
def scrooge_repositories(version):
32+
def scrooge_repositories(version = None):
33+
use_custom_toolchain_deps = False
34+
2435
if version == "18.6.0":
36+
use_custom_toolchain_deps = True
2537
_import_external(
2638
id = "io_bazel_rules_scala_scrooge_core",
2739
artifact = "com.twitter:scrooge-core_2.11:18.6.0",
@@ -48,7 +60,8 @@ def scrooge_repositories(version):
4860
sha256 = "73ddd61cedabd4dab82b30e6c52c1be6c692b063b8ba310d716ead9e3b4e9267",
4961
)
5062

51-
if version == "21.2.0":
63+
elif version == "21.2.0":
64+
use_custom_toolchain_deps = True
5265
_import_external(
5366
id = "io_bazel_rules_scala_scrooge_core",
5467
artifact = "com.twitter:scrooge-core_2.11:21.2.0",
@@ -74,3 +87,20 @@ def scrooge_repositories(version):
7487
artifact = "com.twitter:util-logging_2.11:21.2.0",
7588
sha256 = "f3b62465963fbf0fe9860036e6255337996bb48a1a3f21a29503a2750d34f319",
7689
)
90+
91+
toolchain_deps = {} if use_custom_toolchain_deps == False else {
92+
dep: "@io_bazel_rules_scala_%s" % dep
93+
for dep in [
94+
"scrooge_core",
95+
"scrooge_generator",
96+
"util_core",
97+
"util_logging",
98+
]
99+
}
100+
101+
twitter_scrooge(register_toolchains = False, **toolchain_deps)
102+
scala_toolchains_repo(
103+
name = "twitter_scrooge_test_toolchain",
104+
twitter_scrooge = True,
105+
twitter_scrooge_deps = toolchain_deps,
106+
)

test_version/version_specific_tests_dir/twitter_scrooge/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies_to_test = [
88
deps_with_external_binds = [
99
(
1010
dep_name,
11-
"//external:io_bazel_rules_scala/dependency/thrift/{}".format(dep_name),
11+
"@io_bazel_rules_scala_{}".format(dep_name),
1212
)
1313
for dep_name in dependencies_to_test
1414
]

twitter_scrooge/BUILD

+14-85
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,14 @@
1-
load("//twitter_scrooge/toolchain:toolchain.bzl", "export_scrooge_deps", "scrooge_toolchain")
2-
load("//scala:providers.bzl", "declare_deps_provider")
3-
4-
scrooge_toolchain(
5-
name = "scrooge_toolchain_impl",
6-
visibility = ["//visibility:public"],
7-
)
8-
9-
toolchain(
10-
name = "scrooge_toolchain",
11-
toolchain = ":scrooge_toolchain_impl",
12-
toolchain_type = "@io_bazel_rules_scala//twitter_scrooge/toolchain:scrooge_toolchain_type",
13-
visibility = ["//visibility:public"],
14-
)
15-
16-
declare_deps_provider(
17-
name = "aspect_compile_classpath_provider",
18-
deps_id = "aspect_compile_classpath",
19-
visibility = ["//visibility:public"],
20-
deps = [
21-
"//external:io_bazel_rules_scala/dependency/thrift/javax_annotation_api",
22-
"//external:io_bazel_rules_scala/dependency/thrift/libthrift",
23-
"//external:io_bazel_rules_scala/dependency/thrift/scrooge_core",
24-
"//external:io_bazel_rules_scala/dependency/thrift/util_core",
25-
"//scala/private/toolchain_deps:scala_library_classpath",
26-
],
27-
)
28-
29-
declare_deps_provider(
30-
name = "compile_classpath_provider",
31-
deps_id = "compile_classpath",
32-
visibility = ["//visibility:public"],
33-
deps = [
34-
"//external:io_bazel_rules_scala/dependency/thrift/libthrift",
35-
"//external:io_bazel_rules_scala/dependency/thrift/scrooge_core",
36-
"//scala/private/toolchain_deps:scala_library_classpath",
37-
],
38-
)
39-
40-
declare_deps_provider(
41-
name = "scrooge_generator_classpath_provider",
42-
deps_id = "scrooge_generator_classpath",
43-
visibility = ["//visibility:public"],
44-
deps = [
45-
"//external:io_bazel_rules_scala/dependency/thrift/scrooge_generator",
46-
],
47-
)
48-
49-
declare_deps_provider(
50-
name = "compiler_classpath_provider",
51-
deps_id = "compiler_classpath",
52-
visibility = ["//visibility:public"],
53-
deps = [
54-
"//external:io_bazel_rules_scala/dependency/thrift/mustache",
55-
"//external:io_bazel_rules_scala/dependency/thrift/scopt",
56-
"//external:io_bazel_rules_scala/dependency/thrift/scrooge_generator",
57-
"//external:io_bazel_rules_scala/dependency/thrift/util_core",
58-
"//external:io_bazel_rules_scala/dependency/thrift/util_logging",
59-
"//scala/private/toolchain_deps:parser_combinators",
60-
],
61-
)
62-
63-
export_scrooge_deps(
64-
name = "compile_classpath",
65-
deps_id = "compile_classpath",
66-
visibility = ["//visibility:public"],
67-
)
68-
69-
export_scrooge_deps(
70-
name = "aspect_compile_classpath",
71-
deps_id = "aspect_compile_classpath",
72-
visibility = ["//visibility:public"],
73-
)
74-
75-
export_scrooge_deps(
76-
name = "scrooge_generator_classpath",
77-
deps_id = "scrooge_generator_classpath",
78-
visibility = ["//visibility:public"],
79-
)
80-
81-
export_scrooge_deps(
82-
name = "compiler_classpath",
83-
deps_id = "compiler_classpath",
84-
visibility = ["//visibility:public"],
85-
)
1+
load(
2+
"//twitter_scrooge/toolchain:toolchain.bzl",
3+
"DEP_PROVIDERS",
4+
"export_scrooge_deps",
5+
)
6+
7+
[
8+
export_scrooge_deps(
9+
name = dep,
10+
deps_id = dep,
11+
visibility = ["//visibility:public"],
12+
)
13+
for dep in DEP_PROVIDERS
14+
]

0 commit comments

Comments
 (0)