Back to Bazel

Label

docs/versions/8.1.0/rules/lib/builtins/Label.mdx

9.1.09.6 KB
Original Source
<html devsite> <head> <meta name="project_path" value="/_project.yaml"> <meta name="book_path" value="/versions/8.1.0/_book.yaml"> </head> <body> <h1 class="page-title" id="modules.Label">Label</h1>

{% dynamic setvar source_file "src/main/java/com/google/devtools/build/lib/cmdline/Label.java" %} {% dynamic setvar version "8.1.0" %} {% dynamic setvar original_path "/rules/lib/builtins/Label" %} {% include "_buttons.html" %}

<!-- {% raw %} -->

A BUILD target identifier.<p>For every <code>Label</code> instance <code>l</code>, the string representation <code>str(l)</code> has the property that <code>Label(str(l)) == l</code>, regardless of where the <code>Label()</code> call occurs.<p>When passed as positional arguments to <code>print()</code> or <code>fail()</code>, <code>Label</code> use a string representation optimized for human readability instead. This representation uses an <a href="/versions/8.1.0/external/overview#apparent-repo-name">apparent repository name</a> from the perspective of the main repository if possible.

<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="#repo_name">repo_name</a> </li> <li> <a href="#same_package_label">same_package_label</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="../builtins/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='../toplevel/native.html#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>
                      <a class="anchor" href="../core/string.html">string</a>; or <a class="anchor" href="../builtins/Label.html">Label</a>;
                                 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="../core/string.html">string</a> Label.name</pre></p>

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

The name of the package containing the target referred to by this label, without the repository name. For instance:
<pre class=language-python>Label("@@repo//pkg/foo:abc").package == "pkg/foo"</pre>
    <h2 id="relative">relative</h2>
    <p><pre class="rule-signature"><a class="anchor" href="../builtins/Label.html">Label</a> Label.relative(relName)</pre></p>

<b>Experimental</b>. This API is experimental and may change at any time. Please do not depend on it. It may be enabled on an experimental basis by setting <code>--+incompatible_enable_deprecated_label_apis</code> 

<strong>Deprecated.</strong> This method behaves surprisingly when used with an argument containing an apparent repo name. Prefer <a href="#same_package_label"><code>Label.same_package_label()</code></a>, <a href="../toplevel/native.html#package_relative_label"><code>native.package_relative_label()</code></a>, or <a href="#Label"><code>Label()</code></a> instead.<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>
                      <a class="anchor" href="../core/string.html">string</a>;
                                 required

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

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

The canonical name of the repository containing the target referred to by this label, without any leading at-signs (<code>@</code>). For instance, <pre class=language-python>Label("@@foo//bar:baz").repo_name == "foo"</pre>


    <h2 id="same_package_label">same_package_label</h2>
    <p><pre class="rule-signature"><a class="anchor" href="../builtins/Label.html">Label</a> Label.same_package_label(target_name)</pre></p>

Creates a label in the same package as this label with the given target name.

      <!-- 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="same_package_label.target_name">
          <code>target_name</code>
        </td>
        <td>
                      <a class="anchor" href="../core/string.html">string</a>;
                                 required

                      The target name of the new label.
        </td>
      </tr>
            </tbody>
  </table>

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

<b>Experimental</b>. This API is experimental and may change at any time. Please do not depend on it. It may be enabled on an experimental basis by setting <code>--+incompatible_enable_deprecated_label_apis</code> 

<strong>Deprecated.</strong> The field name "workspace name" is a misnomer here; use the identically-behaving <a href="#repo_name"><code>Label.repo_name</code></a> instead.<p>The canonical name of the repository containing the target referred to by this label, without any leading at-signs (<code>@</code>). 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="../core/string.html">string</a> Label.workspace_root</pre></p>

Returns the execution root for the repository containing the target referred to by 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 %} -->