Save the Date: BazelCon 2023 will be held October 24-25 at Google Munich! Learn more


Report an issue View source

A BUILD target identifier.



Label Label(input)

Converts a label string into a Label object, in the context of the package where the calling .bzl source file lives. If the given value is already a Label, it is returned unchanged.

For macros, a related function, native.package_relative_label(), converts the input into a Label in the context of the package currently being constructed. Use that function to mimic the string-to-label conversion that is automatically done by label-valued rule attributes.


Parameter Description
input string; or Label; required
The input label string or Label object. If a Label object is passed, it's returned as is.



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


string Label.package

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


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")


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


string Label.workspace_name

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


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"