bazel queries Last updated Sept 21, 2023

Bazel includes a powerful query engine.

examples

external repos

  • $ bazel query 'kind(local_repository, //external:*)'

  • $ bazel query 'kind(http_archive, //external:*)'

  • $ bazel query 'kind(.*, //external:*)'

  • $ ls $(bazel info output_base)/external/

providers

bazel cquery //path/to:tgt --output=starlark --starlark:expr="[p for p in providers(target)]"
bazel run @obazl//inspect:providers --@obazl//tgt=//interop/ffi/case110:Main

misc

Show all ocaml_signature targets:

bazel query 'kind("ocaml_signature", deps(//..., 1))' --keep_going

Show all .mll files:

bazel query 'filter("\.mll$", deps("//...", 1))'

bazel query 'attr(sig, ".*", kind("ocaml_module", deps(//src/…​)))'

Show all the ocaml_module targets that lack a sig attribute (i.e. all singleton structfiles.):

bazel query 'kind("ocaml_module", deps(//mwe/...)) except attr(sig, ".*", kind("ocaml_module", deps(//mwe/...)))'

Run build based on results of query:

bazel build $(bazel query 'some_pattern')

This can be used to selectively apply an aspect:

bazel build $(bazel query 'some_pattern') --aspects @rules_ocaml//aspects:aspects.bzl%foo