如何将 bash 变量传递给 "bazel 'set' query"

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

我有以下一组文件,我想使用

传递到 Bazel
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})'

但简单地尝试会导致:

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

我知道可以使用

将我的命令嵌入到 bazel 查询本身
bazel query --keep_going 'set($(git diff --name-only ${COMMIT_HASH}^! | grep '^src/' | uniq | cut -d/ -f2-))'

但为了清楚起见,我想避免这种情况,而是使用 $res.

这可能吗?

这可能是因为单引号和双引号在 bash 中的行为不同(假设您使用的是 bash)。单引号不会插入变量,因此请尝试使用双引号:

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