Skip to main content

Query.eval

def Query.eval(
) -> 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
**Query** dependencies of a target
deps = ctx.bazel.query().targets("//myapp:main").deps()
all_deps: target_set = deps.eval()

**Chain** multiple operations
sources = ctx.bazel.query().targets("//myapp:main")
    .deps()
    .kind("source file")
    .eval()

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.
**Complex** intersection query
complex = ctx.bazel.query().raw("deps(//foo) intersect kind('test', //bar:*)")

**Path**-based query
path_query = ctx.bazel.query().raw("somepath(//start, //end)")

**Chaining** after raw
filtered = complex.kind("source file")