Skip to main content

DefaultInfo

A provider that all rules' implementations must return

In many simple cases, this can be inferred for the user.

Example of a rule's implementation function and how these fields are used by the framework:

# //foo_binary.bzl
def impl(ctx):
ctx.action.run([ctx.attrs._cc[RunInfo], "-o", ctx.attrs.out.as_output()] + ctx.attrs.srcs)
ctx.action.run([
ctx.attrs._strip[RunInfo],
"--binary",
ctx.attrs.out,
"--stripped-out",
ctx.attrs.stripped.as_output(),
"--debug-symbols-out",
ctx.attrs.debug_info.as_output(),
])
return [
DefaultInfo(
sub_targets = {
"stripped": [
DefaultInfo(default_outputs = [ctx.attrs.stripped, ctx.attrs.debug_info]),
],
},
default_output = ctx.attrs.out,
]

foo_binary = rule(
impl=impl,
attrs={
"srcs": attrs.list(attrs.source()),
"out": attrs.output(),
"stripped": attrs.output(),
"debug_info": attrs.output(),
"_cc": attrs.dep(default="//tools:cc", providers=[RunInfo]),
"_strip_script": attrs.dep(default="//tools:strip", providers=[RunInfo])
)

def foo_binary_wrapper(name, srcs):
foo_binary(
name = name,
srcs = src,
out = name,
stripped = name + ".stripped",
debug_info = name + ".debug_info",
)

# //subdir/BUCK
load("//:foo_binary.bzl", "foo_binary_wrapper")

genrule(name = "gen_stuff", ...., default_outs = ["foo.cpp"])

# ":gen_stuff" pulls the default_outputs for //subdir:gen_stuff
foo_binary_wrapper(name = "foo", srcs = glob(["*.cpp"]) + [":gen_stuff"])

# Builds just 'foo' binary. The strip command is never invoked.
$ buck build //subdir:foo

# builds the 'foo' binary, because it is needed by the 'strip' command. Ensures that
# both the stripped binary and the debug symbols are built.
$ buck build //subdir:foo[stripped]

DefaultInfo.default_outputs

DefaultInfo.default_outputs: list[artifact]

A list of Artifacts that are built by default if this rule is requested explicitly (via CLI or $(location) etc), or depended on as as a "source" (i.e., attrs.source()).


DefaultInfo.other_outputs

DefaultInfo.other_outputs: list[CellPath | artifact | cell_root | cmd_args | label | output_artifact | project_root | resolved_macro | str | tagged_command_line | target_label | transitive_set_args_projection | write_json_cli_args | RunInfo]

A list of ArtifactTraversable. The underlying Artifacts they define will be built by default if this rule is requested (via CLI or $(location) etc), but not when it's depended on as as a "source" (i.e., attrs.source()). ArtifactTraversable can be an Artifact (which yields itself), or cmd_args, which expand to all their inputs.


DefaultInfo.sub_targets

DefaultInfo.sub_targets: dict[str, provider_collection]

A mapping of names to ProviderCollections. The keys are used when resolving the ProviderName portion of a ProvidersLabel in order to access the providers for a subtarget, such as when doing buck2 build cell//foo:bar[baz]. Just like any ProviderCollection, this collection must include at least a DefaultInfo provider. The subtargets can have their own subtargets as well, which can be accessed by chaining them, e.g.: buck2 build cell//foo:bar[baz][qux].