Skip to main content

CqueryContext

The context for performing cquery operations in bxl. The functions offered on this ctx are the same behaviour as the query functions available within cquery command.

Query results are target_sets of target_nodes, which supports iteration, indexing, len(), set addition/subtraction, and equals().

CqueryContext.allpaths

def CqueryContext.allpaths(
from: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
to: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
filter: None | str = None,
) -> target_set

The allpaths query for computing all dependency paths.


CqueryContext.attrfilter

def CqueryContext.attrfilter(
attr: str,
value: str,
targets: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
) -> target_set

The attrfilter query for rule attribute filtering.


CqueryContext.attrregexfilter

def CqueryContext.attrregexfilter(
attribute: str,
value: str,
targets: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
) -> target_set

The attrregexfilter query for rule attribute filtering with regex.

Sample usage:

def _impl_attrregexfilter(ctx):
    filtered = ctx.cquery().attrregexfilter("foo", "he.lo", "bin/kind/...")
    ctx.output.print(filtered)

CqueryContext.buildfile

def CqueryContext.buildfile(
targets: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
) -> file_set

Find the build file(s) that defines a target or a target set.

Sample usage:

def _buildfile_impl(ctx):
    owner = ctx.cquery().owner(["bin/TARGET", "bin/kind"])
    result = ctx.cquery().buildfile(owner)
    ctx.output.print(result)

CqueryContext.deps

def CqueryContext.deps(
universe: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
depth: None | int = None,
filter: None | str = None,
) -> target_set

The deps query for finding the transitive closure of dependencies.

Sample usage:

def _impl_deps(ctx):
    result = ctx.cquery().deps("root//bin:the_binary", 1)
    ctx.output.print(result)

CqueryContext.eval

def CqueryContext.eval(
query: str,
query_args: None | target_set | list[str] = None,
target_universe: None | list[str] | tuple[str, ...] = None,
)

Evaluates some general query string. query_args can be a target_set of unconfigured nodes, or a list of strings. Returns a dict of target labels mapped to their target_set results if query_args was passed in, otherwise returns a single target_set.

Sample usage:

def _impl_eval(ctx):
    result1 = ctx.cquery().eval("inputs(root//bin:the_binary)")
    ctx.output.print(result1)

    result2 = ctx.cquery().eval("inputs(%s)", query_args = ["cell//path/to/file:target"])
    ctx.output.print(result2)

CqueryContext.filter

def CqueryContext.filter(
regex: str,
targets: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
) -> target_set

The filter query for filtering targets by name.

Sample usage:

def _impl_filter(ctx):
    result = ctx.cquery().filter(".*the_binary", "root//...")
    ctx.output.print(result)

CqueryContext.inputs

def CqueryContext.inputs(
targets: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
) -> file_set

The inputs query for finding input files.

Sample usage:

def _impl_inputs(ctx):
    result = ctx.cquery().inputs("root//bin:the_binary")
    ctx.output.print(result)

CqueryContext.kind

def CqueryContext.kind(
regex: str,
targets: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
) -> target_set

The kind query for filtering targets by rule type.

Sample usage:

def _impl_kind(ctx):
    kind = ctx.cquery().kind(".*1", "bin/kind/...")
    ctx.output.print(kind)

CqueryContext.nattrfilter

def CqueryContext.nattrfilter(
attr: str,
value: str,
targets: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
) -> target_set

The nattrfilter query for rule attribute filtering. It is the opposite of attrfilter, i.e. it filters targets by attribute but excludes those that match.


CqueryContext.owner

def CqueryContext.owner(
files: file_set | str | list[str] | tuple[str, ...],
universe: None | bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label] = None,
) -> target_set

The owner query for finding targets that own specified files. Note that if you do not pass in a cell path (where the format is <cell>//path/to/file), the path is resolved against the cell that the BXL script lives in. If you need to evaluate a file path that lives in a different cell, you must pass in the fully qualified cell path.

Sample usage:

def _owner_impl(ctx):
    owner = ctx.cquery().owner("bin/TARGETS.fixture", "foo//target/universe/...")
    ctx.output.print(owner)

CqueryContext.rdeps

def CqueryContext.rdeps(
universe: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
from: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
depth: int = ...,
filter: None | str = None,
) -> target_set

The rdeps query for finding the transitive closure of reverse dependencies.

Sample usage:

def _impl_rdeps(ctx):
    result = ctx.cquery().rdeps("root//bin:the_binary", "//lib:file1", 100)
    ctx.output.print(result)

CqueryContext.somepath

def CqueryContext.somepath(
from: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
to: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
filter: None | str = None,
) -> target_set

CqueryContext.testsof

def CqueryContext.testsof(
targets: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
) -> target_set

The testsof query for listing the tests of the specified targets.


CqueryContext.testsof_with_default_target_platform

def CqueryContext.testsof_with_default_target_platform(
targets: bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label | target_set | target_set | list[bxl.ConfiguredTargetNode | bxl.UnconfiguredTargetNode | configured_target_label | str | target_label],
) -> target_set

The testsof query for listing the tests of the specified targets. Performs default target platform resolution under the hood for the tests found.