Skip to content

Commit

Permalink
Let shellzelisk fallback to bazel-real if it's the requested version.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philwo authored and copybara-github committed Dec 17, 2019
1 parent 7a05289 commit 84eae2f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
13 changes: 13 additions & 0 deletions scripts/packages/bazel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}."

Expand Down
31 changes: 23 additions & 8 deletions src/test/shell/bazel/bazel_wrapper_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand All @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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."

0 comments on commit 84eae2f

Please sign in to comment.