Back to Bazel

Label

docs/versions/6.3.0/rules/lib/Label.mdx

9.1.15.8 KB
Original Source
<html devsite> <head> <meta name="project_path" value="/_project.yaml"> <meta name="book_path" value="/versions/6.3.0/_book.yaml"> </head> <body> <h1 class="page-title" id="modules.Label">Label</h1> <!-- {% raw %} -->

A BUILD target identifier.

<h2>Members</h2> <ul> <li> <a href="#Label">Label</a> </li> <li> <a href="#name">name</a> </li> <li> <a href="#package">package</a> </li> <li> <a href="#relative">relative</a> </li> <li> <a href="#workspace_name">workspace_name</a> </li> <li> <a href="#workspace_root">workspace_root</a> </li> </ul>
  <h2 id="Label">Label</h2>
    <p><pre class="rule-signature"><a class="anchor" href="Label.html">Label</a> Label(input)</pre></p>

Converts a label string into a <code>Label</code> object, in the context of the package where the calling <code>.bzl</code> source file lives. If the given value is already a <code>Label</code>, it is returned unchanged.<p>For macros, a related function, <code><a href='native#package_relative_label'>native.package_relative_label()</a></code>, converts the input into a <code>Label</code> 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.

      <!-- hide-from-toc is a class used by DevSite for the public Bazel site
  (https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
  <h3 class="hide-from-toc">Parameters</h3>
  <table class="table table-bordered table-condensed table-params">
    <colgroup>
      <col class="col-param">
      <col class="param-description">
    </colgroup>
    <thead>
      <tr>
        <th>Parameter</th>
        <th>Description</th>
      </tr>
    </thead>
    <tbody>
              <tr>
        <td id="Label.input">
          <code>input</code>
        </td>
        <td>
                      <code><a class="anchor" href="string.html">string</a>; or <a class="anchor" href="Label.html">Label</a></code>;
                                 required

                      The input label string or Label object. If a Label object is passed, it's returned as is.
        </td>
      </tr>
            </tbody>
  </table>

    <h2 id="name">name</h2>
    <p><pre class="rule-signature"><a class="anchor" href="string.html">string</a> Label.name</pre></p>

The name of this label within the package. For instance:
<pre class=language-python>Label("//pkg/foo:abc").name == "abc"</pre>
    <h2 id="package">package</h2>
    <p><pre class="rule-signature"><a class="anchor" href="string.html">string</a> Label.package</pre></p>

The package part of this label. For instance:
<pre class=language-python>Label("//pkg/foo:abc").package == "pkg/foo"</pre>
    <h2 id="relative">relative</h2>
    <p><pre class="rule-signature"><a class="anchor" href="Label.html">Label</a> Label.relative(relName)</pre></p>

Resolves a label that is either absolute (starts with <code>//</code>) 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:

<pre class=language-python> 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") </pre><p>If the repository mapping passed in is <code>{'@other' : '@remapped'}</code>, then the following remapping will take place: <pre class=language-python> Label("@repo//foo/bar:baz").relative("@other//wiz:quux") == Label("@remapped//wiz:quux") </pre>
      <!-- hide-from-toc is a class used by DevSite for the public Bazel site
  (https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
  <h3 class="hide-from-toc">Parameters</h3>
  <table class="table table-bordered table-condensed table-params">
    <colgroup>
      <col class="col-param">
      <col class="param-description">
    </colgroup>
    <thead>
      <tr>
        <th>Parameter</th>
        <th>Description</th>
      </tr>
    </thead>
    <tbody>
              <tr>
        <td id="relative.relName">
          <code>relName</code>
        </td>
        <td>
                                 required

                      The label that will be resolved relative to this one.
        </td>
      </tr>
            </tbody>
  </table>

    <h2 id="workspace_name">workspace_name</h2>
    <p><pre class="rule-signature"><a class="anchor" href="string.html">string</a> Label.workspace_name</pre></p>

The repository part of this label. For instance, <pre class=language-python>Label("@foo//bar:baz").workspace_name == "foo"</pre>


    <h2 id="workspace_root">workspace_root</h2>
    <p><pre class="rule-signature"><a class="anchor" href="string.html">string</a> Label.workspace_root</pre></p>

Returns the execution root for the workspace of this label, relative to the execroot. For instance:
<pre class=language-python>Label("@repo//pkg/foo:abc").workspace_root == "external/repo"</pre> </body> </html> <!-- {% endraw %} -->