Skip to content

Commit f5b6abc

Browse files
comiuscopybara-github
authored andcommitted
Fix Java tests to use Java toolchain resolution.
The flag is flipped for tests, old configuration options `javabase` and `java_toolchain` are marked deprecated. All the tests are fixed to execute using new toolchain resolution. Changed `local_java_repository` to generate `java_runtime` with `java_home` parameter set (instead of srcs). This behaves better, because `java_stub_template.txt` then does not need runfiles to execute produced `deploy.jar`. Kept the same version with `srcs` in the `jdk.WORKSPACE`. Related to bazelbuild#4592. Closes bazelbuild#12695. PiperOrigin-RevId: 347969628
1 parent 467e1d4 commit f5b6abc

19 files changed

+99
-183
lines changed

src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ maybe(
88
local_java_repository,
99
name = "local_jdk",
1010
java_home = DEFAULT_SYSTEM_JAVABASE,
11+
build_file = "@bazel_tools//tools/jdk:jdk.BUILD",
1112
)
1213

1314
# OpenJDK distributions that should only be downloaded on demand (e.g. when

src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ public ImportDepsCheckingLevelConverter() {
7474

7575
private static final String DEFAULT_JAVABASE = "@bazel_tools//tools/jdk:jdk";
7676

77+
@Deprecated
7778
@Option(
7879
name = "javabase",
7980
defaultValue = DEFAULT_JAVABASE,
8081
converter = LabelConverter.class,
81-
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
82+
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
8283
effectTags = {OptionEffectTag.UNKNOWN},
8384
help =
8485
"JAVABASE used for the JDK invoked by Blaze. This is the "
@@ -88,29 +89,32 @@ public ImportDepsCheckingLevelConverter() {
8889

8990
private static final String DEFAULT_JAVA_TOOLCHAIN = "@bazel_tools//tools/jdk:remote_toolchain";
9091

92+
@Deprecated
9193
@Option(
9294
name = "java_toolchain",
9395
defaultValue = DEFAULT_JAVA_TOOLCHAIN,
9496
converter = LabelConverter.class,
95-
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
97+
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
9698
effectTags = {OptionEffectTag.UNKNOWN},
9799
help = "The name of the toolchain rule for Java.")
98100
public Label javaToolchain;
99101

102+
@Deprecated
100103
@Option(
101104
name = "host_java_toolchain",
102105
defaultValue = DEFAULT_JAVA_TOOLCHAIN,
103106
converter = LabelConverter.class,
104-
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
107+
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
105108
effectTags = {OptionEffectTag.UNKNOWN},
106109
help = "The Java toolchain used to build tools that are executed during a build.")
107110
public Label hostJavaToolchain;
108111

112+
@Deprecated
109113
@Option(
110114
name = "host_javabase",
111115
defaultValue = "null",
112116
converter = LabelConverter.class,
113-
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
117+
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
114118
effectTags = {OptionEffectTag.UNKNOWN},
115119
help =
116120
"JAVABASE used for the host JDK. This is the java_runtime which is used to execute "
@@ -629,31 +633,31 @@ public ImportDepsCheckingLevelConverter() {
629633
@Option(
630634
name = "java_runtime_version",
631635
defaultValue = "local_jdk",
632-
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
636+
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
633637
effectTags = {OptionEffectTag.UNKNOWN},
634638
help = "The Java runtime version")
635639
public String javaRuntimeVersion;
636640

637641
@Option(
638642
name = "tool_java_runtime_version",
639643
defaultValue = "remotejdk_11",
640-
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
644+
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
641645
effectTags = {OptionEffectTag.UNKNOWN},
642646
help = "The Java runtime version used to execute tools during the build")
643647
public String hostJavaRuntimeVersion;
644648

645649
@Option(
646650
name = "java_language_version",
647651
defaultValue = "8",
648-
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
652+
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
649653
effectTags = {OptionEffectTag.UNKNOWN},
650654
help = "The Java language version")
651655
public String javaLanguageVersion;
652656

653657
@Option(
654658
name = "tool_java_language_version",
655659
defaultValue = "8",
656-
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
660+
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
657661
effectTags = {OptionEffectTag.UNKNOWN},
658662
help = "The Java language version used to build tools that are executed during a build")
659663
public String hostJavaLanguageVersion;

src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ public void testJavaRuntimeProviderJavaAbsolute() throws Exception {
105105
" )",
106106
"jrule = rule(_impl, attrs = { '_java_runtime': attr.label(default=Label('//a:alias'))})");
107107

108-
useConfiguration(
109-
"--javabase=//a:jvm", "--extra_toolchains=//a:all", "--platforms=//a:platform");
108+
useConfiguration("--extra_toolchains=//a:all", "--platforms=//a:platform");
110109
ConfiguredTarget ct = getConfiguredTarget("//a:r");
111110
StructImpl myInfo = getMyInfoFromTarget(ct);
112111
String javaHomeExecPath = (String) myInfo.getValue("java_home_exec_path");
@@ -162,8 +161,7 @@ public void testJavaRuntimeProviderJavaHermetic() throws Exception {
162161
" )",
163162
"jrule = rule(_impl, attrs = { '_java_runtime': attr.label(default=Label('//a:alias'))})");
164163

165-
useConfiguration(
166-
"--javabase=//a:jvm", "--extra_toolchains=//a:all", "--platforms=//a:platform");
164+
useConfiguration("--extra_toolchains=//a:all", "--platforms=//a:platform");
167165
ConfiguredTarget ct = getConfiguredTarget("//a:r");
168166
StructImpl myInfo = getMyInfoFromTarget(ct);
169167
String javaHomeExecPath = (String) myInfo.getValue("java_home_exec_path");
@@ -220,8 +218,7 @@ public void testJavaRuntimeProviderJavaGenerated() throws Exception {
220218
" )",
221219
"jrule = rule(_impl, attrs = { '_java_runtime': attr.label(default=Label('//a:alias'))})");
222220

223-
useConfiguration(
224-
"--javabase=//a:jvm", "--extra_toolchains=//a:all", "--platforms=//a:platform");
221+
useConfiguration("--extra_toolchains=//a:all", "--platforms=//a:platform");
225222
// TODO(b/129637690): the runtime shouldn't be resolved in the host config
226223
ConfiguredTarget genrule = getHostConfiguredTarget("//a:gen");
227224
ConfiguredTarget ct = getConfiguredTarget("//a:r");
@@ -1803,7 +1800,6 @@ public void javaToolchainFlag_set() throws Exception {
18031800
"java_toolchain_alias(name='alias')",
18041801
"myrule(name='myrule')");
18051802
useConfiguration(
1806-
"--java_toolchain=//java/com/google/test:toolchain",
18071803
"--extra_toolchains=//java/com/google/test:all",
18081804
"--platforms=//java/com/google/test:platform");
18091805
ConfiguredTarget configuredTarget = getConfiguredTarget("//foo:myrule");
@@ -2194,8 +2190,7 @@ public void testJavaRuntimeProviderFiles() throws Exception {
21942190
" )",
21952191
"jrule = rule(_impl, attrs = { '_java_runtime': attr.label(default=Label('//a:alias'))})");
21962192

2197-
useConfiguration(
2198-
"--javabase=//a:jvm", "--extra_toolchains=//a:all", "--platforms=//a:platform");
2193+
useConfiguration("--extra_toolchains=//a:all", "--platforms=//a:platform");
21992194
ConfiguredTarget ct = getConfiguredTarget("//a:r");
22002195
Depset files = (Depset) ct.get("files");
22012196
assertThat(prettyArtifactNames(files.toList(Artifact.class))).containsExactly("a/a.txt");

src/test/py/bazel/windows_remote_test.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ def testJavaTestRunsRemotely(self):
173173
# it elsewhere, add --test_env=JAVA_HOME to your Bazel invocation to fix this
174174
# test.
175175
def testJavaTestWithRuntimeRunsRemotely(self):
176-
java_home = os.getenv('JAVA_HOME', 'c:/openjdk')
177176
self.CreateWorkspaceWithDefaultRepos('WORKSPACE')
178177
self.ScratchFile('foo/BUILD', [
179178
'package(default_visibility = ["//visibility:public"])',
@@ -184,11 +183,6 @@ def testJavaTestWithRuntimeRunsRemotely(self):
184183
' use_testrunner = 0,',
185184
' data = ["//bar:bar.txt"],',
186185
')',
187-
'java_runtime(',
188-
' name = "jdk8",',
189-
' srcs = [],',
190-
' java_home = ' + repr(java_home) + ',',
191-
')',
192186
])
193187
self.ScratchFile(
194188
'foo/TestFoo.java', [
@@ -203,9 +197,8 @@ def testJavaTestWithRuntimeRunsRemotely(self):
203197
self.ScratchFile('bar/bar.txt', ['hello'])
204198

205199
# Test.
206-
exit_code, stdout, stderr = self._RunRemoteBazel([
207-
'test', '--test_output=all', '--javabase=//foo:jdk8', '//foo:foo_test'
208-
])
200+
exit_code, stdout, stderr = self._RunRemoteBazel(
201+
['test', '--test_output=all', '//foo:foo_test'])
209202
self.AssertExitCode(exit_code, 0, stderr, stdout)
210203

211204
# Genrules are notably different than tests because RUNFILES_DIR is not set

src/test/shell/bazel/BUILD

+13-78
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,10 @@ sh_test(
182182
name = "bazel_java14_test",
183183
srcs = ["bazel_java14_test.sh"],
184184
args = [
185-
# --java_toolchain and --host_java_toolchain values
186-
"@bazel_tools//tools/jdk:toolchain_jdk_14",
187185
# java_tools zip to test
188186
"src/java_tools.zip",
189187
"src/java_tools_prebuilt.zip",
190-
] + select({
191-
# --javabase and --host_javabase values
192-
"//src/conditions:darwin": ["@openjdk14_darwin_archive//:runtime"],
193-
"//src/conditions:windows": ["@openjdk14_windows_archive//:runtime"],
194-
"//src/conditions:linux": ["@openjdk14_linux_archive//:runtime"],
195-
}),
188+
],
196189
data = [
197190
":test-deps",
198191
"//src:java_tools_prebuilt_zip",
@@ -207,17 +200,10 @@ sh_test(
207200
name = "bazel_java15_test",
208201
srcs = ["bazel_java15_test.sh"],
209202
args = [
210-
# --java_toolchain and --host_java_toolchain values
211-
"@bazel_tools//tools/jdk:toolchain_jdk_15",
212203
# java_tools zip to test
213204
"src/java_tools.zip",
214205
"src/java_tools_prebuilt.zip",
215-
] + select({
216-
# --javabase and --host_javabase values
217-
"//src/conditions:darwin": ["@openjdk15_darwin_archive//:runtime"],
218-
"//src/conditions:windows": ["@openjdk15_windows_archive//:runtime"],
219-
"//src/conditions:linux": ["@openjdk15_linux_archive//:runtime"],
220-
}),
206+
],
221207
data = [
222208
":test-deps",
223209
"//src:java_tools_prebuilt_zip",
@@ -235,7 +221,6 @@ sh_test(
235221
timeout = "eternal",
236222
srcs = ["bazel_java_test.sh"],
237223
args = [
238-
"@bazel_tools//tools/jdk:toolchain",
239224
"released",
240225
"released",
241226
],
@@ -256,47 +241,12 @@ JAVA_VERSIONS = ("11", "14", "15")
256241
timeout = "eternal",
257242
srcs = ["bazel_java_test.sh"],
258243
args = [
259-
# --java_toolchain
260-
"@bazel_tools//tools/jdk:toolchain",
261244
# java_tools zips to test
262245
"src/java_tools.zip",
263246
"src/java_tools_prebuilt.zip",
264-
# --javabase value
265-
] + select({
266-
"//src/conditions:darwin": ["@openjdk" + java_version + "_darwin_archive//:runtime"],
267-
"//src/conditions:windows": ["@openjdk" + java_version + "_windows_archive//:runtime"],
268-
"//src/conditions:linux": ["@openjdk" + java_version + "_linux_archive//:runtime"],
269-
}),
270-
data = [
271-
":test-deps",
272-
"//src:java_tools_prebuilt_zip",
273-
"//src:java_tools_zip",
274-
"//src/test/shell/bazel/testdata:jdk_http_archives_filegroup",
275-
"@bazel_tools//tools/bash/runfiles",
247+
# --java_runtime_version value
248+
java_version,
276249
],
277-
exec_compatible_with = ["//:highcpu_machine"],
278-
)
279-
for java_version in JAVA_VERSIONS
280-
]
281-
282-
[
283-
sh_test(
284-
name = "bazel_java_test_jdk" + java_version + "_prebuilt_toolchain_head",
285-
size = "large",
286-
timeout = "eternal",
287-
srcs = ["bazel_java_test.sh"],
288-
args = [
289-
# --java_toolchain
290-
"@bazel_tools//tools/jdk:prebuilt_toolchain",
291-
# java_tools zips to test
292-
"src/java_tools.zip",
293-
"src/java_tools_prebuilt.zip",
294-
# --javabase value
295-
] + select({
296-
"//src/conditions:darwin": ["@openjdk" + java_version + "_darwin_archive//:runtime"],
297-
"//src/conditions:windows": ["@openjdk" + java_version + "_windows_archive//:runtime"],
298-
"//src/conditions:linux": ["@openjdk" + java_version + "_linux_archive//:runtime"],
299-
}),
300250
data = [
301251
":test-deps",
302252
"//src:java_tools_prebuilt_zip",
@@ -316,17 +266,12 @@ JAVA_VERSIONS = ("11", "14", "15")
316266
timeout = "eternal",
317267
srcs = ["bazel_java_test.sh"],
318268
args = [
319-
# --java_toolchain
320-
"@bazel_tools//tools/jdk:toolchain",
321269
# java_tools zips to test
322270
"$(LOCAL_JAVA_TOOLS_ZIP_URL)",
323271
"$(LOCAL_JAVA_TOOLS_PREBUILT_ZIP_URL)",
324-
# --javabase value
325-
] + select({
326-
"//src/conditions:darwin": ["@openjdk" + java_version + "_darwin_archive//:runtime"],
327-
"//src/conditions:windows": ["@openjdk" + java_version + "_windows_archive//:runtime"],
328-
"//src/conditions:linux": ["@openjdk" + java_version + "_linux_archive//:runtime"],
329-
}),
272+
# --java_runtime_version value
273+
java_version,
274+
],
330275
data = [
331276
":test-deps",
332277
"//src/test/shell/bazel/testdata:jdk_http_archives_filegroup",
@@ -505,7 +450,6 @@ sh_test(
505450
name = "bazel_coverage_java_test",
506451
srcs = ["bazel_coverage_java_test.sh"],
507452
args = [
508-
"@bazel_tools//tools/jdk:toolchain",
509453
"released",
510454
"released",
511455
"released",
@@ -525,18 +469,14 @@ sh_test(
525469
name = "bazel_coverage_java_jdk" + java_version + "_toolchain_released_test",
526470
srcs = ["bazel_coverage_java_test.sh"],
527471
args = [
528-
"@bazel_tools//tools/jdk:toolchain",
529472
# java_tools zip to test
530473
"released",
531474
"released",
532475
# coverage_report_generator to test
533476
"released",
534-
# --javabase value
535-
] + select({
536-
"//src/conditions:darwin": ["@openjdk" + java_version + "_darwin_archive//:runtime"],
537-
"//src/conditions:windows": ["@openjdk" + java_version + "_windows_archive//:runtime"],
538-
"//src/conditions:linux": ["@openjdk" + java_version + "_linux_archive//:runtime"],
539-
}),
477+
# --java_runtime_version value
478+
java_version,
479+
],
540480
data = [
541481
":test-deps",
542482
"//src/test/shell/bazel/testdata:jdk_http_archives_filegroup",
@@ -554,19 +494,14 @@ sh_test(
554494
name = "bazel_coverage_java_jdk" + java_version + "_toolchain_head_test",
555495
srcs = ["bazel_coverage_java_test.sh"],
556496
args = [
557-
# --java_toolchain
558-
"@bazel_tools//tools/jdk:toolchain",
559497
# java_tools zips to test
560498
"src/java_tools.zip",
561499
"src/java_tools_prebuilt.zip",
562500
# coverage output generator to test
563501
"tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator/coverage",
564-
# --javabase value
565-
] + select({
566-
"//src/conditions:darwin": ["@openjdk" + java_version + "_darwin_archive//:runtime"],
567-
"//src/conditions:windows": ["@openjdk" + java_version + "_windows_archive//:runtime"],
568-
"//src/conditions:linux": ["@openjdk" + java_version + "_linux_archive//:runtime"],
569-
}),
502+
# --java_runtime_version value
503+
java_version,
504+
],
570505
data = [
571506
":test-deps",
572507
"//src:java_tools_prebuilt_zip",

src/test/shell/bazel/bazel_coverage_java_test.sh

+3-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2121
source "${CURRENT_DIR}/../integration_test_setup.sh" \
2222
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; }
2323

24-
JAVA_TOOLCHAIN="$1"; shift
25-
add_to_bazelrc "build --java_toolchain=${JAVA_TOOLCHAIN}"
26-
add_to_bazelrc "build --host_java_toolchain=${JAVA_TOOLCHAIN}"
2724

2825
JAVA_TOOLS_ZIP="$1"; shift
2926
if [[ "${JAVA_TOOLS_ZIP}" != "released" ]]; then
@@ -52,9 +49,9 @@ if [[ "${COVERAGE_GENERATOR_DIR}" != "released" ]]; then
5249
fi
5350

5451
if [[ $# -gt 0 ]]; then
55-
JAVABASE_VALUE="$1"; shift
56-
add_to_bazelrc "build --javabase=${JAVABASE_VALUE}"
57-
add_to_bazelrc "build --host_javabase=${JAVABASE_VALUE}"
52+
JAVA_RUNTIME_VERSION="$1"; shift
53+
add_to_bazelrc "build --java_runtime_version=${JAVA_RUNTIME_VERSION}"
54+
add_to_bazelrc "build --tool_java_runtime_version=${JAVA_RUNTIME_VERSION}"
5855
fi
5956

6057
function set_up() {

src/test/shell/bazel/bazel_example_test.sh

+5-4
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,12 @@ function test_java_test_with_junitrunner() {
105105
}
106106

107107
function test_genrule_and_genquery() {
108-
# The --javabase flag is to force the tools/jdk:jdk label to be used
109-
# so it appears in the dependency list.
110-
assert_build_output ./bazel-bin/examples/gen/genquery examples/gen:genquery --javabase=@bazel_tools//tools/jdk
108+
# With toolchain resolution java runtime only appears in cquery results.
109+
# //tools/jdk:jdk label appears in the dependency list while --javabase
110+
# is still available, because of migration rules.
111+
assert_build_output ./bazel-bin/examples/gen/genquery examples/gen:genquery
111112
local want=./bazel-genfiles/examples/gen/genrule.txt
112-
assert_build_output $want examples/gen:genrule --javabase=@bazel_tools//tools/jdk
113+
assert_build_output $want examples/gen:genrule
113114

114115
diff $want ./bazel-bin/examples/gen/genquery \
115116
|| fail "genrule and genquery output differs"

0 commit comments

Comments
 (0)