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):
out = ctx.actions.declare_output("out")
ctx.actions.run([ctx.attrs._cc[RunInfo], "-o", out.as_output()] + ctx.attrs.srcs)
stripped_out = ctx.actions.declare_output("stripped")
debug_symbols_out = ctx.actions.declare_output("debug_info")
ctx.actions.run([
ctx.attrs._strip[RunInfo],
"--binary",
out,
"--stripped-out",
stripped_out.as_output(),
"--debug-symbols-out",
debug_symbols_out.as_output(),
])
return [
DefaultInfo(
sub_targets = {
"stripped": [
DefaultInfo(default_outputs = [stripped_out, debug_symbols_out]),
],
},
default_output = out,
),
]

foo_binary = rule(
impl = impl,
attrs = {
"srcs": attrs.list(attrs.source()),
"_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[Artifact | CellPath | CellRoot | Label | OutputArtifact | ProjectRoot | ResolvedStringWithMacros | TaggedCommandLine | TargetLabel | TransitiveSetArgsProjection | WriteJsonCliArgs | cmd_args | str | 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, ProviderCollection]

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].