Skip to content

Commit 4bdf33a

Browse files
Don't rely on SCALA_VERSION in phases (#1559)
* Start documentation for version-dependent behavior * Don't rely on `SCALA_VERSION` in phases Use a new field of toolchain, `scala_version`, instead. Its value is based on the current value of Scala version build setting. Co-authored-by: mkuta <[email protected]> --------- Co-authored-by: mkuta <[email protected]>
1 parent 2df777e commit 4bdf33a

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

cross-compilation-doc.md

+18
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,21 @@ config_setting(
3535
```
3636
The `name` of `config_setting` corresponds to `"scala_version" + version_suffix(scala_version)`.
3737
One may use this config setting in `select()` e.g. to provide dependencies relevant to a currently used Scala version.
38+
39+
40+
## Version-dependent behavior
41+
Don't rely on `SCALA_VERSION` as it represents the default Scala version, not necessarily the one that is currently requested.
42+
43+
If you need to customize the behavior for specific Scala version, there are two scenarios.
44+
45+
### From toolchain
46+
If you have an access to the Scala toolchain (`@io_bazel_rules_scala//scala:toolchain_type`), there is `scala_version` field provided in there:
47+
```starlark
48+
def _rule_impl(ctx):
49+
...
50+
ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_version
51+
...
52+
```
53+
54+
### From config setting
55+
TODO

scala/private/phases/phase_compile.bzl

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ load(
1717
_compile_scala = "compile_scala",
1818
)
1919
load(":resources.bzl", _resource_paths = "paths")
20-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
21-
22-
buildijar_default_value = True if SCALA_VERSION.startswith("2.") else False
2320

2421
def phase_compile_binary(ctx, p):
2522
args = struct(
@@ -105,6 +102,8 @@ def phase_compile_common(ctx, p):
105102
return _phase_compile_default(ctx, p)
106103

107104
def _phase_compile_default(ctx, p, _args = struct()):
105+
buildijar_default_value = True if ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_version.startswith("2.") else False
106+
108107
return _phase_compile(
109108
ctx,
110109
p,

scala/private/phases/phase_semanticdb.bzl

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ load(
33
"find_deps_info_on",
44
)
55
load("@io_bazel_rules_scala//scala:semanticdb_provider.bzl", "SemanticdbInfo")
6-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_MAJOR_VERSION")
76
load("@bazel_skylib//lib:paths.bzl", "paths")
87

98
def phase_semanticdb(ctx, p):
@@ -37,7 +36,7 @@ def phase_semanticdb(ctx, p):
3736
outputfilename = "%s/%s/%s.semanticdb" % (semanticdb_intpath, semanticdb_outpath, currSrc.path)
3837
output_files.append(ctx.actions.declare_file(outputfilename))
3938

40-
if SCALA_MAJOR_VERSION.startswith("2"):
39+
if toolchain.scala_version.startswith("2"):
4140
semanticdb_deps = find_deps_info_on(ctx, toolchain_type_label, "semanticdb").deps
4241

4342
if len(semanticdb_deps) == 0:

scala/scala_toolchain.bzl

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ load(
77
"ENABLE_COMPILER_DEPENDENCY_TRACKING",
88
"SCALA_MAJOR_VERSION",
99
)
10+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
1011

1112
def _compute_strict_deps_mode(input_strict_deps_mode, dependency_mode):
1213
if dependency_mode == "direct":
@@ -101,6 +102,7 @@ def _scala_toolchain_impl(ctx):
101102
enable_semanticdb = ctx.attr.enable_semanticdb,
102103
semanticdb_bundle_in_jar = ctx.attr.semanticdb_bundle_in_jar,
103104
use_argument_file_in_runner = ctx.attr.use_argument_file_in_runner,
105+
scala_version = ctx.attr._scala_version[BuildSettingInfo].value,
104106
)
105107
return [toolchain]
106108

@@ -173,6 +175,7 @@ scala_toolchain = rule(
173175
default = False,
174176
doc = "Changes java binaries scripts (including tests) to use argument files and not classpath jars to improve performance, requires java > 8",
175177
),
178+
"_scala_version": attr.label(default = "@io_bazel_rules_scala_config//:scala_version"),
176179
},
177180
fragments = ["java"],
178181
)

0 commit comments

Comments
 (0)