Skip to content

Commit

Permalink
[7.4.0] Convert values to Starlark values when concating string_list_…
Browse files Browse the repository at this point in the history
…dict type (#23603)

Convert values to Starlark values when concating string_list_dict type.

This is because Dict checks that all the values put in it are Starlark
valid values. But string_list_dict value is of type List<String>, when
Java's List isn't a Starlark value.

Fixes #23065

My first PR to bazel 🤩 Would love you to review the PR.
 Checked that the test didn't pass before the change and did after it.

Closes #23424.

PiperOrigin-RevId: 673463108
Change-Id: Ib2cdce7d94243f4e3bda23203a196fee4e766189

Commit
f1f8d58

---------

Co-authored-by: Lior Gorelik <[email protected]>
Co-authored-by: Alexandre Rostovtsev <[email protected]>
  • Loading branch information
3 people authored Sep 11, 2024
1 parent 06f99bb commit 71e2578
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,11 @@ public Object copyAndLiftStarlarkValue(

@Override
public Map<KeyT, ValueT> concat(Iterable<Map<KeyT, ValueT>> iterable) {
Dict.Builder<KeyT, ValueT> output = new Dict.Builder<>();
ImmutableMap.Builder<KeyT, ValueT> builder = ImmutableMap.builder();
for (Map<KeyT, ValueT> map : iterable) {
output.putAll(map);
builder.putAll(map);
}
return output.buildImmutable();
return builder.buildKeepingLast();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.devtools.build.lib.testutil.TestRuleClassProvider;
import com.google.devtools.build.lib.util.FileTypeSet;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import org.junit.Before;
Expand Down Expand Up @@ -164,6 +165,12 @@ public Object getDefault(AttributeMap rule) {
.add(attr("deps", LABEL_LIST).allowedFileTypes())
.useToolchainResolution(ToolchainResolutionMode.DISABLED));

private static final MockRule RULE_WITH_STRING_LIST_DICT_ATTR =
() ->
MockRule.define(
"rule_with_string_list_dict_attr",
attr("string_list_dict_attr", Type.STRING_LIST_DICT));

@Override
protected ConfiguredRuleClassProvider createRuleClassProvider() {
ConfiguredRuleClassProvider.Builder builder =
Expand All @@ -173,7 +180,8 @@ protected ConfiguredRuleClassProvider createRuleClassProvider() {
.addRuleDefinition(RULE_WITH_BOOLEAN_ATTR)
.addRuleDefinition(RULE_WITH_ALLOWED_VALUES)
.addRuleDefinition(RULE_WITH_LABEL_DEFAULT)
.addRuleDefinition(RULE_WITH_NO_PLATFORM);
.addRuleDefinition(RULE_WITH_NO_PLATFORM)
.addRuleDefinition(RULE_WITH_STRING_LIST_DICT_ATTR);
TestRuleClassProvider.addStandardRules(builder);
// Allow use of --foo as a dummy flag
builder.addConfigurationFragment(DummyTestFragment.class);
Expand Down Expand Up @@ -1927,4 +1935,24 @@ public void selectWithLabelKeysInMacro() throws Exception {
/*expected:*/ ImmutableList.of("bin java/foo/libb.jar", "bin java/foo/libb2.jar"),
/*not expected:*/ ImmutableList.of("bin java/foo/liba.jar", "bin java/foo/liba2.jar"));
}

@Test
public void stringListDictTypeConcatConfigurable() throws Exception {
writeConfigRules();
scratch.file(
"foo/BUILD",
"""
rule_with_string_list_dict_attr(
name = 'rule',
string_list_dict_attr = {'a': ['a.out']} | select({
'//conditions:b': {'b': ['b.out']},
}))
""");

useConfiguration("--foo=b");
ConfiguredTargetAndData ctad = getConfiguredTargetAndData("//foo:rule");
AttributeMap attributes = getMapperFromConfiguredTargetAndTarget(ctad);
assertThat(attributes.get("string_list_dict_attr", Type.STRING_LIST_DICT))
.containsExactly("a", Arrays.asList("a.out"), "b", Arrays.asList("b.out"));
}
}
29 changes: 29 additions & 0 deletions src/test/java/com/google/devtools/build/lib/packages/TypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,35 @@ public void testStringListDict() throws Exception {
assertThat(collectLabels(Type.STRING_LIST_DICT, converted)).isEmpty();
}

@Test
public void testStringListDict_concat() throws Exception {
assertThat(Type.STRING_LIST_DICT.concat(ImmutableList.of())).isEmpty();

ImmutableMap<String, List<String>> expected =
ImmutableMap.of(
"foo", Arrays.asList("foo", "bar"),
"wiz", Arrays.asList("bang"));
assertThat(Type.STRING_LIST_DICT.concat(ImmutableList.of(expected))).isEqualTo(expected);

ImmutableMap<String, List<String>> map1 =
ImmutableMap.of(
"foo", Arrays.asList("a", "b"),
"bar", Arrays.asList("c", "d"));
ImmutableMap<String, List<String>> map2 =
ImmutableMap.of(
"bar", Arrays.asList("x", "y"),
"baz", Arrays.asList("z"));

ImmutableMap<String, List<String>> expectedAfterConcat =
ImmutableMap.of(
"foo", Arrays.asList("a", "b"),
"bar", Arrays.asList("x", "y"),
"baz", Arrays.asList("z"));

assertThat(Type.STRING_LIST_DICT.concat(ImmutableList.of(map1, map2)))
.isEqualTo(expectedAfterConcat);
}

@Test
public void testStringListDictBadFirstElement() throws Exception {
Object input =
Expand Down

0 comments on commit 71e2578

Please sign in to comment.