Fix b with zsh.

zsh doesn't do string splitting like bash, so it passes the
post-processed bazel args to bazel as a single arg. So this CL adds a
conditional to do the splitting correctly for zsh with `setopt
shwordsplit`.

Reference: https://zsh.sourceforge.io/FAQ/zshfaq03.html

For bash, this uses array[@] to split the list into separate
shell words.

Test: zsh; source build/envsetup.sh && b test
//system/logging/logd:logd-unit-tests --config=linux_x86_64
Test: bash; source build/envsetup.sh && b test
//system/logging/logd:logd-unit-tests --config=linux_x86_64

Change-Id: I4e19a062b7f7e119b1612a8ce5c801878378cc69
This commit is contained in:
Jingwen Chen
2022-09-09 07:08:25 +00:00
parent 46ee66c098
commit 4ed082f61b

View File

@@ -1866,20 +1866,29 @@ function b()
# command. (build, test, run, ect) If the --config was added at the end, it wouldn't work with commands like:
# b run //foo -- --args-for-foo
local config_set=0
local bazel_args_with_config=""
# Represent the args as an array, not a string.
local bazel_args_with_config=()
for arg in $bazel_args; do
if [[ $arg == "--" && $config_set -ne 1 ]]; # if we find --, insert config argument here
then
bazel_args_with_config+="--config=bp2build -- "
bazel_args_with_config+=("--config=bp2build -- ")
config_set=1
else
bazel_args_with_config+="$arg "
bazel_args_with_config+=("$arg ")
fi
done
if [[ $config_set -ne 1 ]]; then
bazel_args_with_config+="--config=bp2build "
bazel_args_with_config+=("--config=bp2build ")
fi
bazel $bazel_args_with_config
if [ -n "$ZSH_VERSION" ]; then
# zsh breaks posix by not doing string-splitting on unquoted args
# by default. Enable the compatibility option.
setopt shwordsplit
fi
# Call Bazel.
bazel ${bazel_args_with_config[@]}
fi
)