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
diff --git a/scripts/packages/bazel.sh b/scripts/packages/bazel.sh
index 635c059..a240dfa 100755
--- a/scripts/packages/bazel.sh
+++ b/scripts/packages/bazel.sh
@@ -139,10 +139,23 @@
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}."