blob: 1fc7e3648e34ace5d4a7925744763e545717780d [file] [log] [blame]
Brian Silvermancc09f182022-03-09 15:40:20 -08001#[[
2## Overview
3
4For [non-Cargo projects](https://rust-analyzer.github.io/manual.html#non-cargo-based-projects),
5[rust-analyzer](https://rust-analyzer.github.io/) depends on a `rust-project.json` file at the
6root of the project that describes its structure. The `rust_analyzer` rule facilitates generating
7such a file.
8
9### Setup
10
Brian Silverman5f6f2762022-08-13 19:30:05 -070011First, ensure `rules_rust` is setup in your workspace. By default, `rust_register_toolchains` will
12ensure a [rust_analyzer_toolchain](#rust_analyzer_toolchain) is registered within the WORKSPACE.
13
14Next, load the dependencies for the `rust-project.json` generator tool:
Brian Silvermancc09f182022-03-09 15:40:20 -080015
16```python
Brian Silverman5f6f2762022-08-13 19:30:05 -070017load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")
Brian Silvermancc09f182022-03-09 15:40:20 -080018
Brian Silverman5f6f2762022-08-13 19:30:05 -070019rust_analyzer_dependencies()
Brian Silvermancc09f182022-03-09 15:40:20 -080020```
21
Brian Silvermancc09f182022-03-09 15:40:20 -080022Finally, run `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project`
23whenever dependencies change to regenerate the `rust-project.json` file. It
24should be added to `.gitignore` because it is effectively a build artifact.
25Once the `rust-project.json` has been generated in the project root,
26rust-analyzer can pick it up upon restart.
27
Brian Silverman5f6f2762022-08-13 19:30:05 -070028For users who do not use `rust_register_toolchains` to register toolchains, the following can be added
29to their WORKSPACE to register a `rust_analyzer_toolchain`. Please make sure the Rust version used in
30this toolchain matches the version used by the currently registered toolchain or the sources/documentation
Adam Snaider1c095c92023-07-08 02:09:58 -040031will not match what's being compiled with and can lead to confusing results.
Brian Silverman5f6f2762022-08-13 19:30:05 -070032
33```python
34load("@rules_rust//rust:repositories.bzl", "rust_analyzer_toolchain_repository")
35
36register_toolchains(rust_analyzer_toolchain_repository(
37 name = "rust_analyzer_toolchain",
38 # This should match the currently registered toolchain.
39 version = "1.62.0",
40))
41```
42
Brian Silvermancc09f182022-03-09 15:40:20 -080043#### VSCode
44
45To set this up using [VSCode](https://code.visualstudio.com/), users should first install the
46[rust_analyzer plugin](https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer).
47With that in place, the following task can be added to the `.vscode/tasks.json` file of the workspace
48to ensure a `rust-project.json` file is created and up to date when the editor is opened.
49
50```json
51{
52 "version": "2.0.0",
53 "tasks": [
54 {
55 "label": "Generate rust-project.json",
56 "command": "bazel",
57 "args": ["run", "@rules_rust//tools/rust_analyzer:gen_rust_project"],
58 "options": {
59 "cwd": "${workspaceFolder}"
60 },
61 "group": "build",
62 "problemMatcher": [],
63 "presentation": {
64 "reveal": "never",
65 "panel": "dedicated",
66 },
67 "runOptions": {
68 "runOn": "folderOpen"
69 }
70 },
71 ]
72}
73```
74]]#