Skip to content

[Doc] Improve rust analyzer section, distinguishing bzlmod and WORKSPACE #3365

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions docs/rust_analyzer.vm
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,46 @@ such a file.

### Setup

#### Bzlmod

First, ensure `rules_rust` is setup in your workspace. By default, `rust_register_toolchains` will
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this accurate for bzlmod? I thought all the toolchains were registered by default for @rules_rust//tools/rust_analyzer:gen_rust_project (excluding the rust toolchain itself)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure i understood correctly (i'm not so experienced with bazel), but commenting the line register_toolchains("@rust_toolchains//:all") from MODULE.bazel made the project still build

ensure a [rust_analyzer_toolchain](#rust_analyzer_toolchain) is registered within the WORKSPACE.

Next, load the dependencies for the `rust-project.json` generator tool:

```python
# BUILD.bazel

load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")
```

rust_analyzer_dependencies()
Bazel will create the target `@rules_rust//tools/rust_analyzer:gen_rust_project`, which you can build
with

```
bazel run @rules_rust//tools/rust_analyzer:gen_rust_project
```

Finally, run `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project`
whenever dependencies change to regenerate the `rust-project.json` file. It
should be added to `.gitignore` because it is effectively a build artifact.
Once the `rust-project.json` has been generated in the project root,
rust-analyzer can pick it up upon restart.

#### WORKSPACE

As with Bzlmod, ensure `rules_rust` is setup in your workspace.

Moreover, when loading the dependencies for the tool, you should call the function `rust_analyzer_dependencies()`:

```python
load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")

rust_analyzer_dependencies()
```

Again, you can now run `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project`
whenever dependencies change to regenerate the `rust-project.json` file.

For users who do not use `rust_register_toolchains` to register toolchains, the following can be added
to their WORKSPACE to register a `rust_analyzer_toolchain`. Please make sure the Rust version used in
this toolchain matches the version used by the currently registered toolchain or the sources/documentation
Expand Down