Skip to main content

Dependency

Represents a dependency in a build rule. When you declare a dependency attribute using attrs.dep() in your rule definition, accessing that attribute gives you a Dependency object that provides access to the dependency's providers and metadata.

Key operations:

  • Index with dep[ProviderType] to access a provider (errors if absent)
  • Use dep.get(ProviderType) to optionally access a provider (returns None if absent)
  • Access the dependency's label with dep.label
  • Get subtargets with dep.sub_target("name")

Example usage in a rule:

my_library = rule(
impl = my_library_impl,
attrs = {
"deps": attrs.list(attrs.dep()),
},
)

def my_library_impl(ctx):
# Iterate over dependencies
for dep in ctx.attrs.deps:
# Access providers
if dep.get(CxxLibraryInfo):
libs = dep[CxxLibraryInfo].libraries

# Access outputs
outputs = dep[DefaultInfo].default_outputs

# Get the label
dep_target = dep.label.raw_target()

Dependency.get

def Dependency.get(index) -> None | Provider

Gets a specific provider from this dependency by provider type. Returns None if the provider is not present. This is the same as using indexing syntax dep[ProviderType], but returns None instead of raising an error when the provider is absent.

Example:

FooInfo = provider(fields=["bar"])

def _impl(ctx):
for dep in ctx.attrs.deps:
# Try to get FooInfo provider, returns None if absent
foo_info = dep.get(FooInfo)
if foo_info:
# Provider exists, use it
value = foo_info.bar
else:
# Provider not available from this dependency
pass

# Compare with indexing (raises error if absent):
# foo_info = dep[FooInfo] # Errors if FooInfo not provided

Dependency.label

Dependency.label: Label

The label of this dependency.


Dependency.providers

Dependency.providers: list

Returns a list of all providers available from this dependency.


Dependency.sub_target

def Dependency.sub_target(
subtarget: str,
/,
) -> Dependency

Returns a Dependency object of the subtarget of this target.

In most cases, you can also use dep[DefaultInfo].sub_targets["foo"] to access subtarget providers directly. This method is useful when you need a real Dependency object, such as when passing to ctx.actions.anon_target().

Example:

def _impl(ctx):
for dep in ctx.attrs.deps:
# Get the dependency for a subtarget named "shared"
shared_dep = dep.sub_target("shared")
# Now shared_dep is a Dependency you can pass to other APIs
# that require a Dependency object
ctx.actions.anon_target(my_rule, {"dep": shared_dep})