Skip to content

Commit 086f2ea

Browse files
authored
Merge branch 'main' into prog
2 parents 24616b6 + 2b1fede commit 086f2ea

41 files changed

Lines changed: 2499 additions & 381 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.bazelci/presubmit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ tasks:
885885
build_flags:
886886
- "--compile_one_dependency"
887887
build_targets:
888-
- "tools/rust_analyzer/main.rs"
888+
- "tools/rust_analyzer/bin/gen_rust_project.rs"
889889
extensions_bindgen_linux:
890890
platform: ubuntu2004
891891
name: Extensions Bindgen

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ user.bazelrc
3030
.vscode
3131
*.code-workspace
3232

33+
# zed
34+
.zed
35+
3336
# JetBrains
3437
.idea
3538
.idea/**

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use_repo(
2222
internal_deps,
2323
"rrra",
2424
"rrra__anyhow-1.0.71",
25+
"rrra__camino-1.1.9",
2526
"rrra__clap-4.3.11",
2627
"rrra__env_logger-0.10.0",
2728
"rrra__itertools-0.11.0",

docs/rust_analyzer.vm

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
## Overview
33

44
For [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
6-
root of the project that describes its structure. The `rust_analyzer` rule facilitates generating
7-
such a file.
5+
[rust-analyzer](https://rust-analyzer.github.io/) depends on either a `rust-project.json` file
6+
at the root of the project that describes its structure or on build system specific
7+
[project auto-discovery](https://rust-analyzer.github.io/manual.html#rust-analyzer.workspace.discoverConfig).
8+
The `rust_analyzer` rules facilitate both approaches.
89

10+
## rust-project.json approach
911
### Setup
1012

1113
First, ensure `rules_rust` is setup in your workspace. By default, `rust_register_toolchains` will
@@ -84,4 +86,71 @@ build --@rules_rust//rust/settings:rustc_output_diagnostics=true --output_groups
8486

8587
Then 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.
8688

87-
]]#
89+
## Project auto-discovery
90+
### Setup
91+
92+
Auto-discovery makes `rust-analyzer` behave in a Bazel project in a similar fashion to how it does
93+
in a Cargo project. This is achieved by generating a structure similar to what `rust-project.json`
94+
contains but, instead of writing that to a file, the data gets piped to `rust-analyzer` directly
95+
through `stdout`. To use auto-discovery the `rust-analyzer` IDE settings must be configured similar to:
96+
97+
```json
98+
"rust-analyzer": {
99+
"workspace": {
100+
"discoverConfig": {
101+
"command": ["discover_bazel_rust_project.sh"],
102+
"progressLabel": "rust_analyzer",
103+
"filesToWatch": ["BUILD", "BUILD.bazel", "MODULE.bazel"]
104+
}
105+
}
106+
}
107+
```
108+
109+
The shell script passed to `discoverConfig.command` is typically meant to wrap the bazel rule invocation,
110+
primarily for muting `stderr` (because `rust-analyzer` will consider that an error has occurred if anything
111+
is passed through `stderr`) and, additionally, for specifying rule arguments. E.g:
112+
113+
```shell
114+
#!/usr/bin/bash
115+
116+
bazel \
117+
run \
118+
@rules_rust//tools/rust_analyzer:discover_bazel_rust_project -- \
119+
--bazel_startup_option=--output_base=~/ide_bazel \
120+
--bazel_arg=--watchfs \
121+
${1:+"$1"} 2>/dev/null
122+
```
123+
124+
The script above also handles an optional CLI argument which gets passed when workspace splitting is
125+
enabled. The script path should be either absolute or relative to the project root.
126+
127+
### Workspace splitting
128+
129+
The above configuration treats the entire project as a single workspace. However, large codebases might be
130+
too much to handle for `rust-analyzer` all at once. This can be addressed by splitting the codebase in
131+
multiple workspaces by extending the `discoverConfig.command` setting:
132+
133+
```json
134+
"rust-analyzer": {
135+
"workspace": {
136+
"discoverConfig": {
137+
"command": ["discover_bazel_rust_project.sh", "{arg}"],
138+
"progressLabel": "rust_analyzer",
139+
"filesToWatch": ["BUILD", "BUILD.bazel", "MODULE.bazel"]
140+
}
141+
}
142+
}
143+
```
144+
145+
`{arg}` acts as a placeholder that `rust-analyzer` replaces with the path of the source / build file
146+
that gets opened.
147+
148+
The root of the workspace will, in this configuration, be the package the crate currently being worked on
149+
belongs to. This means that only that package and its dependencies get built and indexed by `rust-analyzer`,
150+
thus allowing for a smaller footprint.
151+
152+
`rust-analyzer` will switch workspaces whenever an out-of-tree file gets opened, essentially indexing that
153+
crate and its dependencies separately. A caveat of this is that *dependents* of the crate currently being
154+
worked on are not indexed and won't be tracked by `rust-analyzer`.
155+
156+
]]#

examples/crate_universe/MODULE.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ use_repo(
603603
"crates_vendor_pkgs__tracing-0.1.41",
604604
"crates_vendor_pkgs__tracing-subscriber-0.3.19",
605605
"cvm",
606-
"cvm__tempfile-3.19.0",
606+
"cvm__serde_yaml-0.9.34-deprecated",
607+
"cvm__tempfile-3.19.1",
607608
"cvm__tokio-1.44.1",
608609
"cvm__tokio-test-0.4.4",
609610
)

examples/crate_universe/vendor_remote_manifests/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ edition = "2018"
66

77
[dependencies]
88
tokio = { version = "1.17.0", features = ["full"] }
9+
# the full version is "0.9.34+deprecated
10+
# to prevent regressions of the - and + replacements
11+
serde_yaml = "=0.9.34"
912

1013
[dev-dependencies]
1114
tempfile = "3.2.0"

examples/crate_universe/vendor_remote_manifests/crates/BUILD.bazel

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/crate_universe/vendor_remote_manifests/crates/BUILD.equivalent-1.0.2.bazel

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/crate_universe/vendor_remote_manifests/crates/BUILD.hashbrown-0.15.2.bazel

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)