fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
c3d71edd
main
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": [ 58 "run", 59 "//tools/rust_analyzer:gen_rust_project" 60 ], 61 "options": { 62 "cwd": "${workspaceFolder}" 63 }, 64 "group": "build", 65 "problemMatcher": [], 66 "presentation": { 67 "reveal": "never", 68 "panel": "dedicated", 69 }, 70 "runOptions": { 71 "runOn": "folderOpen" 72 } 73 }, 74 ] 75} 76``` 77 78#### Alternative vscode option (prototype) 79 80Add the following to your bazelrc: 81``` 82build --@rules_rust//rust/settings:rustc_output_diagnostics=true --output_groups=+rust_lib_rustc_output,+rust_metadata_rustc_output 83``` 84 85Then 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. 86 87]]#