Skip to content

Commit 800cd82

Browse files
authored
Build with JDK 21 (#1556)
* Build with JDK 21 Add -Djava.security.manager=allow jvm flag to: - tools executed via ctx.actions.run if host jdk is >= 17 - generated test executables if target jdk is >= 17 * allow security manager for coverage worker * Add ci build with jdk21 * lint * test all with jdk 21 * Add libxml * Uncomment JunitRuntimePlatform test Make sure compilation toolchain matches runtime target version This test was failing with --java_language_version=21 Because it got compiled with newer jdk than was run.
1 parent bf70fea commit 800cd82

13 files changed

+147
-16
lines changed

.bazelci/presubmit.yml

+12
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,15 @@ tasks:
9090
platform: ubuntu2004
9191
run_targets:
9292
- "//tools:lint_check"
93+
test_rules_scala_jdk21:
94+
name: "./test_rules_scala with jdk21"
95+
platform: ubuntu2004
96+
shell_commands:
97+
- sudo apt update && sudo apt install -y libxml2-utils
98+
- mv tools/bazel.rc.buildkite tools/bazel.rc
99+
- echo "import %workspace%/tools/bazel.rc" > .bazelrc
100+
- echo "build --java_language_version=21" >> .bazelrc
101+
- echo "build --java_runtime_version=21" >> .bazelrc
102+
- echo "build --tool_java_language_version=21" >> .bazelrc
103+
- echo "build --tool_java_runtime_version=21" >> .bazelrc
104+
- "./test_rules_scala.sh"

WORKSPACE

+6
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,9 @@ repositories(
203203
],
204204
maven_servers = MAVEN_SERVER_URLS,
205205
)
206+
207+
load("//test/toolchains:jdk.bzl", "remote_jdk21_repositories", "remote_jdk21_toolchains")
208+
209+
remote_jdk21_repositories()
210+
211+
remote_jdk21_toolchains()

scala/private/common_attributes.bzl

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ implicit_deps = {
8484
"_java_runtime": attr.label(
8585
default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
8686
),
87+
"_java_host_runtime": attr.label(
88+
default = Label("@bazel_tools//tools/jdk:current_host_java_runtime"),
89+
),
8790
"_scalac": attr.label(
8891
executable = True,
8992
cfg = "exec",

scala/private/phases/phase_coverage.bzl

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ load(
88
"@io_bazel_rules_scala//scala/private:coverage_replacements_provider.bzl",
99
_coverage_replacements_provider = "coverage_replacements_provider",
1010
)
11+
load(
12+
"@io_bazel_rules_scala//scala/private:rule_impls.bzl",
13+
_allow_security_manager = "allow_security_manager",
14+
)
1115

1216
def phase_coverage_library(ctx, p):
1317
args = struct(
@@ -60,7 +64,7 @@ def _phase_coverage(ctx, p, srcjars):
6064
outputs = [output_jar],
6165
executable = ctx.attr._code_coverage_instrumentation_worker.files_to_run,
6266
execution_requirements = {"supports-workers": "1"},
63-
arguments = [args],
67+
arguments = ["--jvm_flag=%s" % f for f in _allow_security_manager(ctx)] + [args],
6468
)
6569

6670
replacements = {input_jar: output_jar}

scala/private/phases/phase_scalafmt.bzl

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ load(
77
"@io_bazel_rules_scala//scala/private:paths.bzl",
88
_scala_extension = "scala_extension",
99
)
10+
load(
11+
"//scala/private:rule_impls.bzl",
12+
_allow_security_manager = "allow_security_manager",
13+
)
1014

1115
def phase_scalafmt(ctx, p):
1216
if ctx.attr.format:
@@ -25,14 +29,15 @@ def _build_format(ctx):
2529
files = []
2630
srcs = []
2731
manifest_content = []
32+
jvm_flags = ["-Dfile.encoding=UTF-8"] + _allow_security_manager(ctx)
2833
for src in ctx.files.srcs:
2934
# only format scala source files, not generated files
3035
if src.path.endswith(_scala_extension) and src.is_source:
3136
srcs.append(src)
3237
file = ctx.actions.declare_file("{}.fmt.output".format(src.short_path))
3338
files.append(file)
3439
ctx.actions.run(
35-
arguments = ["--jvm_flag=-Dfile.encoding=UTF-8", _format_args(ctx, src, file)],
40+
arguments = ["--jvm_flag=%s" % f for f in jvm_flags] + [_format_args(ctx, src, file)],
3641
executable = ctx.executable._fmt,
3742
outputs = [file],
3843
inputs = [ctx.file.config, src],

scala/private/phases/phase_write_executable.bzl

+15-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#
66
load(
77
"@io_bazel_rules_scala//scala/private:rule_impls.bzl",
8+
"allow_security_manager",
89
"expand_location",
910
"first_non_empty",
1011
"is_windows",
@@ -22,14 +23,12 @@ def phase_write_executable_scalatest(ctx, p):
2223
ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scala_test_jvm_flags,
2324
)
2425

25-
expanded_jvm_flags = [
26-
"-DRULES_SCALA_MAIN_WS_NAME=%s" % ctx.workspace_name,
27-
"-DRULES_SCALA_ARGS_FILE=%s" % p.runfiles.args_file.short_path.replace("../", "external/"),
28-
] + expand_location(ctx, final_jvm_flags)
29-
3026
args = struct(
3127
rjars = p.coverage_runfiles.rjars,
32-
jvm_flags = _allow_security_manager(ctx, expanded_jvm_flags),
28+
jvm_flags = [
29+
"-DRULES_SCALA_MAIN_WS_NAME=%s" % ctx.workspace_name,
30+
"-DRULES_SCALA_ARGS_FILE=%s" % p.runfiles.args_file.short_path.replace("../", "external/"),
31+
] + expand_location(ctx, final_jvm_flags) + _allow_security_manager_for_specified_java_runtime(ctx),
3332
use_jacoco = ctx.configuration.coverage_enabled,
3433
)
3534
return _phase_write_executable_default(ctx, p, args)
@@ -44,7 +43,7 @@ def phase_write_executable_repl(ctx, p):
4443
def phase_write_executable_junit_test(ctx, p):
4544
args = struct(
4645
rjars = p.coverage_runfiles.rjars,
47-
jvm_flags = _allow_security_manager(ctx, p.jvm_flags + ctx.attr.jvm_flags),
46+
jvm_flags = p.jvm_flags + ctx.attr.jvm_flags + _allow_security_manager_for_specified_java_runtime(ctx),
4847
main_class = "com.google.testing.junit.runner.BazelTestRunner",
4948
use_jacoco = ctx.configuration.coverage_enabled,
5049
)
@@ -187,7 +186,12 @@ def _jar_path_based_on_java_bin(ctx):
187186
jar_path = java_bin_var.rpartition("/")[0] + "/jar"
188187
return jar_path
189188

190-
def _allow_security_manager(ctx, jvm_flags):
191-
java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"]
192-
java_runtime = specified_java_runtime(ctx, java_toolchain.java.java_runtime if java_toolchain else None)
193-
return jvm_flags + (["-Djava.security.manager=allow"] if java_runtime and java_runtime.version >= 17 else [])
189+
# Allow security manager for generated test executables if they will be run with jdk >= 17
190+
def _allow_security_manager_for_specified_java_runtime(ctx):
191+
return allow_security_manager(
192+
ctx,
193+
specified_java_runtime(
194+
ctx,
195+
default_runtime = ctx.attr._java_runtime[java_common.JavaRuntimeInfo],
196+
),
197+
)

scala/private/rule_impls.bzl

+11-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ def compile_scala(
121121
)
122122

123123
# scalac_jvm_flags passed in on the target override scalac_jvm_flags passed in on the toolchain
124-
final_scalac_jvm_flags = first_non_empty(scalac_jvm_flags, toolchain.scalac_jvm_flags)
124+
# TODO: scalac worker is run with @bazel_tools//tools/jdk:runtime_toolchain_type
125+
# which is different from rules_java where compilation runtime is used from
126+
# @bazel_tools//tools/jdk:toolchain_type
127+
final_scalac_jvm_flags = first_non_empty(scalac_jvm_flags, toolchain.scalac_jvm_flags) + allow_security_manager(ctx)
125128

126129
ctx.actions.run(
127130
inputs = ins,
@@ -219,3 +222,10 @@ def java_bin_windows(ctx):
219222

220223
def is_windows(ctx):
221224
return ctx.configuration.host_path_separator == ";"
225+
226+
# Return a jvm flag allowing security manager for jdk runtime >= 17
227+
# If no runtime is supplied then runtime is taken from ctx.attr._java_host_runtime
228+
# This must be a runtime used in generated java_binary script (usually workers using SecurityManager)
229+
def allow_security_manager(ctx, runtime = None):
230+
java_runtime = runtime if runtime else ctx.attr._java_host_runtime[java_common.JavaRuntimeInfo]
231+
return ["-Djava.security.manager=allow"] if java_runtime.version >= 17 else []

scala/scalafmt/phase_scalafmt_ext.bzl

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ ext_scalafmt = {
2323
default = "//scala/scalafmt",
2424
executable = True,
2525
),
26+
"_java_host_runtime": attr.label(
27+
default = Label("@bazel_tools//tools/jdk:current_host_java_runtime"),
28+
),
2629
"_runner": attr.label(
2730
allow_single_file = True,
2831
default = "//scala/scalafmt:runner",

scala_proto/private/scala_proto_aspect.bzl

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ load(
55
"//scala/private:rule_impls.bzl",
66
"compile_scala",
77
"specified_java_compile_toolchain",
8+
_allow_security_manager = "allow_security_manager",
89
)
910
load("//scala/private/toolchain_deps:toolchain_deps.bzl", "find_deps_info_on")
1011
load(
@@ -73,7 +74,7 @@ def _generate_sources(ctx, toolchain, proto):
7374

7475
ctx.actions.run(
7576
executable = toolchain.worker,
76-
arguments = [toolchain.worker_flags, args],
77+
arguments = ["--jvm_flag=%s" % f for f in _allow_security_manager(ctx)] + [toolchain.worker_flags, args],
7778
inputs = depset(transitive = [descriptors, toolchain.generators_jars]),
7879
outputs = outputs.values(),
7980
tools = [toolchain.protoc],
@@ -203,6 +204,9 @@ def make_scala_proto_aspect(*extras):
203204
"_java_toolchain": attr.label(
204205
default = Label("@bazel_tools//tools/jdk:current_java_toolchain"),
205206
),
207+
"_java_host_runtime": attr.label(
208+
default = Label("@bazel_tools//tools/jdk:current_host_java_runtime"),
209+
),
206210
}
207211
return aspect(
208212
implementation = _scala_proto_aspect_impl,

test/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,8 @@ scala_library(
781781
srcs = [
782782
"src/main/scala/scalarules/test/junit/runtime_platform/JunitRuntimePlatformTest.java",
783783
],
784+
# make sure java compilation toolchain matches runtime toolchain ie --target
785+
java_compile_toolchain = "@bazel_tools//tools/jdk:toolchain_java11",
784786
deps = ["@io_bazel_rules_scala_junit_junit"],
785787
)
786788

test/toolchains/BUILD.bazel

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
load("//scala:scala_toolchain.bzl", "scala_toolchain")
2+
load(
3+
"@bazel_tools//tools/jdk:default_java_toolchain.bzl",
4+
"BASE_JDK9_JVM_OPTS",
5+
"DEFAULT_JAVACOPTS",
6+
"DEFAULT_TOOLCHAIN_CONFIGURATION",
7+
"default_java_toolchain",
8+
)
9+
10+
default_java_toolchain(
11+
name = "java21_toolchain",
12+
configuration = DEFAULT_TOOLCHAIN_CONFIGURATION,
13+
java_runtime = select({
14+
"@platforms//os:linux": "@remotejdk21_linux//:jdk",
15+
"@platforms//os:macos": "@remotejdk21_macos//:jdk",
16+
"@platforms//os:windows": "@remotejdk21_win//:jdk",
17+
}),
18+
javacopts = DEFAULT_JAVACOPTS,
19+
jvm_opts = BASE_JDK9_JVM_OPTS,
20+
source_version = "21",
21+
target_version = "21",
22+
visibility = ["//visibility:public"],
23+
)
224

325
scala_toolchain(
426
name = "ast_plus_one_deps_unused_deps_warn_impl",

test/toolchains/jdk.bzl

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
2+
load("@bazel_tools//tools/jdk:remote_java_repository.bzl", "remote_java_repository")
3+
4+
def remote_jdk21_repositories():
5+
maybe(
6+
remote_java_repository,
7+
name = "remotejdk21_linux",
8+
target_compatible_with = [
9+
"@platforms//os:linux",
10+
"@platforms//cpu:x86_64",
11+
],
12+
sha256 = "5ad730fbee6bb49bfff10bf39e84392e728d89103d3474a7e5def0fd134b300a",
13+
strip_prefix = "zulu21.32.17-ca-jdk21.0.2-linux_x64",
14+
urls = [
15+
"https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu21.32.17-ca-jdk21.0.2-linux_x64.tar.gz",
16+
"https://cdn.azul.com/zulu/bin/zulu21.32.17-ca-jdk21.0.2-linux_x64.tar.gz",
17+
],
18+
version = "21",
19+
)
20+
21+
maybe(
22+
remote_java_repository,
23+
name = "remotejdk21_macos",
24+
target_compatible_with = [
25+
"@platforms//os:macos",
26+
"@platforms//cpu:x86_64",
27+
],
28+
sha256 = "3ad8fe288eb57d975c2786ae453a036aa46e47ab2ac3d81538ebae2a54d3c025",
29+
strip_prefix = "zulu21.32.17-ca-jdk21.0.2-macosx_x64",
30+
urls = [
31+
"https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu21.32.17-ca-jdk21.0.2-macosx_x64.tar.gz",
32+
"https://cdn.azul.com/zulu/bin/zulu21.32.17-ca-jdk21.0.2-macosx_x64.tar.gz",
33+
],
34+
version = "21",
35+
)
36+
37+
maybe(
38+
remote_java_repository,
39+
name = "remotejdk21_win",
40+
target_compatible_with = [
41+
"@platforms//os:windows",
42+
"@platforms//cpu:x86_64",
43+
],
44+
sha256 = "f7cc15ca17295e69c907402dfe8db240db446e75d3b150da7bf67243cded93de",
45+
strip_prefix = "zulu21.32.17-ca-jdk21.0.2-win_x64",
46+
urls = [
47+
"https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu21.32.17-ca-jdk21.0.2-win_x64.zip",
48+
"https://cdn.azul.com/zulu/bin/zulu21.32.17-ca-jdk21.0.2-win_x64.zip",
49+
],
50+
version = "21",
51+
)
52+
53+
def remote_jdk21_toolchains():
54+
native.register_toolchains("//test/toolchains:java21_toolchain_definition")

twitter_scrooge/twitter_scrooge.bzl

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ load(
1313
)
1414
load(
1515
"//scala/private:rule_impls.bzl",
16+
"allow_security_manager",
1617
"compile_java",
1718
"compile_scala",
1819
)
@@ -205,7 +206,7 @@ def _generate_jvm_code(ctx, label, compile_thrifts, include_thrifts, jar_output,
205206
# be correctly handled since the executable is a jvm app that will
206207
# consume the flags on startup.
207208
#arguments = ["--jvm_flag=%s" % flag for flag in ctx.attr.jvm_flags] +
208-
arguments = ["@" + argfile.path],
209+
arguments = ["--jvm_flag=%s" % f for f in allow_security_manager(ctx)] + ["@" + argfile.path],
209210
)
210211

211212
def _compiled_jar_file(actions, scrooge_jar):
@@ -434,6 +435,7 @@ common_attrs = {
434435
),
435436
],
436437
),
438+
"_java_host_runtime": attr.label(default = Label("@bazel_tools//tools/jdk:current_host_java_runtime")),
437439
}
438440

439441
common_aspect_providers = [

0 commit comments

Comments
 (0)