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

Support rustfmt toolchains for bzlmod #2260

Closed
stephencpope opened this issue Nov 13, 2023 · 0 comments · Fixed by #2410
Closed

Support rustfmt toolchains for bzlmod #2260

stephencpope opened this issue Nov 13, 2023 · 0 comments · Fixed by #2410

Comments

@stephencpope
Copy link

When running under bazel 6.4.0 using --enable_blzmod, the rustfmt toolchain doesn't get wired up as it should. A simple no-op bazel module fails to support the @rules_rust//:rustfmt target like this:

$ bazel run --enable_bzlmod @rules_rust//:rustfmt
Starting local Bazel server and connecting to it...
DEBUG: @@rules_rust~override//:MODULE.bazel:6:6: WARNING: The rules_rust Bazel module is still highly experimental and subject to change at any time. Only use it to try out bzlmod for now.
ERROR: .../external/rules_rust~override/rust/toolchain/BUILD.bazel:40:26: While resolving toolchains for target @rules_rust~override//rust/toolchain:current_rustfmt_toolchain: No matching toolchains found for types @rules_rust~override//rust/rustfmt:toolchain_type.
To debug, rerun with --toolchain_resolution_debug='@rules_rust~override//rust/rustfmt:toolchain_type'
If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.
ERROR: Analysis of target '@rules_rust~override//:rustfmt' failed; build aborted: 
INFO: Elapsed time: 13.555s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (42 packages loaded, 168 targets configured)
    currently loading: @rules_rust~override//tools/runfiles
ERROR: Build failed. Not running target

when using this MODULE.bazel:

bazel_dep(name = "rules_rust", version = "0.30.0")
git_override(
    module_name = "rules_rust",
    remote = "https://github.com/bazelbuild/rules_rust",
    commit = "54bef51d8e3458ec60ea2b6a27e15b62502c673d",
)

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")

rust.toolchain()

use_repo(rust, "rust_toolchains")

register_toolchains(
    "@rust_toolchains//:all",
    dev_dependency = True,
)

The problem can also be seen in the output of

$ bazel query --enable_bzlmod @rust_toolchains//:\*
@rust_toolchains//:BUILD.bazel
@rust_toolchains//:rust_analyzer_1.73.0
@rust_toolchains//:rust_darwin_aarch64__aarch64-apple-darwin__nightly
@rust_toolchains//:rust_darwin_aarch64__aarch64-apple-darwin__stable
...

where there are no rustfmt toolchains.

Unfortunately at the moment I cannot sign a CLA and thus cannot submit a PR, but the following fixes the problem:

--- rust/repositories.bzl
+++ rust/repositories.bzl
@@ -250,6 +250,14 @@ def rust_register_toolchains(
             exec_triple = exec_triple,
         )
 
+        toolchain_names.append(rustfmt_repo_name)
+        toolchain_labels[rustfmt_repo_name] = "@{}_tools//:rustfmt_toolchain".format(
+            rustfmt_repo_name,
+        )
+        exec_compatible_with_by_toolchain[rustfmt_repo_name] = []
+        target_compatible_with_by_toolchain[rustfmt_repo_name] = []
+        toolchain_types[rustfmt_repo_name] = "@rules_rust//rust/rustfmt:toolchain_type"
+
         if register_toolchains:
             native.register_toolchains("@{}//:toolchain".format(
                 rustfmt_repo_name,

The problem is that although this isn't necessary when working in WORKSPACE mode when native.register_toolchains is invoked directly, without it the rustfmt toolchains are not added to the toolchain repository hub, and so are not picked up later in MODULE.bazel when register_toolchains is called. And as far as I can tell it is not harmful to have this added code when working in WORKSPACE mode.

I note that this problem not only affects the direct use of rustfmt, but also is a problem for rust_bindgen (which isn't yet supported for bzlmod but I am working on that!)

Disclaimer: I am no expert in toolchains; I am not sure if the target_compatible_with_by_toolchain setting is correct; I just copied it from the similar for for rust_analyzer farther above.

daivinhtran pushed a commit to daivinhtran/rules_rust that referenced this issue Jan 17, 2024
Drop import macro (bazelbuild#2411)

As discussed over at
bazelbuild#2383

Fix rustfmt toolchains when consuming rules_rust with bzlmod. (bazelbuild#2410)

Fixes bazelbuild#2260

Provide a better error message when trying to generate rust-project.json (bazelbuild#2196)

Currently when trying to generate a `rust-project.json`, if there aren't
actually any Rust targets defined, the script mysteriously fails.

This adds a better error message.

Update android example to use Starlark version of android_ndk_repository (bazelbuild#2417)

The native version of `android_ndk_repository` rule no longer work with
the newer ndk versions.

When I set `ANDROID_NDK_HOME` to
`/usr/local/vinhdaitran/Android/Sdk/ndk/26.1.10909125`, the rule expects
a different structure from the Android NDK path.
```
ERROR: /usr/local/vinhdaitran/github/rules_rust/examples/android/WORKSPACE.bazel:67:23: fetching android_ndk_repository rule //external:androidndk: java.io.IOException:
Expected directory at /usr/local/vinhdaitran/Android/Sdk/ndk/26.1.10909125/platforms but it is not a directory or it does not exist.
Unable to read the Android NDK at /usr/local/vinhdaitran/Android/Sdk/ndk/26.1.10909125, the path may be invalid. Is the path in android_ndk_repository() or ANDROID_NDK_HOME set correctly?
If the path is correct, the contents in the Android NDK directory may have been modified.
```

Using the Starlark version of the rule, as recommended in
https://bazel.build/reference/be/android#android_ndk_repository, fixes
the issue.

cc: @keith

Allow ~ in repository names (bazelbuild#2427)

Fixes bazelbuild#2426

Prepare rust rules for Starlark CcToolchainInfo. (bazelbuild#2424)

bazelbuild#2425
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant