Skip to content

Commit

Permalink
Add alias_label struct field to all targets
Browse files Browse the repository at this point in the history
This allows rules to obtain the label of an `alias` target rather than
only the label of the target an alias chain ultimately resolves to. The
former is the label as specified by the user on the rule, which can be
important to know for e.g. fixup messages and manual implementations of
location expansion.
  • Loading branch information
fmeum committed Apr 14, 2023
1 parent ca30372 commit e11c1b4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public interface ConfiguredTarget extends TransitiveInfoCollection, Structure {
/** All <code>ConfiguredTarget</code>s have a "label" field. */
String LABEL_FIELD = "label";

/** All <code>ConfiguredTarget</code>s have an "alias_label" field. */
String ALIAS_LABEL_FIELD = "alias_label";

/** All <code>ConfiguredTarget</code>s have a "files" field. */
String FILES_FIELD = "files";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public abstract class AbstractConfiguredTarget implements ConfiguredTarget, Visi
// attributed to normal user-specified providers).
private static final ImmutableSet<String> SPECIAL_FIELD_NAMES =
ImmutableSet.of(
ALIAS_LABEL_FIELD,
LABEL_FIELD,
FILES_FIELD,
DEFAULT_RUNFILES_FIELD,
Expand Down Expand Up @@ -141,6 +142,9 @@ public Object getValue(String name) {
switch (name) {
case LABEL_FIELD:
return getLabel();
case ALIAS_LABEL_FIELD:
// Overridden in AliasConfiguredTarget.
return Starlark.NONE;
case ACTIONS_FIELD_NAME:
// Depending on subclass, the 'actions' field will either be unsupported or of type
// java.util.List, which needs to be converted to Sequence before being returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,19 @@ public BuildConfigurationKey getConfigurationKey() {

@Override
public Object getValue(String name) {
if (name.equals(LABEL_FIELD)) {
return getLabel();
} else if (name.equals(FILES_FIELD)) {
// A shortcut for files to build in Starlark. FileConfiguredTarget and RuleConfiguredTarget
// always has FileProvider and Error- and PackageGroupConfiguredTarget-s shouldn't be
// accessible in Starlark.
return Depset.of(Artifact.class, getProvider(FileProvider.class).getFilesToBuild());
switch (name) {
case LABEL_FIELD:
return getLabel();
case ALIAS_LABEL_FIELD:
return label;
case FILES_FIELD:
// A shortcut for files to build in Starlark. FileConfiguredTarget and RuleConfiguredTarget
// always has FileProvider and Error- and PackageGroupConfiguredTarget-s shouldn't be
// accessible in Starlark.
return Depset.of(Artifact.class, getProvider(FileProvider.class).getFilesToBuild());
default:
return actual.getValue(name);
}
return actual.getValue(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@
+ "<ul>\n" //
+ "<li><h3 id='modules.Target.label'>label</h3>\n" //
+ "<code><a href='../builtins/Label.html'>Label</a> Target.label</code><br/>\n" //
+ "The identifier of the target.</li>\n" //
+ "The label of the target after following all <code><a " //
+ "href='general.html#alias>alias</a></code>es.</li>\n" //
//
+ "<ul>\n" //
+ "<li><h3 id='modules.Target.alias_label'>alias_label</h3>\n" //
+ "<code><a href='../builtins/Label.html'>Label</a> Target.alias_label</code><br/>\n" //
+ "The label of the target if it is an <code><a" //
+ "href='general.html#alias>alias</a></code>, otherwise <code>None</code>.</li>\n" //
//
+ "<li><h3 id='modules.Target.files'>files</h3>\n" //
+ "<code><a href='../builtins/depset.html'>depset</a> Target.files </code><br/>\n" //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4062,4 +4062,37 @@ public void testTemplateExpansionComputedSubstitutionMapEachMustBeTopLevel() thr

checkError("//test:testing", "must be declared by a top-level def statement");
}

@Test
public void testTargetAliasLabel() throws Exception {
scratch.file(
"test/rules.bzl",
"def _my_rule_impl(ctx):",
" direct_dep = ctx.attr.deps[0]",
" direct_dep.label == Label('//test:target') or fail()",
" direct_dep.alias_label == None or fail()",
" alias_dep = ctx.attr.deps[1]",
" alias_dep.label == Label('//test:target') or fail()",
" alias_dep.alias_label == Label('//test:alias') or fail()",
"my_rule = rule(",
" implementation = _my_rule_impl,",
" attrs = {'deps': attr.label_list()},",
")");
scratch.file(
"test/BUILD",
"load(':rules.bzl', 'my_rule')",
"my_rule(",
" name = 'my_rule',",
" deps = [",
" ':target',",
" ':alias',",
" ],",
")",
"",
"cc_binary(name = 'target')",
"alias(name = 'alias', actual = ':further_alias')",
"alias(name = 'further_alias', actual = ':target')");

getConfiguredTarget("//test:my_rule");
}
}

0 comments on commit e11c1b4

Please sign in to comment.