Skip to main content

bxl_output_stream type

The output stream for bxl to print values to the console as their result

bxl_output_stream.ensure

def bxl_output_stream.ensure(artifact: artifact) -> ensured_artifact

Marks the artifact as an artifact that should be available to the users at the end of the bxl invocation. Any artifacts that do not get registered via this call is not accessible by users at the end of bxl script.

This function returns an ensured_artifact type that can be printed via ctx.output.print() to print its actual path on disk.

Sample usage:

def _impl_ensure(ctx):
actions = ctx.bxl_actions().actions
output = actions.write("my_output", "my_content")
ensured = ctx.output.ensure(output)
ctx.output.print(ensured)

bxl_output_stream.ensure_multiple

def bxl_output_stream.ensure_multiple(artifacts)

Same as ensure, but for multiple artifacts. Will preserve the shape of the inputs (i.e. if the resulting Dict of a ctx.build() is passed in, the output will be a Dict where the key is preserved, and the values are converted to ensured_artifacts).

Note that is slower to loop through objects and ensure them one by one than it is to call ensure_multiple() on all the objects at once (if possible). So, it is suggested to use this method when you are only ensuring a few individual artifacts that are not stored in an iterable.

Sample usage:

def _impl_ensure_multiple(ctx):
outputs = {}
for target, value in ctx.build(ctx.cli_args.target).items():
outputs.update({target.raw_target(): ctx.output.ensure_multiple(value.artifacts())})
ctx.output.print_json(outputs)

bxl_output_stream.print

def bxl_output_stream.print(*args, sep: str = " ") -> None

Outputs results to the console via stdout. These outputs are considered to be the results of a bxl script, which will be displayed to stdout by buck2 even when the script is cached. Accepts an optional separator that defaults to " ".

Prints that are not result of the bxl should be printed via stderr via the stdlib print and pprint. Note that ctx.output.print() is intended for simple outputs. For more complex outputs, the recommendation would be to write them to a file.

Sample usage:

def _impl_print(ctx):
ctx.output.print("test")

bxl_output_stream.print_json

def bxl_output_stream.print_json(value, *, pretty: bool = True) -> None

Outputs results to the console via stdout as pretty-printed json. Pretty printing can be turned off by the pretty keyword-only parameter. These outputs are considered to be the results of a bxl script, which will be displayed to stdout by buck2 even when the script is cached.

Prints that are not result of the bxl should be printed via stderr via the stdlib print and pprint.

Sample usage:

def _impl_print_json(ctx):
outputs = {}
outputs.update({"foo": bar})
ctx.output.print_json("test")