Skip to content

Commit d44c3f9

Browse files
lberkicopybara-github
authored andcommitted
Accept labels of aliases in config_setting.
Previously, if a config_setting referenced a Starlark setting through an alias, it was always evaluated as if the setting had its default value. Now, it is evaluated correctly. This is done by looking up the value of the build setting in the configuration based on its own label as specified in BuildSettingProvider. Fixes #13463 . RELNOTES: None. PiperOrigin-RevId: 585658985 Change-Id: Id534cd95282355f1143302bf703145bb53708a41
1 parent 67021ff commit d44c3f9

3 files changed

Lines changed: 58 additions & 4 deletions

File tree

src/main/java/com/google/devtools/build/lib/rules/config/ConfigSetting.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,14 @@ static UserDefinedFlagMatch fromAttributeValueAndPrerequisites(
497497
} else if (target.satisfies(BuildSettingProvider.REQUIRE_BUILD_SETTING_PROVIDER)) {
498498
// build setting
499499
BuildSettingProvider provider = target.getProvider(BuildSettingProvider.class);
500-
Object configurationValue =
501-
optionDetails.getOptionValue(specifiedLabel) != null
502-
? optionDetails.getOptionValue(specifiedLabel)
503-
: provider.getDefaultValue();
500+
501+
Object configurationValue;
502+
if (optionDetails.getOptionValue(provider.getLabel()) != null) {
503+
configurationValue = optionDetails.getOptionValue(provider.getLabel());
504+
} else {
505+
configurationValue = provider.getDefaultValue();
506+
}
507+
504508
Object convertedSpecifiedValue;
505509
try {
506510
// We don't need to supply a base package or repo mapping for the conversion here,

src/test/java/com/google/devtools/build/lib/rules/LabelBuildSettingTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,35 @@ public void testLabelFlag_set() throws Exception {
116116
assertThat(b.get("value")).isEqualTo("command_line_value");
117117
}
118118

119+
@Test
120+
public void withSelectThroughAlias() throws Exception {
121+
writeRulesBzl("flag");
122+
scratch.file(
123+
"test/BUILD",
124+
"load('//test:rules.bzl', 'my_rule', 'simple_rule')",
125+
"simple_rule(name = 'default', value = 'default_value')",
126+
"simple_rule(name = 'command_line', value = 'command_line_value')",
127+
"label_flag(name = 'my_label_flag', build_setting_default = ':default')",
128+
"alias(name = 'my_label_flag_alias', actual = ':my_label_flag')",
129+
"config_setting(",
130+
" name = 'is_default_label',",
131+
" flag_values = {':my_label_flag_alias': '//test:default'}",
132+
")",
133+
"simple_rule(name = 'selector', value = select({':is_default_label': 'valid'}))");
134+
135+
useConfiguration();
136+
getConfiguredTarget("//test:selector");
137+
assertNoEvents();
138+
139+
reporter.removeHandler(failFastHandler);
140+
useConfiguration(
141+
ImmutableMap.of(
142+
"//test:my_label_flag", Label.parseCanonicalUnchecked("//test:command_line")));
143+
getConfiguredTarget("//test:selector");
144+
assertContainsEvent(
145+
"configurable attribute \"value\" in //test:selector doesn't match this configuration");
146+
}
147+
119148
@Test
120149
public void withSelect() throws Exception {
121150
writeRulesBzl("flag");

src/test/java/com/google/devtools/build/lib/rules/config/ConfigSettingTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,27 @@ public void ruleLicensesUsed() throws Exception {
21772177
assertThat(getLicenses("//test:match")).containsExactly(LicenseType.NONE);
21782178
}
21792179

2180+
@Test
2181+
public void aliasedStarlarkFlag() throws Exception {
2182+
scratch.file(
2183+
"test/flagdef.bzl",
2184+
"def _impl(ctx):",
2185+
" return []",
2186+
"my_flag = rule(",
2187+
" implementation = _impl,",
2188+
" build_setting = config.string(flag = True))");
2189+
2190+
scratch.file(
2191+
"test/BUILD",
2192+
"load('//test:flagdef.bzl', 'my_flag')",
2193+
"my_flag(name = 'flag', build_setting_default = 'default')",
2194+
"alias(name = 'alias', actual = ':flag')",
2195+
"config_setting(name = 'alias_setting', flag_values = {':alias': 'specified'})");
2196+
2197+
useConfiguration(ImmutableMap.of("//test:flag", "specified"));
2198+
assertThat(getConfigMatchingProviderResultAsBoolean("//test:alias_setting")).isTrue();
2199+
}
2200+
21802201
@Test
21812202
public void simpleStarlarkFlag() throws Exception {
21822203
scratch.file(

0 commit comments

Comments
 (0)