Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 1 | #[[ |
| 2 | ## Overview |
| 3 | |
| 4 | For [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 |
| 6 | root of the project that describes its structure. The `rust_analyzer` rule facilitates generating |
| 7 | such a file. |
| 8 | |
| 9 | ### Setup |
| 10 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 11 | First, ensure `rules_rust` is setup in your workspace. By default, `rust_register_toolchains` will |
| 12 | ensure a [rust_analyzer_toolchain](#rust_analyzer_toolchain) is registered within the WORKSPACE. |
| 13 | |
| 14 | Next, load the dependencies for the `rust-project.json` generator tool: |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 15 | |
| 16 | ```python |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 17 | load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies") |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 18 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 19 | rust_analyzer_dependencies() |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 20 | ``` |
| 21 | |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 22 | Finally, run `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project` |
| 23 | whenever dependencies change to regenerate the `rust-project.json` file. It |
| 24 | should be added to `.gitignore` because it is effectively a build artifact. |
| 25 | Once the `rust-project.json` has been generated in the project root, |
| 26 | rust-analyzer can pick it up upon restart. |
| 27 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 28 | For users who do not use `rust_register_toolchains` to register toolchains, the following can be added |
| 29 | to their WORKSPACE to register a `rust_analyzer_toolchain`. Please make sure the Rust version used in |
| 30 | this toolchain matches the version used by the currently registered toolchain or the sources/documentation |
Adam Snaider | 1c095c9 | 2023-07-08 02:09:58 -0400 | [diff] [blame^] | 31 | will not match what's being compiled with and can lead to confusing results. |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame] | 32 | |
| 33 | ```python |
| 34 | load("@rules_rust//rust:repositories.bzl", "rust_analyzer_toolchain_repository") |
| 35 | |
| 36 | register_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 Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 43 | #### VSCode |
| 44 | |
| 45 | To 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). |
| 47 | With that in place, the following task can be added to the `.vscode/tasks.json` file of the workspace |
| 48 | to 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 | ]]# |