Skip to content

Commit 07400c0

Browse files
AustinSchuhcopybara-github
authored andcommitted
Add --{no,}autodetect_server_javabase.
We want bazel to fail to start instead of falling back to a host JRE/JDK. We are using a hermetic JDK and the embedded JRE, so there should be no need to use anything from the host. We've debugged enough cases so far where the host installed JDK was buggy and causing random crashes on specific machines. Fixes: #12451 Closes #12542. PiperOrigin-RevId: 347411720
1 parent ddf95df commit 07400c0

File tree

11 files changed

+156
-1
lines changed

11 files changed

+156
-1
lines changed

site/docs/user-manual.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,6 +2836,14 @@ <h4 id='flag--host_jvm_debug'><code class='flag'>--host_jvm_debug</code></h4>
28362836
subprocesses of Bazel: applications, tests, tools, etc.)
28372837
</p>
28382838

2839+
<h4 id='flag--autodetect_server_javabase'><code class='flag'>--autodetect_server_javabase</code></h4>
2840+
<p>
2841+
This option causes Bazel to automatically search for an installed JDK on startup,
2842+
and to fall back to the installed JRE if the embedded JRE isn't available.
2843+
<code>--explicit_server_javabase</code> can be used to pick an explicit JRE to
2844+
run bazel with.
2845+
</p>
2846+
28392847
<h4 id='flag--batch'><code class='flag'>--batch</code></h4>
28402848

28412849
<p>

src/main/cpp/blaze.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,9 @@ static vector<string> GetServerExeArgs(const blaze_util::Path &jvm_path,
458458
startup_options.output_base.AsCommandLineArgument());
459459
result.push_back("--workspace_directory=" +
460460
blaze_util::ConvertPath(workspace));
461-
result.push_back("--default_system_javabase=" + GetSystemJavabase());
461+
if (startup_options.autodetect_server_javabase) {
462+
result.push_back("--default_system_javabase=" + GetSystemJavabase());
463+
}
462464

463465
if (!startup_options.server_jvm_out.IsEmpty()) {
464466
result.push_back("--server_jvm_out=" +

src/main/cpp/startup_options.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ StartupOptions::StartupOptions(const string &product_name,
7171
ignore_all_rc_files(false),
7272
block_for_lock(true),
7373
host_jvm_debug(false),
74+
autodetect_server_javabase(true),
7475
batch(false),
7576
batch_cpu_scheduling(false),
7677
io_nice_level(-1),
@@ -137,6 +138,8 @@ StartupOptions::StartupOptions(const string &product_name,
137138
RegisterNullaryStartupFlag("fatal_event_bus_exceptions",
138139
&fatal_event_bus_exceptions);
139140
RegisterNullaryStartupFlag("host_jvm_debug", &host_jvm_debug);
141+
RegisterNullaryStartupFlag("autodetect_server_javabase",
142+
&autodetect_server_javabase);
140143
RegisterNullaryStartupFlag("idle_server_tasks", &idle_server_tasks);
141144
RegisterNullaryStartupFlag("incompatible_enable_execution_transition",
142145
&incompatible_enable_execution_transition);
@@ -483,6 +486,10 @@ StartupOptions::GetServerJavabaseAndType() const {
483486
// 2) Use a bundled JVM if we have one.
484487
default_server_javabase_ = std::pair<blaze_util::Path, JavabaseType>(
485488
bundled_jre_path, JavabaseType::EMBEDDED);
489+
} else if (!autodetect_server_javabase) {
490+
BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR)
491+
<< "Could not find embedded or explicit server javabase, and "
492+
"--noautodetect_server_javabase is set.";
486493
} else {
487494
// 3) Otherwise fall back to using the default system JVM.
488495
blaze_util::Path system_javabase = GetSystemJavabase();

src/main/cpp/startup_options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ class StartupOptions {
165165

166166
bool host_jvm_debug;
167167

168+
bool autodetect_server_javabase;
169+
168170
std::string host_jvm_profile;
169171

170172
std::vector<std::string> host_jvm_args;

src/main/java/com/google/devtools/build/lib/runtime/BlazeServerStartupOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,4 +501,14 @@ public String getTypeDescription() {
501501
+ "extended attribute is checked on all source files and output files, meaning "
502502
+ "that it causes a significant number of invocations of the getxattr() system call.")
503503
public String unixDigestHashAttributeName;
504+
505+
@Option(
506+
name = "autodetect_server_javabase",
507+
defaultValue = "true", // NOTE: only for documentation, value never passed to the server.
508+
documentationCategory = OptionDocumentationCategory.BAZEL_CLIENT_OPTIONS,
509+
effectTags = {OptionEffectTag.AFFECTS_OUTPUTS, OptionEffectTag.LOSES_INCREMENTAL_STATE},
510+
help =
511+
"When --noautodetect_server_javabase is passed, Bazel does not fall back to the local "
512+
+ "JDK for running the bazel server and instead exits.")
513+
public boolean autodetectServerJavabase;
504514
}

src/test/cpp/bazel_startup_options_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ TEST_F(BazelStartupOptionsTest, ValidStartupFlags) {
100100
ExpectValidNullaryOption(options, "fatal_event_bus_exceptions");
101101
ExpectValidNullaryOption(options, "home_rc");
102102
ExpectValidNullaryOption(options, "host_jvm_debug");
103+
ExpectValidNullaryOption(options, "autodetect_server_javabase");
103104
ExpectValidNullaryOption(options, "ignore_all_rc_files");
104105
ExpectValidNullaryOption(options, "incompatible_enable_execution_transition");
105106
ExpectValidNullaryOption(options, "master_bazelrc");

src/test/shell/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package(default_visibility = ["//visibility:private"])
55
exports_files([
66
"bin/bazel",
77
"bin/bazel_jdk_minimal",
8+
"bin/bazel_nojdk",
89
"testenv.sh",
910
"integration_test_setup.sh",
1011
"sandboxing_test_utils.sh",

src/test/shell/bin/bazel_nojdk

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2020 The Bazel Authors. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
# Wrapper script to run bazel in the tests. Any change to this file will
18+
# affect all our integration tests.
19+
#
20+
exec $(rlocation io_bazel/src/bazel_nojdk) \
21+
--bazelrc=$TEST_TMPDIR/bazelrc "$@"

src/test/shell/integration/BUILD

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ filegroup(
2626
],
2727
)
2828

29+
filegroup(
30+
name = "test-deps-nojdk",
31+
testonly = 1,
32+
srcs = [
33+
"//src:bazel-bin_nojdk",
34+
"//src/test/shell:bin/bazel_nojdk",
35+
"//src/test/shell/bazel:test-deps-wo-bazel",
36+
],
37+
)
38+
2939
sh_test(
3040
name = "progress_reporting_test",
3141
size = "large",
@@ -185,6 +195,18 @@ sh_test(
185195
],
186196
)
187197

198+
sh_test(
199+
name = "nojdk_startup_options_test",
200+
size = "medium",
201+
srcs = ["nojdk_startup_options_test.sh"],
202+
data = [
203+
":test-deps-nojdk",
204+
"@bazel_tools//tools/bash/runfiles",
205+
],
206+
# Windows doesn't support sandboxing, which BAZEL_SUFFIX needs.
207+
tags = ["no_windows"],
208+
)
209+
188210
sh_test(
189211
name = "run_test",
190212
size = "medium",
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2020 The Bazel Authors. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
# Test of Bazel's startup option handling cof the nojdk version.
18+
19+
# --- begin runfiles.bash initialization ---
20+
set -euo pipefail
21+
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
22+
if [[ -f "$0.runfiles_manifest" ]]; then
23+
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
24+
elif [[ -f "$0.runfiles/MANIFEST" ]]; then
25+
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
26+
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
27+
export RUNFILES_DIR="$0.runfiles"
28+
fi
29+
fi
30+
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
31+
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
32+
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
33+
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
34+
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
35+
else
36+
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
37+
exit 1
38+
fi
39+
# --- end runfiles.bash initialization ---
40+
41+
# We don't want to use the cached, extracted bazel. It will have a different
42+
# sha1 and fail the test. The 2 version commands below are cheap.
43+
unset TEST_INSTALL_BASE
44+
45+
export BAZEL_SUFFIX="_nojdk"
46+
source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \
47+
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; }
48+
49+
case "$(uname -s | tr [:upper:] [:lower:])" in
50+
msys*|mingw*|cygwin*)
51+
declare -r is_windows=true
52+
;;
53+
*)
54+
declare -r is_windows=false
55+
;;
56+
esac
57+
58+
if "$is_windows"; then
59+
export MSYS_NO_PATHCONV=1
60+
export MSYS2_ARG_CONV_EXCL="*"
61+
fi
62+
63+
# Test that nojdk bazel works with --autodetect_server_javabase
64+
function test_autodetect_server_javabase() {
65+
bazel --autodetect_server_javabase version &> $TEST_log || fail "Should pass"
66+
}
67+
68+
# Test that nojdk bazel fails with --noautodetect_server_javabase
69+
function test_noautodetect_server_javabase() {
70+
bazel --noautodetect_server_javabase version &> $TEST_log && fail "Should fail"
71+
expect_log "FATAL: Could not find embedded or explicit server javabase, and --noautodetect_server_javabase is set."
72+
}
73+
74+
run_suite "${PRODUCT_NAME} startup options test"

src/test/shell/integration/startup_options_test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,11 @@ function test_command_args_are_not_parsed_as_startup_args() {
7575
expect_not_log "Error: Unable to read .bazelrc file"
7676
}
7777

78+
# Test that normal bazel works with and without --autodetect_server_javabase
79+
# because it has an embedded JRE.
80+
function test_autodetect_server_javabase() {
81+
bazel --autodetect_server_javabase version &> $TEST_log || fail "Should pass"
82+
bazel --noautodetect_server_javabase version &> $TEST_log || fail "Should pass"
83+
}
84+
7885
run_suite "${PRODUCT_NAME} startup options test"

0 commit comments

Comments
 (0)