From 84eae2ff550c433a3d0409cf2b5525059939439d Mon Sep 17 00:00:00 2001 From: philwo Date: Tue, 17 Dec 2019 02:19:49 -0800 Subject: [PATCH] Let shellzelisk fallback to bazel-real if it's the requested version. This means that if you "apt-get install bazel" and that happens to be version "2.0.0" and in your `.bazelversion` file you have "2.0.0", then the new Bazel wrapper will just use the Bazel binary installed as the "latest" Bazel by "apt-get install bazel" and not ask the user to install the same version of Bazel as a side-by-side binary using "apt-get install bazel-2.0.0". The same applies when using the Bazel installer or any other setup that uses the wrapper script. Closes #10356. RELNOTES: None. PiperOrigin-RevId: 285941301 --- scripts/packages/bazel.sh | 13 +++++++++ src/test/shell/bazel/bazel_wrapper_test.sh | 31 ++++++++++++++++------ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/scripts/packages/bazel.sh b/scripts/packages/bazel.sh index 635c0591e6069b..a240dfa4cc3dfb 100755 --- a/scripts/packages/bazel.sh +++ b/scripts/packages/bazel.sh @@ -139,10 +139,23 @@ if [[ -z $bazel_version ]]; then fi BAZEL_REAL="${wrapper_dir}/bazel-${bazel_version}-${os_arch_suffix}" + +# Try without the architecture suffix. if [[ ! -x ${BAZEL_REAL} ]]; then BAZEL_REAL="${wrapper_dir}/bazel-${bazel_version}" fi +# Last try: Maybe `bazel-real` is actually the requested correct version? +readonly bazel_real_path="${wrapper_dir}/bazel-real" +if [[ ! -x ${BAZEL_REAL} && -x ${bazel_real_path} ]]; then + # Note that "bazel --version" is very fast and doesn't start the Bazel server, + # as opposed to "bazel version". + readonly bazel_real_version="$("${bazel_real_path}" --version | grep '^bazel ' | cut -d' ' -f2)" + if [[ $bazel_real_version == $bazel_version ]]; then + BAZEL_REAL="${bazel_real_path}" + fi +fi + if [[ ! -x $BAZEL_REAL ]]; then color "31" "ERROR: The project you're trying to build requires Bazel ${bazel_version} (${reason}), but it wasn't found in ${wrapper_dir}." diff --git a/src/test/shell/bazel/bazel_wrapper_test.sh b/src/test/shell/bazel/bazel_wrapper_test.sh index 84fce7564c1e9a..e1378e6d5761e6 100755 --- a/src/test/shell/bazel/bazel_wrapper_test.sh +++ b/src/test/shell/bazel/bazel_wrapper_test.sh @@ -45,13 +45,19 @@ set -e wrapper=$(rlocation io_bazel/scripts/packages/bazel.sh) mock_bazel() { - cat > "$1" <<'EOF' + { + cat <<'EOF' #!/bin/bash set -euo pipefail -echo "Hello from $(basename $0)!" -echo "My args: $@" +if [[ ${1:-""} == "--version" ]]; then + echo "bazel BAZEL_VERSION" +else + echo "Hello from $(basename $0)!" + echo "My args: $@" +fi exit 0 EOF + } | sed "s/BAZEL_VERSION/$2/" > "$1" chmod +x "$1" } @@ -65,9 +71,9 @@ setup_mock() { mkdir bin cp "$wrapper" "bin/bazel" chmod +x "bin/bazel" - mock_bazel "bin/bazel-0.29.1" - mock_bazel "bin/bazel-1.0.1" - mock_bazel "bin/bazel-real" + mock_bazel "bin/bazel-0.29.1" "0.29.1" + mock_bazel "bin/bazel-1.0.1" "1.0.1" + mock_bazel "bin/bazel-real" "1.1.0" # We don't want USE_BAZEL_VERSION passed by --test_env to affect this test. unset USE_BAZEL_VERSION @@ -77,9 +83,9 @@ setup_mock() { test_use_bazel_version_envvar() { setup_mock - USE_BAZEL_VERSION="0.29.1" ../bin/bazel version &> "$TEST_log" + USE_BAZEL_VERSION="0.29.1" ../bin/bazel build &> "$TEST_log" expect_log "Hello from bazel-0.29.1" - expect_log "My args: version" + expect_log "My args: build" } test_bazelversion_file() { @@ -206,4 +212,13 @@ EOF expect_log "My args: build //src:bazel" } +test_wrapper_detects_version_of_bazel_real() { + setup_mock + + echo "1.1.0" > .bazelversion + ../bin/bazel &> "$TEST_log" + expect_log "Hello from bazel-real" + expect_log "My args:" +} + run_suite "Integration tests for scripts/packages/bazel.sh."