Skip to content

Commit

Permalink
Update the no-op ToolchainContext to throw an EvalException.
Browse files Browse the repository at this point in the history
This is only possible from starlark rules with no toolchain context, which are typically build settings. Such rules should not attempt to use toolchains as they are part of configuration.

PiperOrigin-RevId: 368200348
  • Loading branch information
katre authored and copybara-github committed Apr 13, 2021
1 parent 04ab8d0 commit dfe2a31
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public abstract class StarlarkToolchainContext implements ToolchainContextApi {
new ToolchainContextApi() {
@Override
public Object getIndex(
StarlarkThread starlarkThread, StarlarkSemantics semantics, Object key) {
// TODO(jcater): throw Starlark.errorf instead of returning NONE.
return Starlark.NONE;
StarlarkThread starlarkThread, StarlarkSemantics semantics, Object key)
throws EvalException {
throw Starlark.errorf("Toolchains are not valid in this context");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3233,14 +3233,15 @@ public void testNoToolchainContext() throws Exception {
scratch.file(
"test/rule.bzl",
"def _sample_impl(ctx):",
" info = ctx.toolchains['//:toolchain_type']",
" if info != None:",
" fail('Toolchain should be empty')",
" # This should raise an error.",
" ctx.toolchains['//:toolchain_type']",
" fail('Toolchain was not empty')",
"sample_setting = rule(",
" implementation = _sample_impl,",
" build_setting = config.bool(flag = True),",
")");
getConfiguredTarget("//test:test");
assertNoEvents();
assertThrows(AssertionError.class, () -> getConfiguredTarget("//test:test"));
assertContainsEvent("Toolchains are not valid in this context");
assertDoesNotContainEvent("Toolchain was not empty");
}
}

0 comments on commit dfe2a31

Please sign in to comment.