-
Notifications
You must be signed in to change notification settings - Fork 434
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
Error: local variable 'cc_toolchain' is referenced before assignment. #635
Comments
Can you provide an example that demonstrates this issue? |
The issue occurs when using given
given the target load("@rules_rust//rust:rust.bzl", "rust_library")
rust_library(
name = "hello-world",
srcs = [
"src/main.rs",
],
edition = "2018",
visibility = ["//visibility:public"],
) $ bazel build --incompatible_enable_cc_toolchain_resolution hello-world
INFO: SHA256 (https://github.com/bazelbuild/rules_rust/archive/76c44206c4d37491767446c6ccdf1a98274a0887.tar.gz) = 0044473997e40c5467aa410912ae8771aa328e2d44d71df073b87f147ab5cfcf
DEBUG: Rule 'rules_rust' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = "0044473997e40c5467aa410912ae8771aa328e2d44d71df073b87f147ab5cfcf"
DEBUG: Repository rules_rust instantiated at:
/tmp/repro/WORKSPACE:3:13: in <toplevel>
Repository rule http_archive defined at:
/home/iffy/.cache/bazel/_bazel_iffy/a81b5ac6b5d7900969957b5de3807d8b/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
ERROR: /home/iffy/.cache/bazel/_bazel_iffy/a81b5ac6b5d7900969957b5de3807d8b/external/rust_linux_x86_64/BUILD.bazel:82:15: in rust_toolchain rule @rust_linux_x86_64//:toolchain_for_x86_64-unknown-linux-gnu_impl:
Traceback (most recent call last):
File "/home/iffy/.cache/bazel/_bazel_iffy/a81b5ac6b5d7900969957b5de3807d8b/external/rules_rust/rust/toolchain.bzl", line 154, column 72, in _rust_toolchain_impl
libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, ctx.attr.rust_lib, ctx.attr.allocator_library),
File "/home/iffy/.cache/bazel/_bazel_iffy/a81b5ac6b5d7900969957b5de3807d8b/external/rules_rust/rust/toolchain.bzl", line 43, column 60, in _make_libstd_and_allocator_ccinfo
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
File "/home/iffy/.cache/bazel/_bazel_iffy/a81b5ac6b5d7900969957b5de3807d8b/external/rules_rust/rust/private/utils.bzl", line 43, column 24, in find_cc_toolchain
cc_toolchain = cc_toolchain,
Error: local variable 'cc_toolchain' is referenced before assignment.
ERROR: While resolving toolchains for target //hello-world:hello-world: Analysis of target '@rust_linux_x86_64//:toolchain_for_x86_64-unknown-linux-gnu_impl' failed
ERROR: Analysis of target '//hello-world:hello-world' failed; build aborted: Analysis of target '@rust_linux_x86_64//:toolchain_for_x86_64-unknown-linux-gnu_impl' failed
INFO: Elapsed time: 4.123s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (28 packages loaded, 27940 targets configured) |
I reproduce, this looks like it was introduced in in #624, but looks really weird - the actual error message given: I'll keep digging for a bit... |
The problematic part occurs here while trying to resolve the toolchain https://github.com/bazelbuild/bazel/blob/master/tools/cpp/toolchain_utils.bzl#L41. Even though the check succeeds, This works around the issue by skipping that code path entirely and always falling back diff --git rust/private/utils.bzl rust/private/utils.bzl
index 06ed842..be8904e 100644
--- rust/private/utils.bzl
+++ rust/private/utils.bzl
@@ -14,7 +14,11 @@
"""Utility functions not specific to the rust toolchain."""
-load("@bazel_tools//tools/cpp:toolchain_utils.bzl", find_rules_cc_toolchain = "find_cpp_toolchain")
+def find_rules_cc_toolchain(ctx):
+ if hasattr(ctx.attr, "_cc_toolchain"):
+ return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]
+ fail("In order to use find_cpp_toolchain, you must define the '_cc_toolchain' attribute on your rule or aspect.")
+
def find_toolchain(ctx):
"""Finds the first rust toolchain that is configured. |
I probably don't have time to dig much deeper into this at the moment, but definitely feels like a bug internal to bazel to me |
One thing that we missed is that
But even with that the build still fails. I am starting to worry that toolchains cannot depend on other toolchains. @katre may help us there? |
Let me take a look and see if I can reproduce this. |
Using the setup from @iffyio (and a hello world for rust that I grabbed from somewhere), I can see the same problem. Even after adding the toolchain type (and the Debugging into Bazel I can see that the toolchain context exists, and has no toolchains, which is very odd, so I'm going to debug further. |
Ah ha, found the problem. In I'll work up a patch for Bazel and send it shortly. I'll also upload a PR to add the proper toolchain declaration to Filed bazelbuild/bazel#13243 to track this on Bazel's side. |
Part of the work on #635, but not sufficient to fix it.
I've fixed bazelbuild/bazel#13243 with Bazel at head. My repro now works, please test yourselves and let me know if there are still problems. |
This reverts commit 2109a0a. Works around bazelbuild#635.
…alloc (bazelbuild#624)" This reverts commit 802bcf9. Works around bazelbuild#635.
I hit this trying to make rules_rust use the correct cc_toolchain, and I would say the error I assigned this variable to 0, and got an error I would expect for an incorrect type. Whatever |
@sayrer That's a valid point, can you file a separate issue in bazelbuild/bazel and I'll try and route it to the correct people? |
Hey, wanted to note that this was fixed, but with e7b1e5a it regressed and now fails with the following error.
|
Oh no! What are you redoing to repro this? And what Bazel version are you using? |
4.1.0, but I also tried with 4.2.1. This is what the rules look like, but I can try and make a fuller repro if you need. I should clarify that it seems like it only happens with
|
@XAMPPRocky A fuller example would be helpful. But can you see if #1021 would fix the issue? |
Hi! I get this error from
rust_library
targets after updating to commit76c44206c4d37491767446c6ccdf1a98274a0887
it seems like somehow
@bazel_tools//tools/cpp:toolchain_type
was supposed to be set but no longer isnt?The text was updated successfully, but these errors were encountered: