Skip to main content

OutputArtifact

An artifact marked as an output of an action.

When you create custom actions with ctx.actions.run(), you need to tell Buck2 which artifacts the action will produce. OutputArtifact is how you mark a declared artifact as an output.

Common Usage

def _impl(ctx):
# Declare what file will be produced
out = ctx.actions.declare_output("output.txt")

# Run an action that produces it
# The artifact must be marked as output using .as_output()
ctx.actions.run(
cmd_args(["my_tool", "--output", out.as_output()]),
category = "process",
)

return [DefaultInfo(default_output = out)]

When is .as_output() needed?

  • Required: When passing declared artifacts to ctx.actions.run() - the action needs to know which artifacts it's responsible for producing
  • Not needed: When using ctx.actions.write(), ctx.actions.copy_file(), etc. - these methods handle the output declaration automatically

Key Rules

  • Every action must have at least one output
  • Each declared artifact can only be bound to one action
  • All declared artifacts must be bound before the rule finishes
  • If you forget to bind a declared artifact, Buck2 will raise an error

OutputArtifact.as_input

def OutputArtifact.as_input(
) -> Artifact

Returns the input artifact from which this output artifact was constructed


OutputArtifact.basename

OutputArtifact.basename: str

The base name of this artifact. e.g. for an artifact at foo/bar, this is bar


OutputArtifact.extension

OutputArtifact.extension: str

The file extension of this artifact. e.g. for an artifact at foo/bar.sh, this is .sh. If no extension is present, "" is returned.


OutputArtifact.is_source

OutputArtifact.is_source: bool

Whether the artifact represents a source file


OutputArtifact.owner

OutputArtifact.owner: Label | None

The Label of the rule that originally created this artifact. May also be None in the case of source files, or if the artifact has not be used in an action, or if the action was not created by a rule.


OutputArtifact.short_path

OutputArtifact.short_path: str

The interesting part of the path, relative to somewhere in the output directory. For an artifact declared as foo/bar, this is foo/bar.