bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.06k stars 4.04k forks source link

Error in fail: Unable to find a CC toolchain using toolchain resolution. #20970

Open sgowroji opened 8 months ago

sgowroji commented 8 months ago

CI: https://buildkite.com/bazel/bazel-at-head-plus-downstream/builds/3608#018d2ef4-1d2c-446b-a314-c133272f58d9

Platform: Ubuntu, MacOS

Logs:

Traceback (most recent call last):
    File "/virtual_builtins_bzl/common/cc/cc_toolchain_alias.bzl", line 26, column 48, in _impl
    File "/virtual_builtins_bzl/common/cc/cc_helper.bzl", line 217, column 13, in _find_cpp_toolchain
Error in fail: Unable to find a CC toolchain using toolchain resolution. Target: @@bazel_tools//tools/cpp:current_cc_toolchain, Platform: @@//:arm64-v8a, Exec platform: @@local_config_platform//:host
(02:20:31) ERROR: /var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/051ad6a69d7a84f28cf16b88c532f996/external/bazel_tools/tools/cpp/BUILD:58:19: Analysis of target '@@bazel_tools//tools/cpp:current_cc_toolchain' failed
(02:20:31) ERROR: Analysis of target '//app/src/main:app' failed; build aborted: Analysis failed

Culprit: https://github.com/bazelbuild/bazel/commit/03490b9f72dc479c979afff6b1731a72787cba8b, https://github.com/bazelbuild/bazel/commit/8ba66188e378fe3df5db9549b3b9661d62cf93bb

Steps:

git clone -v https://github.com/bazelbuild/examples.git 
git reset d18ce42623f136616e9512d713a47c33f9ad6a58 --hard
export USE_BAZEL_VERSION=addf41bce1f26e8bc4bd0b09cb2fbffbb0446f25
cd examples/android/jetpack-compose
bazel build  --android_platforms=//:arm64-v8a --remote_download_outputs=all --enable_bzlmod  //app/src/main:app 

CC Greenteam @comius

AnDevi commented 7 months ago

Hello, I'm using bazel 7.0.2 right now and I'm facing exactly the same problem. Is there possibility that that issue came back ?

comius commented 7 months ago

Hey @ted-xie, this looks like Android NDK toolchains problem happening https://github.com/bazelbuild/examples/tree/main/android/jetpack-compose. Could you investigate?

comius commented 6 months ago

@ted-xie @ahumesky friendly ping

comius commented 5 months ago

@ted-xie @ahumesky friendly ping

comius commented 5 months ago

cc @meisterT

ted-xie commented 4 months ago

I was able to reproduce this (after a small change in the repro instructions that I've edited the original issue for). I think I've seen this before; we should just need some extra flags in bazelrc IIRC.

ted-xie commented 4 months ago

I think there may be multiple problems with this example. I noticed there was no NDK repository specified anywhere, so I added it:

diff --git a/android/jetpack-compose/WORKSPACE b/android/jetpack-compose/WORKSPACE
index b0325a8..db38a08 100644
--- a/android/jetpack-compose/WORKSPACE
+++ b/android/jetpack-compose/WORKSPACE
@@ -41,3 +41,14 @@ load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")

 kt_register_toolchains()

+http_archive(
+    name = "rules_android_ndk",
+    sha256 = "b1a5ddd784e6ed915c2035c0db536a278b5f50c64412128c06877115991391ef",
+    strip_prefix = "rules_android_ndk-877c68ef34c9f3353028bf490d269230c1990483",
+    url = "https://github.com/bazelbuild/rules_android_ndk/archive/877c68ef34c9f3353028bf490d269230c1990483.zip",
+)
+
+load("@rules_android_ndk//:rules.bzl", "android_ndk_repository")
+
+android_ndk_repository(name = "androidndk")
+

Then when I try to build the example app, there's a different error about a missing Java toolchain:

$ bazelisk build  --android_platforms=//:arm64-v8a --remote_download_outputs=all --enable_bzlmod  //app/src/main:app 
ERROR: /usr/local/google/home/tedx/work/github/bazel_issue_20970/examples/android/jetpack-compose/app/src/main/BUILD.bazel:4:19: While resolving toolchains for target //app/src/main:lib_kt (aadbf16): No matching toolchains found for types @@bazel_tools//tools/jdk:runtime_toolchain_type.

I think overall it may be better to deprecate this jetpack compose example entirely, since rules_kotlin has its own version here that is more actively maintained and closer to the development pulse. @Bencodes

ted-xie commented 4 months ago

Updating to a more recent rules_kotlin version seems to make things a little better, but not entirely; at least now there are no more toolchain-related issues.

My patch:

diff --git a/android/jetpack-compose/WORKSPACE b/android/jetpack-compose/WORKSPACE
index b0325a8..2ab68f2 100644
--- a/android/jetpack-compose/WORKSPACE
+++ b/android/jetpack-compose/WORKSPACE
@@ -22,10 +22,15 @@ android_sdk_repository(name = "androidsdk")

 ## Kotlin

+#http_archive(
+#    name = "io_bazel_rules_kotlin",
+#    sha256 = "f033fa36f51073eae224f18428d9493966e67c27387728b6be2ebbdae43f140e",
+#    url = "https://github.com/bazelbuild/rules_kotlin/releases/download/v1.7.0-RC-3/rules_kotlin_release.tgz",
+#)
 http_archive(
     name = "io_bazel_rules_kotlin",
-    sha256 = "f033fa36f51073eae224f18428d9493966e67c27387728b6be2ebbdae43f140e",
-    url = "https://github.com/bazelbuild/rules_kotlin/releases/download/v1.7.0-RC-3/rules_kotlin_release.tgz",
+    sha256 = "34e8c0351764b71d78f76c8746e98063979ce08dcf1a91666f3f3bc2949a533d",
+    url = "https://github.com/bazelbuild/rules_kotlin/releases/download/v1.9.5/rules_kotlin-v1.9.5.tar.gz",
 )

 load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "kotlinc_version")
@@ -41,3 +46,15 @@ load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")

 kt_register_toolchains()

+## NDK
+http_archive(
+    name = "rules_android_ndk",
+    sha256 = "b1a5ddd784e6ed915c2035c0db536a278b5f50c64412128c06877115991391ef",
+    strip_prefix = "rules_android_ndk-877c68ef34c9f3353028bf490d269230c1990483",
+    url = "https://github.com/bazelbuild/rules_android_ndk/archive/877c68ef34c9f3353028bf490d269230c1990483.zip",
+)
+
+load("@rules_android_ndk//:rules.bzl", "android_ndk_repository")
+
+android_ndk_repository(name = "androidndk")

The build:

$ bazelisk build --android_platforms=//:arm64-v8a --remote_download_outputs=all --enable_bzlmod  //app/src/main:app  
INFO: Analyzed target //app/src/main:app (12 packages loaded, 6078 targets configured).
INFO: From KotlinCompile //app/src/main:lib_kt { kt: 1, java: 0, srcjars: 0 } for k8:
warning: language version 1.9 is experimental, there are no backwards compatibility guarantees for new language and library features
warning: language version 1.9 is experimental, there are no backwards compatibility guarantees for new language and library features
ERROR: /usr/local/google/home/tedx/work/github/bazel_issue_20970/examples/android/jetpack-compose/app/src/main/BUILD.bazel:4:19: output 'app/src/main/lib_kt-kt.jdeps' was not created
ERROR: /usr/local/google/home/tedx/work/github/bazel_issue_20970/examples/android/jetpack-compose/app/src/main/BUILD.bazel:4:19: KotlinCompile //app/src/main:lib_kt { kt: 1, java: 0, srcjars: 0 } for k8 failed: not all outputs were created or valid

I think this needs some input from the rules_kotlin folks on how to proceed.