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

[Doc] How to use Rust analyzer support? #2997

Open
bertulli opened this issue Nov 13, 2024 · 0 comments
Open

[Doc] How to use Rust analyzer support? #2997

bertulli opened this issue Nov 13, 2024 · 0 comments

Comments

@bertulli
Copy link

bertulli commented Nov 13, 2024

Hi, first of all, thank you for your work.

I am trying to set up Bazel to build a Rust project. I followed the instructions in the documentation, and I can successfully build an executable. However, I have trouble in using the Rust analyzer extension. Specifically, I get

~/MWE$ bazel build //rust:executable-rs
Starting local Bazel server and connecting to it...
WARNING: --enable_bzlmod is set, but no MODULE.bazel file was found at the workspace root. Bazel will create an empty MODULE.bazel file. Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. For more details, please refer to https://github.com/bazelbuild/bazel/issues/18958.
ERROR: Traceback (most recent call last):
        File "/home/abertulli/MWE/rust/BUILD.bazel", line 9, column 27, in <toplevel>
                rust_analyzer_dependencies()
        File "/home/abertulli/.cache/bazel/_bazel_abertulli/b2e37475533f474e11fd385fceaab463/external/rules_rust/tools/rust_analyzer/deps.bzl", line 9, column 30, in rust_analyzer_dependencies
                return crate_repositories()
        File "/home/abertulli/.cache/bazel/_bazel_abertulli/b2e37475533f474e11fd385fceaab463/external/rules_rust/tools/rust_analyzer/3rdparty/crates/defs.bzl", line 413, column 10, in crate_repositories
                maybe(
        File "/home/abertulli/.cache/bazel/_bazel_abertulli/b2e37475533f474e11fd385fceaab463/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 268, column 18, in maybe
                repo_rule(name = name, **kwargs)
Error in repository_rule: 'repository rule http_archive' can only be called during workspace loading
WARNING: Target pattern parsing failed.
ERROR: Skipping '//rust:executable-rs': Error evaluating '//rust:executable-rs': error loading package 'rust': Package 'rust' contains errors
ERROR: Error evaluating '//rust:executable-rs': error loading package 'rust': Package 'rust' contains errors
INFO: Elapsed time: 7.895s
INFO: 0 processes.
ERROR: Build did NOT complete successfully

If I comment the two lines using the Rust analyzer extension I can compile successfully. Also notice how it created an (empty) MODULE.bazel and a MODULE.bazel.lock file. However, I also tried the new approach using MODULE.bazel (see below), but I got the same error. I don't know if I misread the docs, or if they are not mentioning something. What am I doing worng? Thanks!


MWE

  • my workspace:
~/MWE$ tree
.
├── WORKSPACE.bazel
└── rust
    ├── BUILD.bazel
    └── main.rs

1 directory, 3 files
  • WORKSPACE.bazel:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# To find additional information on this release or newer ones visit:
# https://github.com/bazelbuild/rules_rust/releases
http_archive(
    name = "rules_rust",
    integrity = "sha256-r09Wyq5QqZpov845sUG1Cd1oVIyCBLmKt6HK/JTVuwI=",
    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.54.1/rules_rust-v0.54.1.tar.gz"],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains(
    edition = "2021",
)

load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")
  • rust/BUILD.bazel:
load("@rules_rust//rust:defs.bzl", "rust_binary")

rust_binary(
    name = "executable-rs",
    srcs = ["main.rs"]
)

# try commenting these two lines
load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")
rust_analyzer_dependencies()
  • rust/main.rs:
fn main() {
    let a = vec![test(), test()];
    dbg!(a);
}

fn test() -> i32 { 0 }
  • Extra: MODULE.bazel that I tried:
bazel_dep(name = "rules_rust", version = "0.54.1")

# Rust toolchain
RUST_EDITION = "2021"

RUST_VERSION = "1.79.0"

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
    edition = RUST_EDITION,
    versions = [RUST_VERSION],
)
use_repo(rust, "rust_toolchains")

register_toolchains("@rust_toolchains//:all")

that gave me the same

~/MWE$ bazel build //rust:executable-rs
ERROR: Traceback (most recent call last):
        File "/home/abertulli/MWE/rust/BUILD.bazel", line 9, column 27, in <toplevel>
                rust_analyzer_dependencies()
        File "/home/abertulli/.cache/bazel/_bazel_abertulli/b2e37475533f474e11fd385fceaab463/external/rules_rust~/tools/rust_analyzer/deps.bzl", line 9, column 30, in rust_analyzer_dependencies
                return crate_repositories()
        File "/home/abertulli/.cache/bazel/_bazel_abertulli/b2e37475533f474e11fd385fceaab463/external/rules_rust~/tools/rust_analyzer/3rdparty/crates/defs.bzl", line 413, column 10, in crate_repositories
                maybe(
        File "/home/abertulli/.cache/bazel/_bazel_abertulli/b2e37475533f474e11fd385fceaab463/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 268, column 18, in maybe
                repo_rule(name = name, **kwargs)
Error in repository_rule: 'repository rule http_archive' can only be called during workspace loading
WARNING: Target pattern parsing failed.
ERROR: Skipping '//rust:executable-rs': Error evaluating '//rust:executable-rs': error loading package 'rust': Package 'rust' contains errors
ERROR: Error evaluating '//rust:executable-rs': error loading package 'rust': Package 'rust' contains errors
INFO: Elapsed time: 3.394s
INFO: 0 processes.
ERROR: Build did NOT complete successfully
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

No branches or pull requests

1 participant