1*d4726bddSHONG Yifan#[[ 2*d4726bddSHONG Yifan## Overview 3*d4726bddSHONG Yifan 4*d4726bddSHONG YifanFor [non-Cargo projects](https://rust-analyzer.github.io/manual.html#non-cargo-based-projects), 5*d4726bddSHONG Yifan[rust-analyzer](https://rust-analyzer.github.io/) depends on a `rust-project.json` file at the 6*d4726bddSHONG Yifanroot of the project that describes its structure. The `rust_analyzer` rule facilitates generating 7*d4726bddSHONG Yifansuch a file. 8*d4726bddSHONG Yifan 9*d4726bddSHONG Yifan### Setup 10*d4726bddSHONG Yifan 11*d4726bddSHONG YifanFirst, ensure `rules_rust` is setup in your workspace. By default, `rust_register_toolchains` will 12*d4726bddSHONG Yifanensure a [rust_analyzer_toolchain](#rust_analyzer_toolchain) is registered within the WORKSPACE. 13*d4726bddSHONG Yifan 14*d4726bddSHONG YifanNext, load the dependencies for the `rust-project.json` generator tool: 15*d4726bddSHONG Yifan 16*d4726bddSHONG Yifan```python 17*d4726bddSHONG Yifanload("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies") 18*d4726bddSHONG Yifan 19*d4726bddSHONG Yifanrust_analyzer_dependencies() 20*d4726bddSHONG Yifan``` 21*d4726bddSHONG Yifan 22*d4726bddSHONG YifanFinally, run `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project` 23*d4726bddSHONG Yifanwhenever dependencies change to regenerate the `rust-project.json` file. It 24*d4726bddSHONG Yifanshould be added to `.gitignore` because it is effectively a build artifact. 25*d4726bddSHONG YifanOnce the `rust-project.json` has been generated in the project root, 26*d4726bddSHONG Yifanrust-analyzer can pick it up upon restart. 27*d4726bddSHONG Yifan 28*d4726bddSHONG YifanFor users who do not use `rust_register_toolchains` to register toolchains, the following can be added 29*d4726bddSHONG Yifanto their WORKSPACE to register a `rust_analyzer_toolchain`. Please make sure the Rust version used in 30*d4726bddSHONG Yifanthis toolchain matches the version used by the currently registered toolchain or the sources/documentation 31*d4726bddSHONG Yifanwill not match what's being compiled with and can lead to confusing results. 32*d4726bddSHONG Yifan 33*d4726bddSHONG Yifan```python 34*d4726bddSHONG Yifanload("@rules_rust//rust:repositories.bzl", "rust_analyzer_toolchain_repository") 35*d4726bddSHONG Yifan 36*d4726bddSHONG Yifanregister_toolchains(rust_analyzer_toolchain_repository( 37*d4726bddSHONG Yifan name = "rust_analyzer_toolchain", 38*d4726bddSHONG Yifan # This should match the currently registered toolchain. 39*d4726bddSHONG Yifan version = "1.63.0", 40*d4726bddSHONG Yifan)) 41*d4726bddSHONG Yifan``` 42*d4726bddSHONG Yifan 43*d4726bddSHONG Yifan#### VSCode 44*d4726bddSHONG Yifan 45*d4726bddSHONG YifanTo set this up using [VSCode](https://code.visualstudio.com/), users should first install the 46*d4726bddSHONG Yifan[rust_analyzer plugin](https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer). 47*d4726bddSHONG YifanWith that in place, the following task can be added to the `.vscode/tasks.json` file of the workspace 48*d4726bddSHONG Yifanto ensure a `rust-project.json` file is created and up to date when the editor is opened. 49*d4726bddSHONG Yifan 50*d4726bddSHONG Yifan```json 51*d4726bddSHONG Yifan{ 52*d4726bddSHONG Yifan "version": "2.0.0", 53*d4726bddSHONG Yifan "tasks": [ 54*d4726bddSHONG Yifan { 55*d4726bddSHONG Yifan "label": "Generate rust-project.json", 56*d4726bddSHONG Yifan "command": "bazel", 57*d4726bddSHONG Yifan "args": ["run", "@rules_rust//tools/rust_analyzer:gen_rust_project"], 58*d4726bddSHONG Yifan "options": { 59*d4726bddSHONG Yifan "cwd": "${workspaceFolder}" 60*d4726bddSHONG Yifan }, 61*d4726bddSHONG Yifan "group": "build", 62*d4726bddSHONG Yifan "problemMatcher": [], 63*d4726bddSHONG Yifan "presentation": { 64*d4726bddSHONG Yifan "reveal": "never", 65*d4726bddSHONG Yifan "panel": "dedicated", 66*d4726bddSHONG Yifan }, 67*d4726bddSHONG Yifan "runOptions": { 68*d4726bddSHONG Yifan "runOn": "folderOpen" 69*d4726bddSHONG Yifan } 70*d4726bddSHONG Yifan }, 71*d4726bddSHONG Yifan ] 72*d4726bddSHONG Yifan} 73*d4726bddSHONG Yifan``` 74*d4726bddSHONG Yifan 75*d4726bddSHONG Yifan#### Alternative vscode option (prototype) 76*d4726bddSHONG Yifan 77*d4726bddSHONG YifanAdd the following to your bazelrc: 78*d4726bddSHONG Yifan``` 79*d4726bddSHONG Yifanbuild --@rules_rust//:output_diagnostics=true --output_groups=+rust_lib_rustc_output,+rust_metadata_rustc_output 80*d4726bddSHONG Yifan``` 81*d4726bddSHONG Yifan 82*d4726bddSHONG YifanThen 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*d4726bddSHONG Yifan 84*d4726bddSHONG Yifan]]# 85