bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Crash when referencing "--action_env" command line option in settings parameter in transition implementation

Open nmosier opened this issue 2 years ago • 7 comments

Description of the bug:

I need to modify the --action_env settings when transitioning, but when I access them in my transition implementation like this

... = settings["//command_line_option:action_env"]

Bazel crashes with this internal error:

Starting local Bazel server and connecting to it...
Loading: 
Loading: 0 packages loaded
FATAL: bazel crashed due to an internal error. Printing stack trace:
net.starlark.java.eval.Starlark$UncheckedEvalException: IllegalArgumentException thrown during Starlark evaluation (//action-env-bug:foo.bzl)
	at <starlark>._foo_transition_impl(/usr/local/google/home/nmosier/tmp/bazel-bug/action-env-bug/foo.bzl:1)
Caused by: java.lang.IllegalArgumentException: invalid Starlark value: class com.google.common.collect.ImmutableEntry
	at net.starlark.java.eval.Starlark.checkValid(Starlark.java:112)
	at net.starlark.java.eval.StarlarkList.checkElemsValid(StarlarkList.java:176)
	at net.starlark.java.eval.StarlarkList.copyOf(StarlarkList.java:170)
	at net.starlark.java.eval.Starlark.fromJava(Starlark.java:188)
	at net.starlark.java.eval.EvalUtils.index(EvalUtils.java:456)
	at net.starlark.java.eval.Eval.evalIndex(Eval.java:718)
	at net.starlark.java.eval.Eval.eval(Eval.java:493)
	at net.starlark.java.eval.Eval.evalBinaryOperator(Eval.java:532)
	at net.starlark.java.eval.Eval.eval(Eval.java:479)
	at net.starlark.java.eval.Eval.evalCall(Eval.java:622)
	at net.starlark.java.eval.Eval.eval(Eval.java:489)
	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:191)
	at net.starlark.java.eval.Starlark.fastcall(Starlark.java:619)
	at com.google.devtools.build.lib.analysis.config.StarlarkDefinedConfigTransition$RegularTransition.evaluate(StarlarkDefinedConfigTransition.java:360)
	at com.google.devtools.build.lib.analysis.starlark.FunctionTransitionUtil.applyAndValidate(FunctionTransitionUtil.java:98)
	at com.google.devtools.build.lib.analysis.starlark.StarlarkRuleTransitionProvider$FunctionPatchTransition.patch(StarlarkRuleTransitionProvider.java:186)
	at com.google.devtools.build.lib.analysis.config.transitions.PatchTransition.apply(PatchTransition.java:75)
	at com.google.devtools.build.lib.analysis.config.transitions.ComposingTransition.apply(ComposingTransition.java:68)
	at com.google.devtools.build.lib.analysis.config.ConfigurationResolver.applyStarlarkTransition(ConfigurationResolver.java:505)
	at com.google.devtools.build.lib.analysis.config.ConfigurationResolver.applyTransitionWithoutSkyframe(ConfigurationResolver.java:440)
	at com.google.devtools.build.lib.skyframe.SkyframeExecutor.getConfigurations(SkyframeExecutor.java:2086)
	at com.google.devtools.build.lib.analysis.config.ConfigurationResolver.getConfigurationsFromExecutor(ConfigurationResolver.java:584)
	at com.google.devtools.build.lib.analysis.AnalysisUtils.getTargetsWithConfigs(AnalysisUtils.java:193)
	at com.google.devtools.build.lib.analysis.BuildView.update(BuildView.java:245)
	at com.google.devtools.build.lib.buildtool.AnalysisPhaseRunner.runAnalysisPhase(AnalysisPhaseRunner.java:217)
	at com.google.devtools.build.lib.buildtool.AnalysisPhaseRunner.execute(AnalysisPhaseRunner.java:129)
	at com.google.devtools.build.lib.buildtool.BuildTool.buildTargets(BuildTool.java:198)
	at com.google.devtools.build.lib.buildtool.BuildTool.processRequest(BuildTool.java:443)
	at com.google.devtools.build.lib.buildtool.BuildTool.processRequest(BuildTool.java:411)
	at com.google.devtools.build.lib.runtime.commands.BuildCommand.exec(BuildCommand.java:103)
	at com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.execExclusively(BlazeCommandDispatcher.java:585)
	at com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.exec(BlazeCommandDispatcher.java:231)
	at com.google.devtools.build.lib.server.GrpcServerImpl.executeCommand(GrpcServerImpl.java:550)
	at com.google.devtools.build.lib.server.GrpcServerImpl.lambda$run$1(GrpcServerImpl.java:614)
	at io.grpc.Context$1.run(Context.java:579)
	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)

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

action-env-bug.zip

$ unzip action-env-bug
$ cd action-env-bug
$ bazel build bin # SUCCESS
$ bazel build --action_env="X=1" bin # CRASH

Which operating system are you running Bazel on?

Linux (Debian)

What is the output of bazel info release?

release 5.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

Have you found anything relevant by searching the web?

No response

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

No response

nmosier avatar Jul 15 '22 18:07 nmosier

Hello @nmosier, Can you share the right code repo as we are not able to reproduce from the Above action-env-bug.zip.

sgowroji avatar Jul 18 '22 06:07 sgowroji

Sorry, I neglected to include the WORKSPACE file. This should work. action-env-bug.zip

$ unzip action-env-bug
$ cd action-env-bug
$ bazel build --action_env="X=1" bin # CRASH

nmosier avatar Jul 19 '22 04:07 nmosier

I see the below error logs

(base) sgowroji-macbookpro:action-env-bug sgowroji$ bazel build --action_env="X=1" bin Starting local Bazel server and connecting to it... Analyzing: target //:bin (1 packages loaded) FATAL: bazel crashed due to an internal error. Printing stack trace: java.lang.IllegalStateException: java.lang.RuntimeException: Unrecoverable error while evaluating node 'BuildConfigurationValue.Key[d4cb0a4b3f4f0893dcd83a9f0e5c72723c2bcc058ce1404b220d90cc32c4d106]' (requested by nodes ) at com.google.devtools.build.lib.skyframe.SkyframeExecutor.evaluateSkyKeys(SkyframeExecutor.java:2257) at com.google.devtools.build.lib.skyframe.SkyframeExecutor.getConfigurations(SkyframeExecutor.java:2100) at com.google.devtools.build.lib.analysis.config.ConfigurationResolver.getConfigurationsFromExecutor(ConfigurationResolver.java:584) at com.google.devtools.build.lib.analysis.AnalysisUtils.getTargetsWithConfigs(AnalysisUtils.java:193) at com.google.devtools.build.lib.analysis.BuildView.update(BuildView.java:245) at com.google.devtools.build.lib.buildtool.AnalysisPhaseRunner.runAnalysisPhase(AnalysisPhaseRunner.java:217) at com.google.devtools.build.lib.buildtool.AnalysisPhaseRunner.execute(AnalysisPhaseRunner.java:129) at com.google.devtools.build.lib.buildtool.BuildTool.buildTargets(BuildTool.java:198) at com.google.devtools.build.lib.buildtool.BuildTool.processRequest(BuildTool.java:443) at com.google.devtools.build.lib.buildtool.BuildTool.processRequest(BuildTool.java:411) at com.google.devtools.build.lib.runtime.commands.BuildCommand.exec(BuildCommand.java:103) at com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.execExclusively(BlazeCommandDispatcher.java:585) at com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.exec(BlazeCommandDispatcher.java:231) at com.google.devtools.build.lib.server.GrpcServerImpl.executeCommand(GrpcServerImpl.java:550) at com.google.devtools.build.lib.server.GrpcServerImpl.lambda$run$1(GrpcServerImpl.java:614) at io.grpc.Context$1.run(Context.java:579) 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: java.lang.RuntimeException: Unrecoverable error while evaluating node 'BuildConfigurationValue.Key[d4cb0a4b3f4f0893dcd83a9f0e5c72723c2bcc058ce1404b220d90cc32c4d106]' (requested by nodes ) at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:674) at com.google.devtools.build.lib.concurrent.AbstractQueueVisitor$WrappedRunnable.run(AbstractQueueVisitor.java:382) ... 3 more Caused by: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.Map$Entry (java.lang.String and java.util.Map$Entry are in module java.base of loader 'bootstrap') at com.google.devtools.build.lib.bazel.rules.BazelRuleClassProvider.lambda$static$1(BazelRuleClassProvider.java:220) at com.google.devtools.build.lib.skyframe.BuildConfigurationFunction.compute(BuildConfigurationFunction.java:90) at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:590) ... 4 more

sgowroji avatar Jul 19 '22 05:07 sgowroji

That's the same error I get too.

nmosier avatar Jul 19 '22 19:07 nmosier

This may be fixed at head. We're looking into backporting the fix to 5.x

aiuto avatar Sep 19 '22 14:09 aiuto

Closing based on the previous comment. If anyone verifies the issue in a newer Bazel version happy to re-open.

gregestren avatar Jan 17 '24 18:01 gregestren

@gregestren, I ask you to reopen the isssue, because bug is still actual. Reproduced at Linux with bazel 7.0.1.

How to reproduce:

BUILD.bazel file:

load(":env_transition.bzl", "rule_with_env_transition")

rule_with_env_transition(
    name = "bug_example",
)

env_transition.bzl file:

def _env_transition_impl(settings, _):
    return {"//command_line_option:action_env": settings["//command_line_option:action_env"]}

_env_transition = transition(
    implementation = _env_transition_impl,
    inputs = ["//command_line_option:action_env"],
    outputs = ["//command_line_option:action_env"],
)

def _rule_with_env_transition_impl(ctx):
    pass

rule_with_env_transition = rule(
    implementation = _rule_with_env_transition_impl,
    cfg = _env_transition,
)

Fil-Den avatar Jan 21 '24 18:01 Fil-Den

I think https://github.com/bazelbuild/bazel/commit/08bd3742c6374c4080af45d5a9d79e81bd59ea66 fixes this. That was submitted Nov 13, 2023 - after Bazel 7.0's baseline on Oct 17, 2023.

Let's ask to add https://github.com/bazelbuild/bazel/commit/08bd3742c6374c4080af45d5a9d79e81bd59ea66 to the next 7.x patch (or test with bazel@head).

gregestren avatar Feb 21 '24 00:02 gregestren

@bazel-io fork 7.2.0

gregestren avatar Feb 21 '24 00:02 gregestren

A fix for this issue has been included in Bazel 7.2.0 RC1. Please test out the release candidate and report any issues as soon as possible. If you're using Bazelisk, you can point to the latest RC by setting USE_BAZEL_VERSION=7.2.0rc1. Thanks!

iancha1992 avatar May 15 '24 22:05 iancha1992

A fix for this issue has been included in Bazel 7.2.0 RC1. Please test out the release candidate and report any issues as soon as possible.

That's work for me, thanks!

Fil-Den avatar May 27 '24 19:05 Fil-Den