Skip to main content

Bazel.build

def Bazel.build(
*targets: str,
build_events: bool | list[bazel.build.BuildEventSink] = ,
workspace_events: bool = False,
execution_logs: bool = False,
flags: list[str] = [],
startup_flags: list[str] = [],
inherit_stdout: bool = False,
inherit_stderr: bool = True,
) -> bazel.build.Build
Build targets Bazel within AXL with ctx.bazel.build(). The result is a Build object, which has artifacts() (TODO), failures() (TODO), and a events() functions that provide iterators to the artifacts, failures, events respectively. Running ctx.bazel.build() does not block the Starlark thread. Explicitly call .wait() on the Build object to wait until the invocation finishes. You can pass in a single target or target pattern to build. Examples
def _fancy_build_impl(ctx):
    io = ctx.std.io
    build = ctx.bazel.build(
        "//target/to:build"
        build_events = True,
    )
    for event in build.build_events():
        if event.kind == "progress":
            io.stdout.write(event.payload.stdout)
            io.stderr.write(event.payload.stderr)

Bazel.query

def Bazel.query() -> bazel.query.Query
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 = deps.eval()

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

Bazel.test

def Bazel.test(
*targets: str,
build_events: bool | list[bazel.build.BuildEventSink] = ,
workspace_events: bool = False,
execution_logs: bool = False,
flags: list[str] = [],
startup_flags: list[str] = [],
inherit_stdout: bool = False,
inherit_stderr: bool = True,
) -> bazel.build.Build
Build & test Bazel targets within AXL with ctx.bazel.test(). The result is a Build object, which has artifacts() (TODO), failures() (TODO), and a events() functions that provide iterators to the artifacts, failures, events respectively. Running ctx.bazel.test() does not block the Starlark thread. Explicitly call .wait() on the Build object to wait until the invocation finishes. You can pass in a single target or target pattern to test. Examples
def _fancy_test_impl(ctx):
    io = ctx.std.io
    test = ctx.bazel.test(
        "//target/to:test"
        build_events = True,
    )
    for event in test.build_events():
        if event.kind == "progress":
            io.stdout.write(event.payload.stdout)
            io.stderr.write(event.payload.stderr)