xref: /aosp_15_r20/external/bazelbuild-rules_rust/docs/rust_analyzer.md (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2*d4726bddSHONG Yifan# Rust Analyzer
3*d4726bddSHONG Yifan
4*d4726bddSHONG Yifan* [rust_analyzer_aspect](#rust_analyzer_aspect)
5*d4726bddSHONG Yifan* [rust_analyzer_toolchain](#rust_analyzer_toolchain)
6*d4726bddSHONG Yifan
7*d4726bddSHONG Yifan
8*d4726bddSHONG Yifan## Overview
9*d4726bddSHONG Yifan
10*d4726bddSHONG YifanFor [non-Cargo projects](https://rust-analyzer.github.io/manual.html#non-cargo-based-projects),
11*d4726bddSHONG Yifan[rust-analyzer](https://rust-analyzer.github.io/) depends on a `rust-project.json` file at the
12*d4726bddSHONG Yifanroot of the project that describes its structure. The `rust_analyzer` rule facilitates generating
13*d4726bddSHONG Yifansuch a file.
14*d4726bddSHONG Yifan
15*d4726bddSHONG Yifan### Setup
16*d4726bddSHONG Yifan
17*d4726bddSHONG YifanFirst, ensure `rules_rust` is setup in your workspace. By default, `rust_register_toolchains` will
18*d4726bddSHONG Yifanensure a [rust_analyzer_toolchain](#rust_analyzer_toolchain) is registered within the WORKSPACE.
19*d4726bddSHONG Yifan
20*d4726bddSHONG YifanNext, load the dependencies for the `rust-project.json` generator tool:
21*d4726bddSHONG Yifan
22*d4726bddSHONG Yifan```python
23*d4726bddSHONG Yifanload("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")
24*d4726bddSHONG Yifan
25*d4726bddSHONG Yifanrust_analyzer_dependencies()
26*d4726bddSHONG Yifan```
27*d4726bddSHONG Yifan
28*d4726bddSHONG YifanFinally, run `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project`
29*d4726bddSHONG Yifanwhenever dependencies change to regenerate the `rust-project.json` file. It
30*d4726bddSHONG Yifanshould be added to `.gitignore` because it is effectively a build artifact.
31*d4726bddSHONG YifanOnce the `rust-project.json` has been generated in the project root,
32*d4726bddSHONG Yifanrust-analyzer can pick it up upon restart.
33*d4726bddSHONG Yifan
34*d4726bddSHONG YifanFor users who do not use `rust_register_toolchains` to register toolchains, the following can be added
35*d4726bddSHONG Yifanto their WORKSPACE to register a `rust_analyzer_toolchain`. Please make sure the Rust version used in
36*d4726bddSHONG Yifanthis toolchain matches the version used by the currently registered toolchain or the sources/documentation
37*d4726bddSHONG Yifanwill not match what's being compiled with and can lead to confusing results.
38*d4726bddSHONG Yifan
39*d4726bddSHONG Yifan```python
40*d4726bddSHONG Yifanload("@rules_rust//rust:repositories.bzl", "rust_analyzer_toolchain_repository")
41*d4726bddSHONG Yifan
42*d4726bddSHONG Yifanregister_toolchains(rust_analyzer_toolchain_repository(
43*d4726bddSHONG Yifan    name = "rust_analyzer_toolchain",
44*d4726bddSHONG Yifan    # This should match the currently registered toolchain.
45*d4726bddSHONG Yifan    version = "1.63.0",
46*d4726bddSHONG Yifan))
47*d4726bddSHONG Yifan```
48*d4726bddSHONG Yifan
49*d4726bddSHONG Yifan#### VSCode
50*d4726bddSHONG Yifan
51*d4726bddSHONG YifanTo set this up using [VSCode](https://code.visualstudio.com/), users should first install the
52*d4726bddSHONG Yifan[rust_analyzer plugin](https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer).
53*d4726bddSHONG YifanWith that in place, the following task can be added to the `.vscode/tasks.json` file of the workspace
54*d4726bddSHONG Yifanto ensure a `rust-project.json` file is created and up to date when the editor is opened.
55*d4726bddSHONG Yifan
56*d4726bddSHONG Yifan```json
57*d4726bddSHONG Yifan{
58*d4726bddSHONG Yifan    "version": "2.0.0",
59*d4726bddSHONG Yifan    "tasks": [
60*d4726bddSHONG Yifan        {
61*d4726bddSHONG Yifan            "label": "Generate rust-project.json",
62*d4726bddSHONG Yifan            "command": "bazel",
63*d4726bddSHONG Yifan            "args": ["run", "@rules_rust//tools/rust_analyzer:gen_rust_project"],
64*d4726bddSHONG Yifan            "options": {
65*d4726bddSHONG Yifan                "cwd": "${workspaceFolder}"
66*d4726bddSHONG Yifan            },
67*d4726bddSHONG Yifan            "group": "build",
68*d4726bddSHONG Yifan            "problemMatcher": [],
69*d4726bddSHONG Yifan            "presentation": {
70*d4726bddSHONG Yifan                "reveal": "never",
71*d4726bddSHONG Yifan                "panel": "dedicated",
72*d4726bddSHONG Yifan            },
73*d4726bddSHONG Yifan            "runOptions": {
74*d4726bddSHONG Yifan                "runOn": "folderOpen"
75*d4726bddSHONG Yifan            }
76*d4726bddSHONG Yifan        },
77*d4726bddSHONG Yifan    ]
78*d4726bddSHONG Yifan}
79*d4726bddSHONG Yifan```
80*d4726bddSHONG Yifan
81*d4726bddSHONG Yifan#### Alternative vscode option (prototype)
82*d4726bddSHONG Yifan
83*d4726bddSHONG YifanAdd the following to your bazelrc:
84*d4726bddSHONG Yifan```
85*d4726bddSHONG Yifanbuild --@rules_rust//:output_diagnostics=true --output_groups=+rust_lib_rustc_output,+rust_metadata_rustc_output
86*d4726bddSHONG Yifan```
87*d4726bddSHONG Yifan
88*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.
89*d4726bddSHONG Yifan
90*d4726bddSHONG Yifan
91*d4726bddSHONG Yifan
92*d4726bddSHONG Yifan<a id="rust_analyzer_toolchain"></a>
93*d4726bddSHONG Yifan
94*d4726bddSHONG Yifan## rust_analyzer_toolchain
95*d4726bddSHONG Yifan
96*d4726bddSHONG Yifan<pre>
97*d4726bddSHONG Yifanrust_analyzer_toolchain(<a href="#rust_analyzer_toolchain-name">name</a>, <a href="#rust_analyzer_toolchain-proc_macro_srv">proc_macro_srv</a>, <a href="#rust_analyzer_toolchain-rustc">rustc</a>, <a href="#rust_analyzer_toolchain-rustc_srcs">rustc_srcs</a>)
98*d4726bddSHONG Yifan</pre>
99*d4726bddSHONG Yifan
100*d4726bddSHONG YifanA toolchain for [rust-analyzer](https://rust-analyzer.github.io/).
101*d4726bddSHONG Yifan
102*d4726bddSHONG Yifan**ATTRIBUTES**
103*d4726bddSHONG Yifan
104*d4726bddSHONG Yifan
105*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
106*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
107*d4726bddSHONG Yifan| <a id="rust_analyzer_toolchain-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
108*d4726bddSHONG Yifan| <a id="rust_analyzer_toolchain-proc_macro_srv"></a>proc_macro_srv |  The path to a `rust_analyzer_proc_macro_srv` binary.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
109*d4726bddSHONG Yifan| <a id="rust_analyzer_toolchain-rustc"></a>rustc |  The path to a `rustc` binary.   | <a href="https://bazel.build/concepts/labels">Label</a> | required |  |
110*d4726bddSHONG Yifan| <a id="rust_analyzer_toolchain-rustc_srcs"></a>rustc_srcs |  The source code of rustc.   | <a href="https://bazel.build/concepts/labels">Label</a> | required |  |
111*d4726bddSHONG Yifan
112*d4726bddSHONG Yifan
113*d4726bddSHONG Yifan<a id="rust_analyzer_aspect"></a>
114*d4726bddSHONG Yifan
115*d4726bddSHONG Yifan## rust_analyzer_aspect
116*d4726bddSHONG Yifan
117*d4726bddSHONG Yifan<pre>
118*d4726bddSHONG Yifanrust_analyzer_aspect(<a href="#rust_analyzer_aspect-name">name</a>)
119*d4726bddSHONG Yifan</pre>
120*d4726bddSHONG Yifan
121*d4726bddSHONG YifanAnnotates rust rules with RustAnalyzerInfo later used to build a rust-project.json
122*d4726bddSHONG Yifan
123*d4726bddSHONG Yifan**ASPECT ATTRIBUTES**
124*d4726bddSHONG Yifan
125*d4726bddSHONG Yifan
126*d4726bddSHONG Yifan| Name | Type |
127*d4726bddSHONG Yifan| :------------- | :------------- |
128*d4726bddSHONG Yifan| deps| String |
129*d4726bddSHONG Yifan| proc_macro_deps| String |
130*d4726bddSHONG Yifan| crate| String |
131*d4726bddSHONG Yifan| actual| String |
132*d4726bddSHONG Yifan| proto| String |
133*d4726bddSHONG Yifan
134*d4726bddSHONG Yifan
135*d4726bddSHONG Yifan**ATTRIBUTES**
136*d4726bddSHONG Yifan
137*d4726bddSHONG Yifan
138*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
139*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
140*d4726bddSHONG Yifan| <a id="rust_analyzer_aspect-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
141*d4726bddSHONG Yifan
142*d4726bddSHONG Yifan
143