1#[[ 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 11First, 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: 15 16```python 17load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies") 18 19rust_analyzer_dependencies() 20``` 21 22Finally, 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 28For 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 31will not match what's being compiled with and can lead to confusing results. 32 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.63.0", 40)) 41``` 42 43#### 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 75#### Alternative vscode option (prototype) 76 77Add the following to your bazelrc: 78``` 79build --@rules_rust//:output_diagnostics=true --output_groups=+rust_lib_rustc_output,+rust_metadata_rustc_output 80``` 81 82Then you can use a prototype [rust-analyzer plugin](https://marketplace.visualstudio.com/items?itemName=MattStark.bazel-rust-analyzer) that automatically collects the outputs whenever you recompile. 83 84]]# 85