Skip to content

Commit bd5dbc5

Browse files
fmeumcopybara-github
authored andcommitted
Improve error when a label is provided in config_setting's values
Suggest to use `flag_values` instead. Previously, this only resulted in an ambiguous "Unrecognized option" error. Closes #19464. PiperOrigin-RevId: 564367170 Change-Id: Ibe9397be4300783ab6e5b5de9e08e40142de0b4c
1 parent 06e370e commit bd5dbc5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import java.util.LinkedHashMap;
6868
import java.util.List;
6969
import java.util.Map;
70+
import java.util.Optional;
7071
import javax.annotation.Nullable;
7172

7273
/**
@@ -83,6 +84,20 @@ public ConfiguredTarget create(RuleContext ruleContext)
8384
throws InterruptedException, ActionConflictException {
8485
AttributeMap attributes = NonconfigurableAttributeMapper.of(ruleContext.getRule());
8586

87+
Optional<String> likelyLabelInvalidSetting =
88+
attributes.get(ConfigSettingRule.SETTINGS_ATTRIBUTE, Type.STRING_DICT).keySet().stream()
89+
.filter(s -> s.startsWith("@") || s.startsWith("//") || s.startsWith(":"))
90+
.findFirst();
91+
if (likelyLabelInvalidSetting.isPresent()) {
92+
ruleContext.attributeError(
93+
ConfigSettingRule.SETTINGS_ATTRIBUTE,
94+
String.format(
95+
"'%s' is not a valid setting name, but appears to be a label. Did you mean to place"
96+
+ " it in %s instead?",
97+
likelyLabelInvalidSetting.get(), ConfigSettingRule.FLAG_SETTINGS_ATTRIBUTE));
98+
return null;
99+
}
100+
86101
// Get the built-in Blaze flag settings that match this rule.
87102
ImmutableMultimap<String, String> nativeFlagSettings =
88103
ImmutableMultimap.<String, String>builder()

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,4 +2317,20 @@ public void singleValueThatLooksLikeMultiValueIsOkay() throws Exception {
23172317
assertThat(getConfiguredTarget("//test:fg")).isNotNull();
23182318
assertNoEvents();
23192319
}
2320+
2321+
@Test
2322+
public void labelInValuesError() throws Exception {
2323+
scratch.file(
2324+
"test/BUILD",
2325+
"config_setting(",
2326+
" name = 'match',",
2327+
" values = {'//foo:bar': 'value'},",
2328+
")");
2329+
reporter.removeHandler(failFastHandler); // expect errors
2330+
assertThat(getConfiguredTarget("//test:match")).isNull();
2331+
assertContainsEvent(
2332+
"in values attribute of config_setting rule //test:match: '//foo:bar' is"
2333+
+ " not a valid setting name, but appears to be a label. Did you mean to place it in"
2334+
+ " flag_values instead?");
2335+
}
23202336
}

0 commit comments

Comments
 (0)