I have a problem where even simple rust core functionality like Option, None, Some doesn't work correctly with basically the auto discovery setup from https://bazelbuild.github.io/rules_rust/rust_analyzer.html. rust-analyzer doesn't seem to recognize e.g. None without an explicit import.
Reproduction:
bazel_dep(name = "rules_rust", version = "0.69.0")
bazel_dep(name = "rules_rs", version = "0.0.26")
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
edition = "2024",
versions = ["1.93.1"],
)
- Create a
hello_world/ folder containing:
main.rs:
// use core::option::Option::None;
#[allow(dead_code)]
fn opt_match(a: Option<u8>) -> u8 {
match a {
Some(a) => a,
None => 0,
}
}
fn main() {
println!("Hello, world!");
}
BUILD.bazel:
load("@rules_rust//rust:defs.bzl", "rust_binary")
rust_binary(
name = "hello_world",
srcs = ["main.rs"],
)
- Setup your editor similar to the suggested setup. E.g. something like:
"rust-analyzer": {
"workspace": {
"discoverConfig": {
"command": ["discover_bazel_rust_project.sh", "{arg}"],
"progressLabel": "rust_analyzer",
"filesToWatch": ["BUILD", "BUILD.bazel", "MODULE.bazel"]
}
}
}
where discover_bazel_rust_project.sh:
#!/usr/bin/env bash
RA_CACHE_OUTPUT_BASE=".rust_analyzer_cache"
bazel \
--output_base="${RA_CACHE_OUTPUT_BASE}" \
run \
@rules_rust//tools/rust_analyzer:discover_bazel_rust_project -- \
--bazel_startup_option=--output_base="${RA_CACHE_OUTPUT_BASE}" \
--bazel_arg=--output_groups=-clippy_checks \
--bazel_arg=--watchfs \
${1:+"$1"} 2>/dev/null
- Open the editor and notice that you get diagnostics on the
None above and it seems that rust-analyzer cannot recognize it.
Problem:
I have looked into this quite a bit now, but I don't know rust-analyzer that well. But looking at my log with RA_LOG=rust_analyzer=trace, it seems that everything with the sysroot actually works fine and it finds e.g., core, std, alloc, ...
But in the log it seems that rust-analyzer cannot understand dependencies between these core crates, which makes sense since there are no BUILD files describing the dependencies, and although there are Cargo.toml files, cargo isn't available so when rust-analyzer tries cargo metadata it fails.
To support this claim, note that uncommenting // use core::option::Option::None; in the above rust code works. So with an explicit core import, rust-analyzer can figure it out.
But I honestly might still be misunderstanding the actual problem here. I can just see that it doesn't work.
I have a branch where I simply add cargo and the .rlib files that you usually have in the rustup sysroots to the rust-analyzer sysroot, and that seems to fix it. But I am not sure if this is the best fix, so I thought I would create this pull request for any further discussion, and then I will just link the PR.
(Note: I added the .rlib files first, and that didn't fix it. Then I added cargo and that fixed it. Just adding cargo is probably enough on its own.)
I have a problem where even simple rust core functionality like
Option,None,Somedoesn't work correctly with basically the auto discovery setup from https://bazelbuild.github.io/rules_rust/rust_analyzer.html. rust-analyzer doesn't seem to recognize e.g.Nonewithout an explicit import.Reproduction:
MODULE.bazel(mainly because of some editor setup when I tried this), e.g.:hello_world/folder containing:main.rs:BUILD.bazel:where
discover_bazel_rust_project.sh:Noneabove and it seems that rust-analyzer cannot recognize it.Problem:
I have looked into this quite a bit now, but I don't know rust-analyzer that well. But looking at my log with
RA_LOG=rust_analyzer=trace, it seems that everything with the sysroot actually works fine and it finds e.g.,core,std,alloc, ...But in the log it seems that rust-analyzer cannot understand dependencies between these core crates, which makes sense since there are no
BUILDfiles describing the dependencies, and although there areCargo.tomlfiles,cargoisn't available so when rust-analyzer triescargo metadatait fails.To support this claim, note that uncommenting
// use core::option::Option::None;in the above rust code works. So with an explicitcoreimport, rust-analyzer can figure it out.But I honestly might still be misunderstanding the actual problem here. I can just see that it doesn't work.
I have a branch where I simply add
cargoand the.rlibfiles that you usually have in the rustup sysroots to the rust-analyzer sysroot, and that seems to fix it. But I am not sure if this is the best fix, so I thought I would create this pull request for any further discussion, and then I will just link the PR.(Note: I added the
.rlibfiles first, and that didn't fix it. Then I addedcargoand that fixed it. Just addingcargois probably enough on its own.)