Skip to content

Commit

Permalink
Throw exception if 'None' value given to List-type command line optio…
Browse files Browse the repository at this point in the history
…ns in Bazel transitions

* Proposed fix for #12559
* Command line options specified as Java List types should default to empty when given 'None' Starlark value for Bazel transitions

Closes #13286.

PiperOrigin-RevId: 371860821
  • Loading branch information
tobiade authored and copybara-github committed May 4, 2021
1 parent bce6d26 commit ced45af
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ private static BuildOptions applyTransition(
.convert(
optionValueAsList.stream().map(Object::toString).collect(joining(",")));
}
} else if (def.getType() == List.class && optionValue == null) {
throw ValidationException.format(
"'None' value not allowed for List-type option '%s'. Please use '[]' instead if"
+ " trying to set option to empty value.",
optionName);
} else if (optionValue == null || def.getType().isInstance(optionValue)) {
convertedValue = optionValue;
} else if (def.getType().equals(boolean.class) && optionValue instanceof Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,37 @@ public void successfulTypeConversionOfNativeListOptionEmptyList() throws Excepti
assertThat(getConfiguration(ct).getOptions().get(CppOptions.class).fissionModes).isEmpty();
}

@Test
public void failedTypeConversionOfNativeListOptionNone() throws Exception {
writeAllowlistFile();
scratch.file(
"test/transitions.bzl",
"def _impl(settings, attr):",
" return {'//command_line_option:copt': None}",
"my_transition = transition(implementation = _impl, inputs = [],",
" outputs = ['//command_line_option:copt'])");
scratch.file(
"test/rules.bzl",
"load('//test:transitions.bzl', 'my_transition')",
"def _impl(ctx):",
" return []",
"my_rule = rule(",
" implementation = _impl,",
" cfg = my_transition,",
" attrs = {",
" '_allowlist_function_transition': attr.label(",
" default = '//tools/allowlists/function_transition_allowlist',",
" ),",
" })");
scratch.file("test/BUILD", "load('//test:rules.bzl', 'my_rule')", "my_rule(name = 'test')");

reporter.removeHandler(failFastHandler);
getConfiguredTarget("//test");
assertContainsEvent(
"'None' value not allowed for List-type option 'copt'. Please use '[]' instead if trying"
+ " to set option to empty value.");
}

@Test
public void starlarkPatchTransitionRequiredFragments() throws Exception {
// All Starlark rule transitions are patch transitions, while all Starlark attribute transitions
Expand Down

0 comments on commit ced45af

Please sign in to comment.