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

Crash when using module extension #18641

Closed
mtribiere opened this issue Jun 12, 2023 · 4 comments
Closed

Crash when using module extension #18641

mtribiere opened this issue Jun 12, 2023 · 4 comments
Assignees
Labels
area-Bzlmod Bzlmod-specific PRs, issues, and feature requests help wanted Someone outside the Bazel team could own this P1 I'll work on this now. (Assignee required) team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. type: bug

Comments

@mtribiere
Copy link

Description of the bug:

While trying to use the Bazel Module extension feature, I found a crash and I have not idea what's causing it.
I'm configuring the module build_tools_reloaded (a custom toolchain) from the TEST_PARTITION project.

The crash :

Loading: 0 packages loaded
FATAL: bazel crashed due to an internal error. Printing stack trace:
java.lang.RuntimeException: Unrecoverable error while evaluating node 'SINGLE_EXTENSION_EVAL:ModuleExtensionId{bzlFileLabel=@build_tools_reloaded~override//extensions:extensions.bzl, extensionName=build_tools_reloaded}' (requested by nodes 'BZLMOD_REPO_RULE:@build_tools_reloaded~override~build_tools_reloaded~build_tools_reloaded')
        at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:642)
        at com.google.devtools.build.lib.concurrent.AbstractQueueVisitor$WrappedRunnable.run(AbstractQueueVisitor.java:382)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)
Caused by: net.starlark.java.eval.Starlark$UncheckedEvalException: IllegalStateException thrown during Starlark evaluation
        at <starlark>.init_helionix_toolchain(<builtin>:0)
        at <starlark>._build_tools_reloaded_impl(C:/tools/dev/bazel_install/6fao65yg/external/build_tools_reloaded~override/extensions/extensions.bzl:7)
Caused by: java.lang.IllegalStateException: Expected BazelStarlarkContext to be available in this Starlark thread
        at com.google.common.base.Preconditions.checkState(Preconditions.java:502)
        at com.google.devtools.build.lib.packages.BazelStarlarkContext.from(BazelStarlarkContext.java:77)
        at com.google.devtools.build.lib.analysis.starlark.StarlarkRuleClassFunctions$StarlarkRuleFunction.call(StarlarkRuleClassFunctions.java:776)
        at net.starlark.java.eval.StarlarkCallable.fastcall(StarlarkCallable.java:86)
        at net.starlark.java.eval.Starlark.fastcall(Starlark.java:638)
        at net.starlark.java.eval.Eval.evalCall(Eval.java:682)
        at net.starlark.java.eval.Eval.eval(Eval.java:497)
        at net.starlark.java.eval.Eval.exec(Eval.java:271)
        at net.starlark.java.eval.Eval.execStatements(Eval.java:82)
        at net.starlark.java.eval.Eval.execFunctionBody(Eval.java:66)
        at net.starlark.java.eval.StarlarkFunction.fastcall(StarlarkFunction.java:173)
        at net.starlark.java.eval.Starlark.fastcall(Starlark.java:638)
        at com.google.devtools.build.lib.bazel.bzlmod.SingleExtensionEvalFunction.compute(SingleExtensionEvalFunction.java:187)
        at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:571)
        at com.google.devtools.build.lib.concurrent.AbstractQueueVisitor$WrappedRunnable.run(AbstractQueueVisitor.java:382)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)

The command:

bazel build //:TEST_BIN --enable_bzlmod --override_module=build_tools_reloaded=..\BUILD_TOOLS_RELOADED  --platforms=@build_tools_reloaded//platforms:myPlatform 

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

TEST_PARTITION/MODULE.bazel:

[...]
bazel_dep(name = "build_tools_reloaded", version = "0.0.1", repo_name="build_tools_reloaded_unconfigured")
build_tools_reloaded = use_extension("@build_tools_reloaded_unconfigured//extensions:extensions.bzl", "build_tools_reloaded")
build_tools_reloaded.configure(includes=["TEST1","TEST2","TEST3"])
use_repo(build_tools_reloaded, "build_tools_reloaded")
[...]

build_tools_reloaded/extensions/extensions.bzl:

load("//toolchain/:rules.bzl", "init_my_toolchain")

def _build_tools_reloaded_impl(module_ctx):
    init_my_toolchain(arg1=["TEST4","TEST5","TEST6"])


_configure = tag_class(attrs = 
        {
            "includes": attr.string_list(),          
        })

build_tools_reloaded = module_extension(
    implementation = _build_tools_reloaded_impl,
    tag_classes = {
            "configure": _configure
    }
)

build_tools_reloaded/toolchain/rules.bzl:

def _init_my_toolchain_impl(ctx):
    print("RULE CALLED")


init_my_toolchain = rule(
    _init_my_toolchain_impl,
    attrs = {
        "arg1": attr.string_list()
    }
)

Which operating system are you running Bazel on?

Windows

What is the output of bazel info release?

release 6.2.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

Maybe similar to : #14974

Any other information, logs, or outputs that you want to share?

No response

@sgowroji sgowroji added team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. area-Bzlmod Bzlmod-specific PRs, issues, and feature requests labels Jun 12, 2023
@Wyverald
Copy link
Member

The cause of the crash is that you're calling a build rule (instead of a repo rule) from a module extension. init_my_toolchain should be a repository_rule.

Nevertheless, we shouldn't crash Bazel, but provide a helpful error message instead.

@Wyverald Wyverald added P1 I'll work on this now. (Assignee required) and removed untriaged labels Jun 12, 2023
@garymm
Copy link

garymm commented Jun 12, 2023

This code seems to cause a similar crash. Is the cause the same?

https://github.com/garymm/bazel-bugs/tree/master/llvm-toolchain-starlark

If not I can file another issue.

@Wyverald
Copy link
Member

That one looks different. Please file another issue.

@mtribiere
Copy link
Author

Oh alright, I see. How would you go about initializing a custom toolchain in a Module then? (Set includes, flags,...)

@meteorcloudy meteorcloudy added the help wanted Someone outside the Bazel team could own this label Jun 27, 2023
SalmaSamy added a commit that referenced this issue Jul 11, 2023
Fixes: #18641

PiperOrigin-RevId: 546092727
Change-Id: I66365813a40390ec61a1a0565b06655b3ca50fcd
keertk pushed a commit that referenced this issue Jul 11, 2023
Fixes: #18641

PiperOrigin-RevId: 546092727
Change-Id: I66365813a40390ec61a1a0565b06655b3ca50fcd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Bzlmod Bzlmod-specific PRs, issues, and feature requests help wanted Someone outside the Bazel team could own this P1 I'll work on this now. (Assignee required) team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. type: bug
Projects
None yet
Development

No branches or pull requests

6 participants