Make it easier for tools/bazel wrapper script to find bazel-real
The launcher script uses "exec -a" to launch the wrapper, but if
the wrapper script is itself a Bash script, Bash will set "$0" to be
the path to the wrapper script, not the path to the launcher script.
To work around this, we've been having our wrapper script search the
user's PATH environment variable for bazel-real, but that doesn't
work well with the IntelliJ plugin for Bazel, which may not use the
expected PATH environment.
--
Change-Id: I402ad29d5b809be8e687e217e19c03e7ac3eb972
Reviewed-on: https://cr.bazel.build/7550
PiperOrigin-RevId: 140851992
MOS_MIGRATED_REVID=140851992
diff --git a/scripts/packages/bazel.sh b/scripts/packages/bazel.sh
index b9ad1d7..43bc593 100755
--- a/scripts/packages/bazel.sh
+++ b/scripts/packages/bazel.sh
@@ -21,23 +21,6 @@
# file and runs that. If that's not found, it runs the real Bazel binary which
# is installed next to this script as bazel-real.
-WORKSPACE_DIR="${PWD}"
-while [[ "${WORKSPACE_DIR}" != / ]]; do
- if [[ -e "${WORKSPACE_DIR}/WORKSPACE" ]]; then
- break;
- fi
- WORKSPACE_DIR="$(dirname "${WORKSPACE_DIR}")"
-done
-readonly WORKSPACE_DIR
-
-if [[ -e "${WORKSPACE_DIR}/WORKSPACE" ]]; then
- readonly WRAPPER="${WORKSPACE_DIR}/tools/bazel"
-
- if [[ -x "${WRAPPER}" ]]; then
- exec -a "$0" "${WRAPPER}" "$@"
- fi
-fi
-
# `readlink -f` that works on OSX too.
function get_realpath() {
if [ "$(uname -s)" == "Darwin" ]; then
@@ -78,7 +61,24 @@
fi
}
-BAZEL_REAL="$(dirname "$(get_realpath "${BASH_SOURCE[0]}")")/bazel-real"
+export BAZEL_REAL="$(dirname "$(get_realpath "${BASH_SOURCE[0]}")")/bazel-real"
+
+WORKSPACE_DIR="${PWD}"
+while [[ "${WORKSPACE_DIR}" != / ]]; do
+ if [[ -e "${WORKSPACE_DIR}/WORKSPACE" ]]; then
+ break;
+ fi
+ WORKSPACE_DIR="$(dirname "${WORKSPACE_DIR}")"
+done
+readonly WORKSPACE_DIR
+
+if [[ -e "${WORKSPACE_DIR}/WORKSPACE" ]]; then
+ readonly WRAPPER="${WORKSPACE_DIR}/tools/bazel"
+
+ if [[ -x "${WRAPPER}" ]]; then
+ exec -a "$0" "${WRAPPER}" "$@"
+ fi
+fi
if [[ ! -x "${BAZEL_REAL}" ]]; then
echo "Failed to find underlying Bazel executable at ${BAZEL_REAL}" >&2