8

A git commit may change some workspace rules, some source files, .bazelrc, etc. How to get all Bazel targets affected, thus need to rebuild and test, after such change?

In Buck, we can run buck targets --show-rulekey //... to see all rule key changes between two Git revisions. Is there any equivalent command in Bazel?

Zebra Propulsion Lab
  • 1,736
  • 1
  • 17
  • 28

2 Answers2

7

See here:

# Under Apache 2.0 licence
COMMIT_RANGE=${COMMIT_RANGE:-$(git merge-base origin/master HEAD)".."}

# Go to the root of the repo
cd "$(git rev-parse --show-toplevel)"

# Get a list of the current files in package form by querying Bazel.
files=()
for file in $(git diff --name-only ${COMMIT_RANGE} ); do
  files+=($(bazel query $file))
  echo $(bazel query $file)
done

# Query for the associated buildables
buildables=$(bazel query \
    --keep_going \
    --noshow_progress \
    "kind(.*_binary, rdeps(//..., set(${files[*]})))")
# Run the tests if there were results
if [[ ! -z $buildables ]]; then
  echo "Building binaries"
  bazel build $buildables
fi

tests=$(bazel query \
    --keep_going \
    --noshow_progress \
    "kind(test, rdeps(//..., set(${files[*]}))) except attr('tags', 'manual', //...)")
# Run the tests if there were results
if [[ ! -z $tests ]]; then
  echo "Running tests"
  bazel test $tests
fi

Take also a look at bazel-diff.

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
3

I don't have an answer, but this bazel-discuss thread may be helpful: https://groups.google.com/d/msg/bazel-discuss/I9udqWIcEdI/iczVgWLOBQAJ "Selecting bazel targets to run in CI - possible approaches"

Rodrigo Queiro
  • 1,324
  • 8
  • 15