bazelbuild / bazelbuild/rules_rust

rust-analyzer discoverConfig setup doesn't resolve core libraries correctly

Open
#3,985 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Starlark
Stars
843
Forks
651
Avg merge
2d 18h
Merged PRs (30d)
15

Description

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:**
- Clone https://github.com/bazel-starters/rust#
- Update the rust version to something new in `MODULE.bazel` (mainly because of some editor setup when I tried this), e.g.:
```bazel
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`:
```rust
// use core::option::Option::None;

#[allow(dead_code)]
fn opt_match(a: Option) -> u8 {
match a {
Some(a) => a,
None => 0,
}
}

fn main() {
println!("Hello, world!");
}
```

`BUILD.bazel`:
```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:
```json
"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`:
```bash
#!/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.)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.