Skip to content

Commit db68419

Browse files
fmeumcopybara-github
authored andcommitted
Allow map_each to return None in TemplateDict#add_joined
Mimics the behavior of `Args#add_all`'s `map_each` callback and can be used to skip over values. Also improves the error message when the callback returns an unexpected type by including the value. Closes bazelbuild#16924. PiperOrigin-RevId: 493609265 Change-Id: Ia9e42dd7edc2928a9945ce647d638fcb94037bc8
1 parent 011e108 commit db68419

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/main/java/com/google/devtools/build/lib/analysis/starlark/TemplateDict.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ public String getValue() throws EvalException {
134134
/*kwargs=*/ ImmutableMap.of());
135135
if (ret instanceof String) {
136136
parts.add((String) ret);
137-
continue;
137+
} else if (ret != Starlark.NONE) {
138+
throw Starlark.errorf(
139+
"Function provided to map_each must return a String or None, but returned type "
140+
+ "%s for key '%s' and value: %s",
141+
Starlark.type(ret), getKey(), Starlark.repr(val));
138142
}
139-
throw Starlark.errorf(
140-
"Function provided to map_each must return a String, but returned type %s for key:"
141-
+ " %s",
142-
Starlark.type(ret), getKey());
143143
} catch (InterruptedException e) {
144144
// Report the error to the user, but the stack trace is not of use to them
145145
throw Starlark.errorf(

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/TemplateDictApi.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public interface TemplateDictApi extends StarlarkValue {
7171
named = true,
7272
positional = false,
7373
doc =
74-
"A Starlark function accepting a single argument and returning a String. This"
75-
+ " function is applied to each item of the depset specified in the"
76-
+ " <code>values</code> parameter"),
74+
"A Starlark function accepting a single argument and returning either a String or "
75+
+ "<code>None</code>. This function is applied to each item of the depset "
76+
+ "specified in the <code>values</code> parameter"),
7777
@Param(
7878
name = "uniquify",
7979
named = true,

src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleContextTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -3626,7 +3626,7 @@ public void testTemplateExpansionComputedSubstitution() throws Exception {
36263626
scratch.file(
36273627
"test/rules.bzl",
36283628
"def _artifact_to_basename(file):",
3629-
" return file.basename",
3629+
" return file.basename if file.basename != 'ignored.txt' else None",
36303630
"",
36313631
"def _undertest_impl(ctx):",
36323632
" template_dict = ctx.actions.template_dict()",
@@ -3657,7 +3657,7 @@ public void testTemplateExpansionComputedSubstitution() throws Exception {
36573657
"undertest_rule(",
36583658
" name = 'undertest',",
36593659
" template = ':template.txt',",
3660-
" srcs = ['foo.txt', 'bar.txt', 'baz.txt'],",
3660+
" srcs = ['foo.txt', 'bar.txt', 'baz.txt', 'ignored.txt'],",
36613661
")",
36623662
"testing_rule(",
36633663
" name = 'testing',",
@@ -3914,8 +3914,8 @@ public void testTemplateExpansionComputedSubstitutionMapEachBadReturnType() thro
39143914
assertThat(evalException)
39153915
.hasMessageThat()
39163916
.isEqualTo(
3917-
"Function provided to map_each must return a String, but returned type Label for key:"
3918-
+ " %files%");
3917+
"Function provided to map_each must return a String or None, but returned "
3918+
+ "type Label for key '%files%' and value: <source file test/template.txt>");
39193919
}
39203920

39213921
@Test

0 commit comments

Comments
 (0)