Skip to content

Commit

Permalink
[bazel] Allow ./bazelisk.sh to use --bazelrc flags
Browse files Browse the repository at this point in the history
These flags need to come _before_ the subcommand, i.e.:

```
bazel --bazelrc=... cquery # VALID
bazel cquery --bazelrc=... # INVALID
```

which is done in this script by pushing flags that come before the
subcommand name into an array and re-applying them in the correct order
later.

If these Bash arrays are confusing and you've come across this commit in
a `git blame` then hopefully this will help:

1. Bash supports arrays defined using parentheses: `array=("foo" "bar")`
2. Arrays can't be passed as arguments to functions, they are expanded
   and get mixed into other arguments.
3. Names of arrays _can_ be passed as arguments, and Bash lets us
   re-create a variable pointing at an array using its name: `${!name}`.
4. the syntax for expanding an array into its elements with correct
   quoting on each is: `"${array[@]}"`.

Signed-off-by: James Wainwright <[email protected]>
  • Loading branch information
jwnrt committed Jan 17, 2024
1 parent 6bc6e82 commit d236951
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions bazelisk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,18 @@ function outquery_starlark_expr() {
esac
}

# Arguments:
# $qexpr: starlark expression - see `outquery_starlark_expr`
# $name: name of an array containing Bazel arguments that should come _before_
# the subcommand (e.g. `--bazelrc=...`).
function do_outquery() {
local qexpr="$1"
shift
"$file" cquery "$@" \
local qexpr="$1"; shift
# Bash can't pass whole arrays through functions so we pass the name of the
# array instead and expand it.
local name="$1"; shift
local pre_cmd_args=("${!name}")

"$file" "${pre_cmd_args[@]}" cquery "$@" \
--output=starlark --starlark:expr="$qexpr" \
--ui_event_filters=-info --noshow_progress
}
Expand All @@ -121,6 +129,14 @@ function main() {
fi
fi

# Shift all flags (starting with `-`) that come before the subcommand
# into an array.
pre_cmd_args=()
while [[ "$1" == -* ]]; do
pre_cmd_args+=("$1")
shift
done

case "${1-}" in
outquery*)
# The custom 'outquery' command can be used to query bazel for the
Expand All @@ -130,10 +146,9 @@ function main() {
# outquery-all: return all output files associated with the label.
# outquery-x: return output files containing the substring "x".
# outquery.x: return output files ending with the substring ".x".
local qexpr
qexpr="$(outquery_starlark_expr "$1")"
local qexpr="$(outquery_starlark_expr "$1")"
shift
do_outquery "$qexpr" "$@"
do_outquery "$qexpr" pre_cmd_args "$@"
;;
build-then)
# The 'build-then' command builds the requested targets and then
Expand All @@ -149,14 +164,14 @@ function main() {
shift
local qexpr outfile
qexpr="$(outquery_starlark_expr outquery)"
outfile=$(do_outquery "$qexpr" "$@")
"$file" build "$@"
outfile=$(do_outquery "$qexpr" pre_cmd_args "$@")
"$file" "${pre_cmd_args[@]}" build "$@"
# shellcheck disable=SC2059
# We are intentionally using $command_template as a format string.
eval "$(printf "$command_template" "$outfile")"
;;
*)
exec "$file" "$@"
exec "$file" "${pre_cmd_args[@]}" "$@"
;;
esac
}
Expand Down

0 comments on commit d236951

Please sign in to comment.