Skip to main content

UqueryContext

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

UqueryContext.allpaths

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

The allpaths query for computing all dependency paths.


UqueryContext.attrfilter

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

The attrfilter query for rule attribute filtering.


UqueryContext.attrregexfilter

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

The attrregexfilter query for rule attribute filtering with regex.

Sample usage:

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

UqueryContext.buildfile

def UqueryContext.buildfile(
targets: bxl.UnconfiguredTargetNode | str | target_label | target_set | list[bxl.UnconfiguredTargetNode | 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.uquery().owner(["bin/TARGET", "bin/kind"])
    result = ctx.uquery().buildfile(owner)
    ctx.output.print(result)

UqueryContext.deps

def UqueryContext.deps(
universe: bxl.UnconfiguredTargetNode | str | target_label | target_set | list[bxl.UnconfiguredTargetNode | 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.uquery().deps("root//bin:the_binary", 1)
    ctx.output.print(result)

UqueryContext.eval

def UqueryContext.eval(
query: str,
query_args: None | target_set | list[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.uquery().eval("inputs(cell//path/to/file:target)")
    ctx.output.print(result1)

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

UqueryContext.filter

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

The filter query for filtering targets by name.

Sample usage:

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

UqueryContext.inputs

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

The inputs query for finding input files.

Sample usage:

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

UqueryContext.kind

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

The kind query for filtering targets by rule type.

Sample usage:

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

UqueryContext.owner

def UqueryContext.owner(
files: file_set | str | list[str] | tuple[str, ...],
) -> 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.uquery().owner("bin/TARGETS.fixture")
    ctx.output.print(owner)

UqueryContext.rdeps

def UqueryContext.rdeps(
universe: bxl.UnconfiguredTargetNode | str | target_label | target_set | list[bxl.UnconfiguredTargetNode | str | target_label],
from: bxl.UnconfiguredTargetNode | str | target_label | target_set | list[bxl.UnconfiguredTargetNode | str | target_label],
depth: None | int = None,
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.uquery().rdeps("root//bin:the_binary", "//lib:file1", 100)
    ctx.output.print(result)

UqueryContext.somepath

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

The somepaths query, which returns the graph of nodes on some arbitrary path from a start to destination target.


UqueryContext.targets_in_buildfile

def UqueryContext.targets_in_buildfile(
files: file_set | str | list[str] | tuple[str, ...],
) -> target_set

Given a set of buildfiles, return all targets within those buildfiles.

Usage:

def _targets_in_buildfile_impl(ctx):
    targets = ctx.uquery().targets_in_buildfile("bin/TARGETS.fixture")
    ctx.output.print(targets)

This is subject to be removed in future in favor of a more general targets_in_packages.


UqueryContext.testsof

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

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

Sample usage:

def _testsof_impl(ctx):
    result = ctx.uquery().testsof("//:foo_lib")
    ctx.output.print(result)