Skip to content

Commit

Permalink
Add --{no,}autodetect_server_javabase.
Browse files Browse the repository at this point in the history
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: bazelbuild#12451

Closes bazelbuild#12542.

PiperOrigin-RevId: 347411720
  • Loading branch information
AustinSchuh authored and Copybara-Service committed Dec 14, 2020
1 parent ddf95df commit 07400c0
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 1 deletion.
8 changes: 8 additions & 0 deletions site/docs/user-manual.html
Expand Up @@ -2836,6 +2836,14 @@ <h4 id='flag--host_jvm_debug'><code class='flag'>--host_jvm_debug</code></h4>
subprocesses of Bazel: applications, tests, tools, etc.)
</p>

<h4 id='flag--autodetect_server_javabase'><code class='flag'>--autodetect_server_javabase</code></h4>
<p>
This option causes Bazel to automatically search for an installed JDK on startup,
and to fall back to the installed JRE if the embedded JRE isn't available.
<code>--explicit_server_javabase</code> can be used to pick an explicit JRE to
run bazel with.
</p>

<h4 id='flag--batch'><code class='flag'>--batch</code></h4>

<p>
Expand Down
4 changes: 3 additions & 1 deletion src/main/cpp/blaze.cc
Expand Up @@ -458,7 +458,9 @@ static vector<string> GetServerExeArgs(const blaze_util::Path &jvm_path,
startup_options.output_base.AsCommandLineArgument());
result.push_back("--workspace_directory=" +
blaze_util::ConvertPath(workspace));
result.push_back("--default_system_javabase=" + GetSystemJavabase());
if (startup_options.autodetect_server_javabase) {
result.push_back("--default_system_javabase=" + GetSystemJavabase());
}

if (!startup_options.server_jvm_out.IsEmpty()) {
result.push_back("--server_jvm_out=" +
Expand Down
7 changes: 7 additions & 0 deletions src/main/cpp/startup_options.cc
Expand Up @@ -71,6 +71,7 @@ StartupOptions::StartupOptions(const string &product_name,
ignore_all_rc_files(false),
block_for_lock(true),
host_jvm_debug(false),
autodetect_server_javabase(true),
batch(false),
batch_cpu_scheduling(false),
io_nice_level(-1),
Expand Down Expand Up @@ -137,6 +138,8 @@ StartupOptions::StartupOptions(const string &product_name,
RegisterNullaryStartupFlag("fatal_event_bus_exceptions",
&fatal_event_bus_exceptions);
RegisterNullaryStartupFlag("host_jvm_debug", &host_jvm_debug);
RegisterNullaryStartupFlag("autodetect_server_javabase",
&autodetect_server_javabase);
RegisterNullaryStartupFlag("idle_server_tasks", &idle_server_tasks);
RegisterNullaryStartupFlag("incompatible_enable_execution_transition",
&incompatible_enable_execution_transition);
Expand Down Expand Up @@ -483,6 +486,10 @@ StartupOptions::GetServerJavabaseAndType() const {
// 2) Use a bundled JVM if we have one.
default_server_javabase_ = std::pair<blaze_util::Path, JavabaseType>(
bundled_jre_path, JavabaseType::EMBEDDED);
} else if (!autodetect_server_javabase) {
BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR)
<< "Could not find embedded or explicit server javabase, and "
"--noautodetect_server_javabase is set.";
} else {
// 3) Otherwise fall back to using the default system JVM.
blaze_util::Path system_javabase = GetSystemJavabase();
Expand Down
2 changes: 2 additions & 0 deletions src/main/cpp/startup_options.h
Expand Up @@ -165,6 +165,8 @@ class StartupOptions {

bool host_jvm_debug;

bool autodetect_server_javabase;

std::string host_jvm_profile;

std::vector<std::string> host_jvm_args;
Expand Down
Expand Up @@ -501,4 +501,14 @@ public String getTypeDescription() {
+ "extended attribute is checked on all source files and output files, meaning "
+ "that it causes a significant number of invocations of the getxattr() system call.")
public String unixDigestHashAttributeName;

@Option(
name = "autodetect_server_javabase",
defaultValue = "true", // NOTE: only for documentation, value never passed to the server.
documentationCategory = OptionDocumentationCategory.BAZEL_CLIENT_OPTIONS,
effectTags = {OptionEffectTag.AFFECTS_OUTPUTS, OptionEffectTag.LOSES_INCREMENTAL_STATE},
help =
"When --noautodetect_server_javabase is passed, Bazel does not fall back to the local "
+ "JDK for running the bazel server and instead exits.")
public boolean autodetectServerJavabase;
}
1 change: 1 addition & 0 deletions src/test/cpp/bazel_startup_options_test.cc
Expand Up @@ -100,6 +100,7 @@ TEST_F(BazelStartupOptionsTest, ValidStartupFlags) {
ExpectValidNullaryOption(options, "fatal_event_bus_exceptions");
ExpectValidNullaryOption(options, "home_rc");
ExpectValidNullaryOption(options, "host_jvm_debug");
ExpectValidNullaryOption(options, "autodetect_server_javabase");
ExpectValidNullaryOption(options, "ignore_all_rc_files");
ExpectValidNullaryOption(options, "incompatible_enable_execution_transition");
ExpectValidNullaryOption(options, "master_bazelrc");
Expand Down
1 change: 1 addition & 0 deletions src/test/shell/BUILD
Expand Up @@ -5,6 +5,7 @@ package(default_visibility = ["//visibility:private"])
exports_files([
"bin/bazel",
"bin/bazel_jdk_minimal",
"bin/bazel_nojdk",
"testenv.sh",
"integration_test_setup.sh",
"sandboxing_test_utils.sh",
Expand Down
21 changes: 21 additions & 0 deletions src/test/shell/bin/bazel_nojdk
@@ -0,0 +1,21 @@
#!/bin/bash
#
# Copyright 2020 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Wrapper script to run bazel in the tests. Any change to this file will
# affect all our integration tests.
#
exec $(rlocation io_bazel/src/bazel_nojdk) \
--bazelrc=$TEST_TMPDIR/bazelrc "$@"
22 changes: 22 additions & 0 deletions src/test/shell/integration/BUILD
Expand Up @@ -26,6 +26,16 @@ filegroup(
],
)

filegroup(
name = "test-deps-nojdk",
testonly = 1,
srcs = [
"//src:bazel-bin_nojdk",
"//src/test/shell:bin/bazel_nojdk",
"//src/test/shell/bazel:test-deps-wo-bazel",
],
)

sh_test(
name = "progress_reporting_test",
size = "large",
Expand Down Expand Up @@ -185,6 +195,18 @@ sh_test(
],
)

sh_test(
name = "nojdk_startup_options_test",
size = "medium",
srcs = ["nojdk_startup_options_test.sh"],
data = [
":test-deps-nojdk",
"@bazel_tools//tools/bash/runfiles",
],
# Windows doesn't support sandboxing, which BAZEL_SUFFIX needs.
tags = ["no_windows"],
)

sh_test(
name = "run_test",
size = "medium",
Expand Down
74 changes: 74 additions & 0 deletions src/test/shell/integration/nojdk_startup_options_test.sh
@@ -0,0 +1,74 @@
#!/bin/bash
#
# Copyright 2020 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Test of Bazel's startup option handling cof the nojdk version.

# --- begin runfiles.bash initialization ---
set -euo pipefail
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
if [[ -f "$0.runfiles_manifest" ]]; then
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
elif [[ -f "$0.runfiles/MANIFEST" ]]; then
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
export RUNFILES_DIR="$0.runfiles"
fi
fi
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
else
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
exit 1
fi
# --- end runfiles.bash initialization ---

# We don't want to use the cached, extracted bazel. It will have a different
# sha1 and fail the test. The 2 version commands below are cheap.
unset TEST_INSTALL_BASE

export BAZEL_SUFFIX="_nojdk"
source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; }

case "$(uname -s | tr [:upper:] [:lower:])" in
msys*|mingw*|cygwin*)
declare -r is_windows=true
;;
*)
declare -r is_windows=false
;;
esac

if "$is_windows"; then
export MSYS_NO_PATHCONV=1
export MSYS2_ARG_CONV_EXCL="*"
fi

# Test that nojdk bazel works with --autodetect_server_javabase
function test_autodetect_server_javabase() {
bazel --autodetect_server_javabase version &> $TEST_log || fail "Should pass"
}

# Test that nojdk bazel fails with --noautodetect_server_javabase
function test_noautodetect_server_javabase() {
bazel --noautodetect_server_javabase version &> $TEST_log && fail "Should fail"
expect_log "FATAL: Could not find embedded or explicit server javabase, and --noautodetect_server_javabase is set."
}

run_suite "${PRODUCT_NAME} startup options test"
7 changes: 7 additions & 0 deletions src/test/shell/integration/startup_options_test.sh
Expand Up @@ -75,4 +75,11 @@ function test_command_args_are_not_parsed_as_startup_args() {
expect_not_log "Error: Unable to read .bazelrc file"
}

# Test that normal bazel works with and without --autodetect_server_javabase
# because it has an embedded JRE.
function test_autodetect_server_javabase() {
bazel --autodetect_server_javabase version &> $TEST_log || fail "Should pass"
bazel --noautodetect_server_javabase version &> $TEST_log || fail "Should pass"
}

run_suite "${PRODUCT_NAME} startup options test"

0 comments on commit 07400c0

Please sign in to comment.