Skip to content

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

Closed
katre wants to merge 4 commits intobazelbuild:masterfrom
katre:i12610-toolchain-crash
Closed

Use a dummy toolchain context for rules that don't have one.#13162
katre wants to merge 4 commits intobazelbuild:masterfrom
katre:i12610-toolchain-crash

Conversation

@katre
Copy link
Copy Markdown
Collaborator

@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
Comment thread src/test/shell/bazel/toolchain_test.sh Outdated

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

typo

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Removed entirely.

Comment thread src/test/shell/bazel/toolchain_test.sh Outdated
cat >> demo/rule.bzl <<EOF
def _sample_impl(ctx):
info = ctx.toolchains["//:toolchain_type"]
if info:
Copy link
Copy Markdown
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
Copy Markdown
Collaborator 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.

Comment thread src/test/shell/bazel/toolchain_test.sh Outdated
expect_not_log "does not provide ToolchainTypeInfo"
}

function test_toolchain_from_no_toolchain_rule() {
Copy link
Copy Markdown
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
Copy Markdown
Collaborator 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
Copy Markdown
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
Copy Markdown
Collaborator 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
Copy Markdown
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
Copy Markdown
Collaborator 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 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