Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a dummy toolchain context for rules that don't have one. #13162

Closed
wants to merge 4 commits into from

Conversation

katre
Copy link
Member

@katre katre commented Mar 5, 2021

Fixes #12610.

@katre katre requested a review from brandjon March 5, 2021 19:05
@google-cla google-cla bot added the cla: yes label Mar 5, 2021
@@ -2034,6 +2034,35 @@ EOF
expect_not_log "does not provide ToolchainTypeInfo"
}

function test_toolchain_from_no_toolchain_rule() {
# Create a build_setting that tries to load a toolchain.
# build_settings cnanot actually have toolchains because they
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed entirely.

cat >> demo/rule.bzl <<EOF
def _sample_impl(ctx):
info = ctx.toolchains["//:toolchain_type"]
if info:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if info != None:? To distinguish from other false-y values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in the unit test.

@@ -2034,6 +2034,35 @@ EOF
expect_not_log "does not provide ToolchainTypeInfo"
}

function test_toolchain_from_no_toolchain_rule() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of testing this at both the unit test and shell test levels?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I wrote this first then added the other to debug, but we only need one. Removed the shell test.

// Starlark rules are easier if this cannot be null, so return a no-op value instead.
return new ToolchainContextApi() {
@Override
public Object getIndex(StarlarkSemantics semantics, Object key) throws EvalException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You can omit the throws declaration since this override doesn't throw. Same below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

ResolvedToolchainContext toolchainContext = ruleContext.getToolchainContext();
if (toolchainContext == null) {
// Starlark rules are easier if this cannot be null, so return a no-op value instead.
return new ToolchainContextApi() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optionally make this a singleton, but I doubt it'll matter much.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's enough benefit, this is fairly clear.

@bazel-io bazel-io closed this in 52b1b74 Mar 8, 2021
katre added a commit to katre/bazel that referenced this pull request Jul 13, 2021
@katre katre deleted the i12610-toolchain-crash branch April 25, 2023 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Starlark build setting invoking go_context throws an exception
2 participants