Search code examples
bazelbazel-query

How to pass a bash variable to a "bazel 'set' query"


I have the following set of files that I want to pass into Bazel using

res=$(git diff --name-only ${COMMIT_HASH}^! | grep '^src/' | uniq | cut -d/ -f2-)
# echo $res
# services/service-a/README.md
# services/service-a/main.go
# ...
bazel query --keep_going 'set(${res})'

But simply trying that results in:

bazel query --keep_going 'set(${res})'
ERROR: Skipping '$': no such target '//:$': target '$' not declared in package '' defined by /Users/aoutadi/code/KeepTruckin/kt/src/BUILD.bazel
ERROR: Skipping '{res': no such target '//:{res': target '{res' not declared in package '' defined by /Users/aoutadi/code/KeepTruckin/kt/src/BUILD.bazel
ERROR: Skipping '}': no such target '//:}': target '}' not declared in package '' defined by /Users/aoutadi/code/KeepTruckin/kt/src/BUILD.bazel
WARNING: --keep_going specified, ignoring errors. Results may be inaccurate
INFO: Empty results

I know that it might be possible to embed my command into the bazel query itself using

bazel query --keep_going 'set($(git diff --name-only ${COMMIT_HASH}^! | grep '^src/' | uniq | cut -d/ -f2-))'

but I would like to avoid this for clarity and instead use $res.

Is this possible?


Solution

  • This is likely because single quotes and double quotes behave differently in bash (assuming you're using bash). Single quotes won't interpolate variables, so try using double quotes:

    bazel query --keep_going "set(${res})"