Skip to content

Commit 0e86b9d

Browse files
Added rust_analyzer_toolchain and repository rules for creating one (#1455)
* Added `rust_analyzer_toolchain` and repository rules for creating one * Regenerate documentation * Update rust/repositories.bzl Co-authored-by: Daniel Wagner-Hall <[email protected]> * Updated rust_analyzer_toolchain export * Fixed rust_analyzer_toolchain_repository rule Co-authored-by: Daniel Wagner-Hall <[email protected]>
1 parent 838e4ea commit 0e86b9d

File tree

17 files changed

+310
-107
lines changed

17 files changed

+310
-107
lines changed

WORKSPACE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_regi
44

55
rules_rust_dependencies()
66

7-
rust_register_toolchains(include_rustc_srcs = True)
7+
rust_register_toolchains()
88

99
load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")
1010

docs/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ PAGES = dict([
7272
header_template = ":rust_analyzer.vm",
7373
symbols = [
7474
"rust_analyzer_aspect",
75+
"rust_analyzer_toolchain",
7576
],
7677
),
7778
page(

docs/flatten.md

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [incompatible_flag](#incompatible_flag)
1515
* [rules_rust_dependencies](#rules_rust_dependencies)
1616
* [rust_analyzer_aspect](#rust_analyzer_aspect)
17+
* [rust_analyzer_toolchain](#rust_analyzer_toolchain)
1718
* [rust_binary](#rust_binary)
1819
* [rust_bindgen](#rust_bindgen)
1920
* [rust_bindgen_dependencies](#rust_bindgen_dependencies)
@@ -177,6 +178,25 @@ A rule defining an incompatible flag.
177178
| <a id="incompatible_flag-issue"></a>issue | The link to the github issue associated with this flag | String | required | |
178179

179180

181+
<a id="rust_analyzer_toolchain"></a>
182+
183+
## rust_analyzer_toolchain
184+
185+
<pre>
186+
rust_analyzer_toolchain(<a href="#rust_analyzer_toolchain-name">name</a>, <a href="#rust_analyzer_toolchain-rustc_srcs">rustc_srcs</a>)
187+
</pre>
188+
189+
A toolchain for [rust-analyzer](https://rust-analyzer.github.io/).
190+
191+
**ATTRIBUTES**
192+
193+
194+
| Name | Description | Type | Mandatory | Default |
195+
| :------------- | :------------- | :------------- | :------------- | :------------- |
196+
| <a id="rust_analyzer_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
197+
| <a id="rust_analyzer_toolchain-rustc_srcs"></a>rustc_srcs | The source code of rustc. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
198+
199+
180200
<a id="rust_binary"></a>
181201

182202
## rust_binary
@@ -1168,8 +1188,8 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r
11681188
## rust_toolchain_repository_proxy
11691189

11701190
<pre>
1171-
rust_toolchain_repository_proxy(<a href="#rust_toolchain_repository_proxy-name">name</a>, <a href="#rust_toolchain_repository_proxy-exec_compatible_with">exec_compatible_with</a>, <a href="#rust_toolchain_repository_proxy-exec_triple">exec_triple</a>, <a href="#rust_toolchain_repository_proxy-parent_workspace_name">parent_workspace_name</a>,
1172-
<a href="#rust_toolchain_repository_proxy-repo_mapping">repo_mapping</a>, <a href="#rust_toolchain_repository_proxy-target_compatible_with">target_compatible_with</a>, <a href="#rust_toolchain_repository_proxy-target_triple">target_triple</a>)
1191+
rust_toolchain_repository_proxy(<a href="#rust_toolchain_repository_proxy-name">name</a>, <a href="#rust_toolchain_repository_proxy-exec_compatible_with">exec_compatible_with</a>, <a href="#rust_toolchain_repository_proxy-repo_mapping">repo_mapping</a>, <a href="#rust_toolchain_repository_proxy-target_compatible_with">target_compatible_with</a>,
1192+
<a href="#rust_toolchain_repository_proxy-toolchain">toolchain</a>, <a href="#rust_toolchain_repository_proxy-toolchain_type">toolchain_type</a>)
11731193
</pre>
11741194

11751195
Generates a toolchain-bearing repository that declares the toolchains from some other rust_toolchain_repository.
@@ -1180,12 +1200,11 @@ Generates a toolchain-bearing repository that declares the toolchains from some
11801200
| Name | Description | Type | Mandatory | Default |
11811201
| :------------- | :------------- | :------------- | :------------- | :------------- |
11821202
| <a id="rust_toolchain_repository_proxy-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
1183-
| <a id="rust_toolchain_repository_proxy-exec_compatible_with"></a>exec_compatible_with | TODO | List of strings | optional | [] |
1184-
| <a id="rust_toolchain_repository_proxy-exec_triple"></a>exec_triple | The Rust-style target triple for the compilation platform | String | required | |
1185-
| <a id="rust_toolchain_repository_proxy-parent_workspace_name"></a>parent_workspace_name | The name of the other rust_toolchain_repository | String | required | |
1203+
| <a id="rust_toolchain_repository_proxy-exec_compatible_with"></a>exec_compatible_with | A list of constraints for the execution platform for this toolchain. | List of strings | optional | [] |
11861204
| <a id="rust_toolchain_repository_proxy-repo_mapping"></a>repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.&lt;p&gt;For example, an entry <code>"@foo": "@bar"</code> declares that, for any time this repository depends on <code>@foo</code> (such as a dependency on <code>@foo//some:target</code>, it should actually resolve that dependency within globally-declared <code>@bar</code> (<code>@bar//some:target</code>). | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | required | |
1187-
| <a id="rust_toolchain_repository_proxy-target_compatible_with"></a>target_compatible_with | TODO | List of strings | optional | [] |
1188-
| <a id="rust_toolchain_repository_proxy-target_triple"></a>target_triple | The Rust-style target that this compiler builds for | String | required | |
1205+
| <a id="rust_toolchain_repository_proxy-target_compatible_with"></a>target_compatible_with | A list of constraints for the target platform for this toolchain. | List of strings | optional | [] |
1206+
| <a id="rust_toolchain_repository_proxy-toolchain"></a>toolchain | The name of the toolchain implementation target. | String | required | |
1207+
| <a id="rust_toolchain_repository_proxy-toolchain_type"></a>toolchain_type | The toolchain type of the toolchain to declare | String | required | |
11891208

11901209

11911210
<a id="rust_toolchain_tools_repository"></a>
@@ -1200,7 +1219,7 @@ rust_toolchain_tools_repository(<a href="#rust_toolchain_tools_repository-name">
12001219

12011220
Composes a single workspace containing the toolchain components for compiling on a given platform to a series of target platforms.
12021221

1203-
A given instance of this rule should be accompanied by a rust_toolchain_repository_proxy invocation to declare its toolchains to Bazel; the indirection allows separating toolchain selection from toolchain fetching.
1222+
A given instance of this rule should be accompanied by a toolchain_repository_proxy invocation to declare its toolchains to Bazel; the indirection allows separating toolchain selection from toolchain fetching.
12041223

12051224
**ATTRIBUTES**
12061225

@@ -1834,9 +1853,9 @@ N.B. A "proxy repository" is needed to allow for registering the toolchain (with
18341853
| <a id="rust_toolchain_repository-target_triple"></a>target_triple | The Rust-style target to build for. | none |
18351854
| <a id="rust_toolchain_repository-exec_compatible_with"></a>exec_compatible_with | A list of constraints for the execution platform for this toolchain. | <code>None</code> |
18361855
| <a id="rust_toolchain_repository-target_compatible_with"></a>target_compatible_with | A list of constraints for the target platform for this toolchain. | <code>None</code> |
1837-
| <a id="rust_toolchain_repository-include_rustc_srcs"></a>include_rustc_srcs | Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. | <code>False</code> |
1838-
| <a id="rust_toolchain_repository-iso_date"></a>iso_date | The date of the tool. Defaults to None. | <code>None</code> |
1839-
| <a id="rust_toolchain_repository-rustfmt_version"></a>rustfmt_version | The version of rustfmt to be associated with the toolchain. Defaults to None. | <code>None</code> |
1856+
| <a id="rust_toolchain_repository-include_rustc_srcs"></a>include_rustc_srcs | Whether to download rustc's src code. This is required in order to use rust-analyzer support. | <code>False</code> |
1857+
| <a id="rust_toolchain_repository-iso_date"></a>iso_date | The date of the tool. | <code>None</code> |
1858+
| <a id="rust_toolchain_repository-rustfmt_version"></a>rustfmt_version | The version of rustfmt to be associated with the toolchain. | <code>None</code> |
18401859
| <a id="rust_toolchain_repository-edition"></a>edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its <code>edition</code> attribute. | <code>None</code> |
18411860
| <a id="rust_toolchain_repository-dev_components"></a>dev_components | Whether to download the rustc-dev components. Requires version to be "nightly". Defaults to False. | <code>False</code> |
18421861
| <a id="rust_toolchain_repository-sha256s"></a>sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. | <code>None</code> |

docs/rust_analyzer.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Rust Analyzer
33

44
* [rust_analyzer_aspect](#rust_analyzer_aspect)
5+
* [rust_analyzer_toolchain](#rust_analyzer_toolchain)
56

67

78
## Overview
@@ -13,30 +14,38 @@ such a file.
1314

1415
### Setup
1516

16-
First, add the following to the `WORKSPACE` file:
17+
First, ensure `rules_rust` is setup in your workspace. By default, `rust_register_toolchains` will
18+
ensure a [rust_analyzer_toolchain](#rust_analyzer_toolchain) is registered within the WORKSPACE.
19+
20+
Next, load the dependencies for the `rust-project.json` generator tool:
1721

1822
```python
1923
load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")
2024

2125
rust_analyzer_dependencies()
2226
```
2327

24-
Next, add the following lines to the `.bazelrc` file of your workspace:
25-
```
26-
build --repo_env=RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS=true
27-
```
28-
29-
This will ensure rust source code is available to `rust-analyzer`. Users
30-
can also set `include_rustc_srcs = True` on any `rust_repository` or
31-
`rust_repositories` calls in the workspace but the environment variable
32-
has higher priority and can override the attribute.
33-
3428
Finally, run `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project`
3529
whenever dependencies change to regenerate the `rust-project.json` file. It
3630
should be added to `.gitignore` because it is effectively a build artifact.
3731
Once the `rust-project.json` has been generated in the project root,
3832
rust-analyzer can pick it up upon restart.
3933

34+
For users who do not use `rust_register_toolchains` to register toolchains, the following can be added
35+
to their WORKSPACE to register a `rust_analyzer_toolchain`. Please make sure the Rust version used in
36+
this toolchain matches the version used by the currently registered toolchain or the sources/documentation
37+
# will not match what's being compiled with and can lead to confusing results.
38+
39+
```python
40+
load("@rules_rust//rust:repositories.bzl", "rust_analyzer_toolchain_repository")
41+
42+
register_toolchains(rust_analyzer_toolchain_repository(
43+
name = "rust_analyzer_toolchain",
44+
# This should match the currently registered toolchain.
45+
version = "1.62.0",
46+
))
47+
```
48+
4049
#### VSCode
4150

4251
To set this up using [VSCode](https://code.visualstudio.com/), users should first install the
@@ -70,6 +79,25 @@ to ensure a `rust-project.json` file is created and up to date when the editor i
7079
```
7180

7281

82+
<a id="rust_analyzer_toolchain"></a>
83+
84+
## rust_analyzer_toolchain
85+
86+
<pre>
87+
rust_analyzer_toolchain(<a href="#rust_analyzer_toolchain-name">name</a>, <a href="#rust_analyzer_toolchain-rustc_srcs">rustc_srcs</a>)
88+
</pre>
89+
90+
A toolchain for [rust-analyzer](https://rust-analyzer.github.io/).
91+
92+
**ATTRIBUTES**
93+
94+
95+
| Name | Description | Type | Mandatory | Default |
96+
| :------------- | :------------- | :------------- | :------------- | :------------- |
97+
| <a id="rust_analyzer_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
98+
| <a id="rust_analyzer_toolchain-rustc_srcs"></a>rustc_srcs | The source code of rustc. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
99+
100+
73101
<a id="rust_analyzer_aspect"></a>
74102

75103
## rust_analyzer_aspect

docs/rust_analyzer.vm

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,38 @@ such a file.
88

99
### Setup
1010

11-
First, add the following to the `WORKSPACE` file:
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:
1215

1316
```python
1417
load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")
1518

1619
rust_analyzer_dependencies()
1720
```
1821

19-
Next, add the following lines to the `.bazelrc` file of your workspace:
20-
```
21-
build --repo_env=RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS=true
22-
```
23-
24-
This will ensure rust source code is available to `rust-analyzer`. Users
25-
can also set `include_rustc_srcs = True` on any `rust_repository` or
26-
`rust_repositories` calls in the workspace but the environment variable
27-
has higher priority and can override the attribute.
28-
2922
Finally, run `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project`
3023
whenever dependencies change to regenerate the `rust-project.json` file. It
3124
should be added to `.gitignore` because it is effectively a build artifact.
3225
Once the `rust-project.json` has been generated in the project root,
3326
rust-analyzer can pick it up upon restart.
3427

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
31+
# will not match what's being compiled with and can lead to confusing results.
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+
3543
#### VSCode
3644

3745
To set this up using [VSCode](https://code.visualstudio.com/), users should first install the

0 commit comments

Comments
 (0)