Skip to main content

Deep dive into Bazel queries: from basics to advanced use cases

Picture of Łukasz Wawrzyk, Senior Software Engineer

Łukasz Wawrzyk

Senior Software Engineer
Nov 13, 2024|23 min read
small forrest shaped like a Bazel queries logo
1% bazel query //src/main/scala/lib:greeting
2//src/main/scala/lib:greeting
1% bazel query //core
2//core:core
1% bazel query //src/main/scala/...
2//src/main/scala/cmd:runner
3//src/main/scala/lib:greeting
1% bazel query //src/main/scala/cmd:all
2//src/main/scala/cmd:runner
1% bazel query //src/main/scala/cmd:*
2zsh: no matches found: //src/main/scala/cmd:*
1% bazel query //src/main/scala/cmd:*
2zsh: no matches found: //src/main/scala/cmd:*
1% bazel query '//src/main/scala/cmd:*'
2//src/main/scala/cmd:BUILD
3//src/main/scala/cmd:Runner.scala
4//src/main/scala/cmd:runner
5//src/main/scala/cmd:runner.diagnosticsproto
6//src/main/scala/cmd:runner.jar
7//src/main/scala/cmd:runner.sdeps
8//src/main/scala/cmd:runner.statsfile
9//src/main/scala/cmd:runner_MANIFEST.MF
10//src/main/scala/cmd:runner_deploy.jar
1% bazel query '//src/main/scala/cmd:*' --output=label_kind
2source file //src/main/scala/cmd:BUILD
3source file //src/main/scala/cmd:Runner.scala
4scala_binary rule //src/main/scala/cmd:runner
5generated file //src/main/scala/cmd:runner.diagnosticsproto
6generated file //src/main/scala/cmd:runner.jar
7generated file //src/main/scala/cmd:runner.sdeps
8generated file //src/main/scala/cmd:runner.statsfile
9generated file //src/main/scala/cmd:runner_MANIFEST.MF
10generated file //src/main/scala/cmd:runner_deploy.jar
1% bazel aquery //contrib/kafka/filters/network/source:kafka_request_parser_lib --noinclude_commandline --noinclude_artifacts
2...
3action 'Compiling contrib/kafka/filters/network/source/external/kafka_request_resolver.cc'
4  Mnemonic: CppCompile
5  Target: //contrib/kafka/filters/network/source:kafka_request_parser_lib
6  Configuration: darwin_arm64-fastbuild
7  Execution platform: @local_config_platform//:host
8  ActionKey: fdd301775be771e1443d79f1f2b60ec7a843cebe6c20aaf90681cf95c0f8b11f
9  Environment: [PATH=/opt/homebrew/bin:/opt/local/bin:/usr/local/bin:/usr/bin:/bin]
10  ...
11action 'Compiling contrib/kafka/filters/network/source/kafka_request_parser.cc'
12  Mnemonic: CppCompile
13  Target: //contrib/kafka/filters/network/source:kafka_request_parser_lib
14  Configuration: darwin_arm64-fastbuild
15  Execution platform: @local_config_platform//:host
16  ActionKey: 2a89b778569ea5fd7c114767406d4d3edcd6323041fd2020650de55ac460c314
17  Environment: [PATH=/opt/homebrew/bin:/opt/local/bin:/usr/local/bin:/usr/bin:/bin]
18  ...
19action 'Linking contrib/kafka/filters/network/source/libkafka_request_parser_lib.lo'
20  Mnemonic: CppArchive
21  Target: //contrib/kafka/filters/network/source:kafka_request_parser_lib
22  Configuration: darwin_arm64-fastbuild
23  Execution platform: @local_config_platform//:host
24  ActionKey: 9b64c5896348854483ff15cd8c2888cbcf9eab9a5a577754a871e4a25a1c215f
25  Environment: [PATH=/opt/homebrew/bin:/opt/local/bin:/usr/local/bin:/usr/bin:/bin]
26  ...
1% bazel aquery 'mnemonic(CppCompile, //contrib/kafka/filters/network/source:kafka_request_parser_lib)'
2action 'Compiling contrib/kafka/filters/network/source/external/kafka_request_resolver.cc'
3 ...
1Inputs: [bazel-out/darwin_arm64-fastbuild/bin/contrib/kafka/filters/network/source/external/kafka_request_resolver.cc, bazel-out/darwin_arm64-fastbuild/internal/_middlemen/_S_Scontrib_Skafka_Sfilters_Snetwork_Ssource_Ckafka_Urequest_Uparser_Ulib-cc_library-compile, external/local_config_cc/cc_wrapper.sh, external/local_config_cc/libtool, external/local_config_cc/libtool_check_unique, external/local_config_cc/make_hashed_objlist.py, external/local_config_cc/wrapped_clang, external/local_config_cc/wrapped_clang_pp, external/local_config_cc/xcrunwrapper.sh]
1Outputs: [bazel-out/darwin_arm64-fastbuild/bin/contrib/kafka/filters/network/source/_objs/kafka_request_parser_lib/kafka_request_resolver.d, bazel-out/darwin_arm64-fastbuild/bin/contrib/kafka/filters/network/source/_objs/kafka_request_parser_lib/kafka_request_resolver.o]
2 ...
3 Command Line: (exec external/local_config_cc/wrapped_clang_pp \
4 ...
5 -O0 \
6 -DDEBUG \
7 '-std=c++11' \
8 -iquote \
9 . \
10 -iquote \
11 bazel-out/darwin_arm64-fastbuild/bin \
12 -iquote \
13 external/com_google_absl \
14 ...
15 -Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/protobuf \
16-Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/internal_visibility \
17-Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/port \ -Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/protobuf_lite \ -Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/arena \
18-Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/arena_align \
19 -Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/stubs/_virtual_includes/lite \
20-Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/arena_allocation_policy \
21-Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/arena_cleanup \ -Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/string_block \ -Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/_virtual_includes/varint_shuffle \ -Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/io/_virtual_includes/io \
22-Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/io/_virtual_includes/io_win32 \
23-Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/third_party/utf8_range/_virtual_includes/utf8_validity \ -Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/third_party/utf8_range/_virtual_includes/utf8_range \ -Ibazel-out/darwin_arm64-fastbuild/bin/external/com_google_protobuf/src/google/protobuf/io/_virtual_includes/gzip_stream \
24
25 -DENVOY_ADMIN_FUNCTIONALITY \
26 -DENVOY_ENABLE_QUIC \
27 -DENVOY_ENABLE_FULL_PROTOS \
28 ...
1% cat contrib/kafka/filters/network/test/metrics_integration_test.cc | head
2#include "test/common/stats/stat_test_utility.h"
3#include "contrib/kafka/filters/network/source/external/request_metrics.h"
4#include "contrib/kafka/filters/network/source/external/response_metrics.h"
5#include "contrib/kafka/filters/network/test/message_utilities.h"
6#include "gtest/gtest.h"
7namespace Envoy {
8namespace Extensions {
9namespace NetworkFilters {
1% find . | grep contrib/kafka/filters/network/source/external/request_metrics.h
1bazel aquery 'outputs(".*/contrib/kafka/filters/network/source/external/request_metrics\.h", deps(//...))' --keep_going
2action 'Executing genrule //contrib/kafka/filters/network/source:kafka_request_generated_source'
3 Mnemonic: Genrule
4 Target: //contrib/kafka/filters/network/source:kafka_request_generated_source
5 ...
1Outputs: [bazel-out/darwin_arm64-fastbuild/bin/contrib/kafka/filters/network/source/external/kafka_request_resolver.cc,
2 bazel-out/darwin_arm64-fastbuild/bin/contrib/kafka/filters/network/source/external/request_metrics.h,
3 bazel-out/darwin_arm64-fastbuild/bin/contrib/kafka/filters/network/source/external/requests.h]
1% cat bazel-out/darwin_arm64-fastbuild/bin/contrib/kafka/filters/network/source/external/request_metrics.h | head
2#pragma once
3#include <array>
4#include <functional>
5#include "envoy/stats/scope.h"
6#include "envoy/stats/stats_macros.h"
7...
1% bazel aquery 'deps(//contrib/kafka/filters/network/test:metrics_integration_test)' --output summary
2Mnemonics:
3 ...
4 Genrule: 8
5 ...
6 CppArchive: 429
7 ...
8 CppCompile: 1235
9...
1% bazel cquery --output build //contrib/kafka/filters/network/test:metrics_integration_test --config=windows
2cc_test(
3 name = "metrics_integration_test"
4 ...
5 srcs = ["//contrib/kafka/filters/network/test:metrics_integration_test.cc"],
6 linkopts = ["-Wl,-no_deduplicate"],
7 copts = ["-Wall", "-Wextra", "-Werror", "-Wnon-virtual-dtor", "-Woverloaded-virtual", "-Wold-style-cast", "-Wformat", "-Wformat-security", "-Wvla", "-Wno-deprecated-declarations", "-Wreturn-type", "-fno-limit-debug-info", "-Wgnu-conditional-omitted-operand", "-Wc++2a-extensions", "-Wrange-loop-analysis", "-Wno-range-loop-analysis", "-DABSL_MALLOC_HOOK_MMAP_DISABLE", "-DENVOY_OBJECT_TRACE_ON_DUMP", "-D__APPLE_USE_RFC_3542", "-DENVOY_NGHTTP2", "-DENVOY_ADMIN_HTML", "-DENVOY_STATIC_EXTENSION_REGISTRATION", "-DENVOY_GOOGLE_GRPC"],
8 ...
9 deps = ["//bazel:static_stdlib", "//contrib/kafka/filters/network/test:message_utilities", "//contrib/kafka/filters/network/source:kafka_metrics_lib", "//test/common/stats:stat_test_utility_lib", "//external:googletest", "//test:main", "//test/test_common:test_version_linkstamp"],
10 malloc = "@bazel_tools//tools/cpp:malloc",
11)
1% bazel cquery //src/main/scala/cmd:runner --output=starlark --starlark:expr='"\n".join([f.path for f in providers(target)["JavaInfo"].transitive_runtime_jars.to_list()])'
2bazel-out/darwin_arm64-fastbuild/bin/src/main/scala/cmd/runner.jar
3bazel-out/darwin_arm64-fastbuild/bin/src/main/scala/lib/greeting.jar
4external/io_bazel_rules_scala_scala_library/scala-library-2.13.12.jar
5external/io_bazel_rules_scala_scala_reflect/scala-reflect-2.13.12.jar
1 % bazel cquery //src/main/scala/cmd:runner --output=starlark --starlark:expr='dir(target)'
2["actions", "data_runfiles", "default_runfiles", "files", "files_to_run", "label", "output_groups"]
3% bazel cquery //src/main/scala/cmd:runner --output=starlark --starlark:expr='dir(target.actions)'
4["append", "clear", "extend", "index", "insert", "pop", "remove"]
5% bazel cquery //src/main/scala/cmd:runner --output=starlark --starlark:expr='dir(target.actions[0])'
6["args", "argv", "content", "env", "inputs", "mnemonic", "outputs", "substitutions"]
1//tools/type_whisperer:embedded_api_type_db (dfb10c3)
2 deps#//tools/type_whisperer:api_type_db#(TestTrimmingTransition + ConfigFeatureFlagTaggedTrimmingTransition) -> dfb10c3
3 $proto_cc_source_gen#//tools/type_whisperer:proto_cc_source_gen#((exec + ToDefault) + (TestTrimmingTransition + ConfigFeatureFlagTaggedTrimmingTransition)) -> bc9c49a
4ToDefault -> //tools/type_whisperer:file_descriptor_set_text_gen (bc9c49a)
5ToDefault -> //tools/type_whisperer:file_descriptor_set_text_gen (dfb10c3)
6ToDefault -> //tools/type_whisperer:proto_build_targets_gen (bc9c49a)
7 deps#//tools/type_whisperer:api_type_db_proto_py_proto#(TestTrimmingTransition + ConfigFeatureFlagTaggedTrimmingTransition) -> bc9c49a
8ToDefault -> //tools/type_whisperer:proto_build_targets_gen (dfb10c3)
9 deps#//tools/type_whisperer:api_type_db_proto_py_proto#(TestTrimmingTransition + ConfigFeatureFlagTaggedTrimmingTransition) -> dfb10c3
1% bazel query //src/main/scala/lib:greeting --output=xml
2<?xml version="1.1" encoding="UTF-8" standalone="no"?>
3<query version="2">
4 <rule class="scala_library" location="/Users/lukasz/dev/playground/bazel-scala-sandbox/src/main/scala/lib/BUILD:3:14" name="//src/main/scala/lib:greeting">
5 <string name="name" value="greeting"/>
6 <list name="visibility">
7 <label value="//visibility:public"/>
8 </list>
9 <list name="srcs">
10 <label value="//src/main/scala/lib:Greeting.scala"/>
11 </list>
12 <rule-input name="//src/main/scala/lib:Greeting.scala"/>
13 <rule-input name="@bazel_tools//tools/jdk:current_host_java_runtime"/>
14 <rule-input name="@bazel_tools//tools/jdk:current_java_runtime"/>
15 <rule-input name="@bazel_tools//tools/jdk:current_java_toolchain"/>
16 <rule-input name="@io_bazel_rules_scala//scala/private/toolchain_deps:scala_library_classpath"/>
17 <rule-input name="@io_bazel_rules_scala//src/java/io/bazel/rulesscala/coverage/instrumenter:instrumenter"/>
18 <rule-input name="@io_bazel_rules_scala//src/java/io/bazel/rulesscala/exe:exe"/>
19 <rule-input name="@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac:scalac"/>
20 <rule-input name="@io_bazel_rules_scala//third_party/dependency_analyzer/src/main:dependency_analyzer"/>
21 <rule-output name="//src/main/scala/lib:greeting.jar"/>
22 <rule-output name="//src/main/scala/lib:greeting_deploy.jar"/>
23 <rule-output name="//src/main/scala/lib:greeting_MANIFEST.MF"/>
24 <rule-output name="//src/main/scala/lib:greeting.statsfile"/>
25 <rule-output name="//src/main/scala/lib:greeting.diagnosticsproto"/>
26 <rule-output name="//src/main/scala/lib:greeting.sdeps"/>
27 </rule>
28</query>

Subscribe to our newsletter and never miss an article