Skip to content

Commit

Permalink
[6.4.0] Improve error when a label is provided in config_setting's …
Browse files Browse the repository at this point in the history
…`values` (#19484)

Suggest to use `flag_values` instead. Previously, this only resulted in
an ambiguous "Unrecognized option" error.

Closes #19464.

Commit
bd5dbc5

PiperOrigin-RevId: 564367170
Change-Id: Ibe9397be4300783ab6e5b5de9e08e40142de0b4c

Co-authored-by: Fabian Meumertzheim <[email protected]>
  • Loading branch information
bazel-io and fmeum authored Sep 11, 2023
1 parent 6052b8b commit 5a0339f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;

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

Optional<String> likelyLabelInvalidSetting =
attributes.get(ConfigSettingRule.SETTINGS_ATTRIBUTE, Type.STRING_DICT).keySet().stream()
.filter(s -> s.startsWith("@") || s.startsWith("//") || s.startsWith(":"))
.findFirst();
if (likelyLabelInvalidSetting.isPresent()) {
ruleContext.attributeError(
ConfigSettingRule.SETTINGS_ATTRIBUTE,
String.format(
"'%s' is not a valid setting name, but appears to be a label. Did you mean to place"
+ " it in %s instead?",
likelyLabelInvalidSetting.get(), ConfigSettingRule.FLAG_SETTINGS_ATTRIBUTE));
return null;
}

// Get the built-in Blaze flag settings that match this rule.
ImmutableMultimap<String, String> nativeFlagSettings =
ImmutableMultimap.<String, String>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2317,4 +2317,20 @@ public void singleValueThatLooksLikeMultiValueIsOkay() throws Exception {
assertThat(getConfiguredTarget("//test:fg")).isNotNull();
assertNoEvents();
}

@Test
public void labelInValuesError() throws Exception {
scratch.file(
"test/BUILD",
"config_setting(",
" name = 'match',",
" values = {'//foo:bar': 'value'},",
")");
reporter.removeHandler(failFastHandler); // expect errors
assertThat(getConfiguredTarget("//test:match")).isNull();
assertContainsEvent(
"in values attribute of config_setting rule //test:match: '//foo:bar' is"
+ " not a valid setting name, but appears to be a label. Did you mean to place it in"
+ " flag_values instead?");
}
}

0 comments on commit 5a0339f

Please sign in to comment.