Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alias_label struct field to all targets #18100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 getOriginalLabel();
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");
}
}