Skip to main content
function Query.eval
def Query.eval(
*,
flags: list[str | (str, str)] = [],
announce_version: bool = False,
announce_command: bool = False
) -> typing.Iterable[generated_file | package_group | rule | source_file]
The query system provides a programmatic interface for analyzing build dependencies and target relationships. Queries are constructed using a chain API and are lazily evaluated only when .eval() is explicitly called. The entry point is ctx.bazel.query(), which returns a query for creating initial query expressions. Most operations operate on query objects, which represent sets of targets that can be filtered, transformed, and combined. Example
Fails with Bazel’s own stderr if the query exits non-zero (bad expression, BUILD evaluation error, …), rather than returning an empty target set — a failed query is not the same as one that matched nothing. Parameters
  • flags: - Command flags to pass to bazel query (between the expression and --output). Callers that run query alongside build / test under --ignore_all_rc_files MUST forward the rc-expanded command flags here, so the query resolves external repositories the same way the build does (e.g. an rc-set --noenable_bzlmod / --enable_workspace). Omitting them lets the query diverge from the build and fail on repos the build can see. Accepts the same str | (str, version-constraint) shape as ctx.bazel.build / .test; version-gated flags are filtered against the running Bazel version identically.
  • announce_version: - Print an INFO: Bazel <version> line before spawning. Resolved from the --announce-bazel-version task flag.
  • announce_command: - Print an INFO: Spawning: <command> line before spawning. Resolved from the --announce-bazel-command task flag. Both mirror the ctx.bazel.build / .test disclosure.
function Query.raw
def Query.raw(
expr: str,
/
) -> bazel.query.Query
Replaces the query expression with a raw query expression string. This escape hatch allows direct use of the underlying query language for complex cases, while still supporting further chaining.