IMPORTANT: The Bazel docs have moved! Please update your bookmark to https://bazel.build/rules/lib/Label

You can read about the migration, and let us know what you think.

Extensions > API reference > Label

Label

A BUILD target identifier.

Label

Label Label(label_string)

Creates a Label referring to a BUILD target. Use this function when you want to give a default value for the label attributes of a rule or when referring to a target via an absolute label from a macro. The argument must refer to an absolute label. The repo part of the label (or its absence) is interpreted in the context of the repo where this Label() call appears. Example:
Label("//tools:default")

Parameters

Parameter Description
label_string required
the label string.

name

string Label.name

The name of this label within the package. For instance:
Label("//pkg/foo:abc").name == "abc"

package

string Label.package

The package part of this label. For instance:
Label("//pkg/foo:abc").package == "pkg/foo"

relative

Label Label.relative(relName)

Resolves a label that is either absolute (starts with //) or relative to the current package. If this label is in a remote repository, the argument will be resolved relative to that repository. If the argument contains a repository name, the current label is ignored and the argument is returned as-is, except that the repository name is rewritten if it is in the current repository mapping. Reserved labels will also be returned as-is.
For example:
Label("//foo/bar:baz").relative(":quux") == Label("//foo/bar:quux")
Label("//foo/bar:baz").relative("//wiz:quux") == Label("//wiz:quux")
Label("@repo//foo/bar:baz").relative("//wiz:quux") == Label("@repo//wiz:quux")
Label("@repo//foo/bar:baz").relative("//visibility:public") == Label("//visibility:public")
Label("@repo//foo/bar:baz").relative("@other//wiz:quux") == Label("@other//wiz:quux")

If the repository mapping passed in is {'@other' : '@remapped'}, then the following remapping will take place:

Label("@repo//foo/bar:baz").relative("@other//wiz:quux") == Label("@remapped//wiz:quux")

Parameters

Parameter Description
relName required
The label that will be resolved relative to this one.

workspace_name

string Label.workspace_name

The repository part of this label. For instance,
Label("@foo//bar:baz").workspace_name == "foo"

workspace_root

string Label.workspace_root

Returns the execution root for the workspace of this label, relative to the execroot. For instance:
Label("@repo//pkg/foo:abc").workspace_root == "external/repo"