Skip to content

Commit a2e69e0

Browse files
committed
Replace scala_deps.toolchains with tag classes
Replaces the `scala_deps.toolchains` tag class, which contained boolean parameters for selecting toolchains, with a series of individual tag classes for each toolchain. Suggested by @simuons in the following comment as an alternative implementation that could allow for adding attributes to each toolchain's tag class. - bazel-contrib#1722 (comment) We already had tag classes for `scalafmt`, `scala_proto`, and `twitter_scrooge` options. Now the presence of these tag classes, along with the tag classes added in this commit, translates to a `True` argument for `scala_toolchains()`.
1 parent 20d3ee6 commit a2e69e0

File tree

16 files changed

+170
-157
lines changed

16 files changed

+170
-157
lines changed

MODULE.bazel

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ use_repo(
7777
"rules_scala_toolchains",
7878
"scala_compiler_sources",
7979
)
80+
scala_deps.scala()
8081

8182
# Register some of our testing toolchains first when building our repo.
8283
register_toolchains(
@@ -100,15 +101,13 @@ dev_deps = use_extension(
100101
"scala_deps",
101102
dev_dependency = True,
102103
)
103-
dev_deps.toolchains(
104-
jmh = True,
105-
junit = True,
106-
scala_proto = True,
107-
scalafmt = True,
108-
scalatest = True,
109-
specs2 = True,
110-
twitter_scrooge = True,
111-
)
104+
dev_deps.jmh()
105+
dev_deps.junit()
106+
dev_deps.scala_proto()
107+
dev_deps.scalafmt()
108+
dev_deps.scalatest()
109+
dev_deps.specs2()
110+
dev_deps.twitter_scrooge()
112111
use_repo(
113112
dev_deps,
114113
"scala_proto_rules_scalapb_compilerplugin",

README.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,18 @@ scala_deps = use_extension(
7575
"scala_deps",
7676
)
7777

78-
# Defines a default toolchain repo for the configured Scala version that
79-
# loads Maven deps like the Scala compiler and standard libs. Enable other builtin
80-
# toolchains by setting their corresponding parameters to `True`. See the
81-
# documentation of `_toolchains_attrs` from `scala/extensions/deps.bzl` for all
82-
# builtin toolchain configuration options.
78+
# Defines a default toolchain repo for the configured Scala version that loads
79+
# Maven deps like the Scala compiler and standard libs. Enable and configure
80+
# other builtin toolchains by instantiating their corresponding tag classes.
81+
# See the documentation in `scala/extensions/deps.bzl` for all builtin
82+
# toolchain configuration options.
8383
#
8484
# On production projects, you may consider defining a custom toolchain to use
85-
# your project's required dependencies instead. In that case, you can omit
86-
# `scala_deps` or explicitly set `scala = False` if you use it to instantiate
87-
# other builtin toolchains.
88-
scala_deps.toolchains()
85+
# your project's required dependencies instead. In that case, you can omit using
86+
# the module extension and this next line altogether. Or, you can still use the
87+
# module extension to instantiate other optional `rules_scala` toolchains
88+
# without it.
89+
scala_deps.scala()
8990

9091
# The remaining items are optional, enabling the precompiled protocol compiler
9192
# toolchain via `--incompatible_enable_proto_toolchain_resolution`. See the
@@ -921,18 +922,23 @@ docs/scala_toolchain.md#b-defining-your-own-scala_toolchain), don't use
921922
`scala_deps` or `scala_toolchains()` if you don't need any other builtin
922923
toolchains.
923924

924-
If you do need other builtin toolchains, then set `scala = False`:
925+
If you do need other builtin toolchains when using Bzlmod, then use the module
926+
extension and only instantiate the tag classes to the corresponding toolchains.
927+
928+
If you do need other builtin toolchains when using `WORKSPACE`, then set `scala
929+
= False`.
925930

926931
```py
927932
# MODULE.bazel
928-
scala_deps.toolchains(
929-
scala = False,
930-
# ...other toolchain parameters...
931-
)
933+
scala_deps.scala_proto()
934+
scala_deps.twitter_scrooge()
935+
# ...other scala_deps tag class instantations...
932936

933937
# WORKSPACE
934938
scala_toolchains(
935939
scala = False,
940+
scala_proto = True,
941+
twitter_scrooge = True,
936942
# ...other toolchain parameters...
937943
)
938944
```
@@ -972,10 +978,9 @@ scala_deps = use_extension(
972978
"@rules_scala//scala/extensions:deps.bzl",
973979
"scala_deps",
974980
)
975-
scala_deps.toolchains(
976-
scalafmt = True,
977-
scalatest = True,
978-
)
981+
scala_deps.scala()
982+
scala_deps.scalafmt()
983+
scala_deps.scalatest()
979984
```
980985

981986
The module extensions call `scala_config()` and `scala_toolchains()`
@@ -1384,9 +1389,7 @@ To access these repositories under Bzlmod, you'll need to add the following to
13841389
your `MODULE.bazel` file:
13851390

13861391
```py
1387-
scala_deps.toolchains(
1388-
twitter_scrooge = True,
1389-
)
1392+
scala_deps.twitter_scrooge()
13901393
use_repo(
13911394
scala_deps,
13921395
"io_bazel_rules_scala_guava",

dt_patches/test_dt_patches/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ scala_deps.settings(
5151
fetch_sources = True,
5252
validate_scala_version = False,
5353
)
54+
scala_deps.scala()
5455

5556
scala_protoc = use_extension(
5657
"@rules_scala//scala/extensions:protoc.bzl",

dt_patches/test_dt_patches_user_srcjar/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ scala_deps.settings(
6161
fetch_sources = True,
6262
validate_scala_version = False,
6363
)
64+
scala_deps.scala()
6465

6566
# The `scala_deps.compiler_srcjar()` tag prevents some of the kinds of errors
6667
# represented in the corresponding `WORKSPACE` file, so we have to force

examples/crossbuild/MODULE.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ use_repo(scala_config, "rules_scala_config")
2525
scala_deps = use_extension(
2626
"@rules_scala//scala/extensions:deps.bzl",
2727
"scala_deps",
28+
dev_dependency = True,
2829
)
2930
scala_deps.settings(
3031
fetch_sources = True,
3132
)
32-
scala_deps.toolchains(
33-
scalatest = True,
34-
)
33+
scala_deps.scala()
34+
scala_deps.scalatest()
3535

3636
scala_protoc = use_extension(
3737
"@rules_scala//scala/extensions:protoc.bzl",

examples/overridden_artifacts/MODULE.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ scala_config.settings(
1919
scala_deps = use_extension(
2020
"@rules_scala//scala/extensions:deps.bzl",
2121
"scala_deps",
22+
dev_dependency = True,
2223
)
2324
scala_deps.settings(
2425
fetch_sources = False,
2526
)
27+
scala_deps.scala()
2628

2729
# Deliberately set for Scala 3.3 and 2.13 versions less than the most
2830
# recently supported. See the `scala_version` setting at the top of
@@ -54,9 +56,7 @@ scala_deps.overridden_artifact(
5456
artifact = "org.scala-lang:scala-library:2.13.14",
5557
sha256 = "43e0ca1583df1966eaf02f0fbddcfb3784b995dd06bfc907209347758ce4b7e3",
5658
)
57-
scala_deps.toolchains(
58-
scalatest = True,
59-
)
59+
scala_deps.scalatest()
6060

6161
scala_protoc = use_extension(
6262
"@rules_scala//scala/extensions:protoc.bzl",

examples/scala3/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ scala_deps = use_extension(
1515
scala_deps.settings(
1616
fetch_sources = True,
1717
)
18+
scala_deps.scala()
1819

1920
scala_protoc = use_extension(
2021
"@rules_scala//scala/extensions:protoc.bzl",

examples/semanticdb/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ scala_deps = use_extension(
2323
scala_deps.settings(
2424
fetch_sources = True,
2525
)
26+
scala_deps.scala()
2627

2728
scala_protoc = use_extension(
2829
"@rules_scala//scala/extensions:protoc.bzl",

examples/testing/multi_frameworks_toolchain/MODULE.bazel

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ use_repo(scala_config, "rules_scala_config")
2424
scala_deps = use_extension(
2525
"@rules_scala//scala/extensions:deps.bzl",
2626
"scala_deps",
27+
dev_dependency = True,
2728
)
2829
scala_deps.settings(
2930
fetch_sources = True,
3031
)
31-
scala_deps.toolchains(
32-
junit = True,
33-
scalatest = True,
34-
specs2 = True,
35-
)
32+
scala_deps.scala()
33+
scala_deps.junit()
34+
scala_deps.scalatest()
35+
scala_deps.specs2()
3636

3737
# Under normal circumstances, the above `scala_deps.toolchains()` registration
3838
# would be all you need. rules_scala will set up and register the toolchains

examples/testing/scalatest_repositories/MODULE.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ local_path_override(
1111
scala_deps = use_extension(
1212
"@rules_scala//scala/extensions:deps.bzl",
1313
"scala_deps",
14+
dev_dependency = True,
1415
)
1516
scala_deps.settings(
1617
fetch_sources = True,
1718
)
18-
scala_deps.toolchains(
19-
scalatest = True,
20-
)
19+
scala_deps.scala()
20+
scala_deps.scalatest()
2121

2222
scala_protoc = use_extension(
2323
"@rules_scala//scala/extensions:protoc.bzl",

examples/testing/specs2_junit_repositories/MODULE.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ local_path_override(
1111
scala_deps = use_extension(
1212
"@rules_scala//scala/extensions:deps.bzl",
1313
"scala_deps",
14+
dev_dependency = True,
1415
)
1516
scala_deps.settings(
1617
fetch_sources = True,
1718
)
18-
scala_deps.toolchains(
19-
specs2 = True,
20-
)
19+
scala_deps.scala()
20+
scala_deps.specs2()
2121

2222
scala_protoc = use_extension(
2323
"@rules_scala//scala/extensions:protoc.bzl",

0 commit comments

Comments
 (0)