Skip to content

Commit 5396eb4

Browse files
Don't rely on SCALA_VERSION in phases
Use a new field of toolchain, `scala_version`, instead. Co-authored-by: mkuta <[email protected]>
1 parent 8f255cd commit 5396eb4

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

cross-compilation-doc.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,21 @@ The support for cross-compilation is currently under development.
88
File created there, `config.bzl`, consists of many variables. In particular:
99
* `SCALA_VERSION` – representing the default Scala version, e.g. `"3.3.1"`;
1010
* `SCALA_VERSIONS` – representing all configured Scala versions (currently one), e.g. `["3.3.1"]`.
11+
12+
13+
## Version-dependent behavior
14+
Don't rely on `SCALA_VERSION` as it represents the default Scala version, not necessarily the one that is currently requested.
15+
16+
If you need to customize the behavior for specific Scala version, there are two scenarios.
17+
18+
### From toolchain
19+
If you have an access to the Scala toolchain (`@io_bazel_rules_scala//scala:toolchain_type`), there is `scala_version` field provided in there:
20+
```starlark
21+
def _rule_impl(ctx):
22+
...
23+
ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_version
24+
...
25+
```
26+
27+
### From config setting
28+
TODO

scala/private/phases/phase_compile.bzl

Lines changed: 2 additions & 3 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ load(
66
"@io_bazel_rules_scala_config//:config.bzl",
77
"ENABLE_COMPILER_DEPENDENCY_TRACKING",
88
"SCALA_MAJOR_VERSION",
9+
"SCALA_VERSION",
10+
"SCALA_VERSIONS",
911
)
1012

1113
def _compute_strict_deps_mode(input_strict_deps_mode, dependency_mode):
@@ -101,6 +103,7 @@ def _scala_toolchain_impl(ctx):
101103
enable_semanticdb = ctx.attr.enable_semanticdb,
102104
semanticdb_bundle_in_jar = ctx.attr.semanticdb_bundle_in_jar,
103105
use_argument_file_in_runner = ctx.attr.use_argument_file_in_runner,
106+
scala_version = ctx.attr.scala_version,
104107
)
105108
return [toolchain]
106109

@@ -173,6 +176,12 @@ scala_toolchain = rule(
173176
default = False,
174177
doc = "Changes java binaries scripts (including tests) to use argument files and not classpath jars to improve performance, requires java > 8",
175178
),
179+
"scala_version": attr.string(
180+
default = SCALA_VERSION,
181+
values = SCALA_VERSIONS,
182+
doc = "The Scala version used by the toolchain. Must be consistent with provided `dep_providers`." +
183+
"If default `dep_providers` are used, you need to use default value here as well.",
184+
),
176185
},
177186
fragments = ["java"],
178187
)

0 commit comments

Comments
 (0)