Skip to content

Commit 7e5ea2f

Browse files
committed
Rust: Update rust-analyzer update instructions and update script
1 parent 693d930 commit 7e5ea2f

3 files changed

Lines changed: 75 additions & 20 deletions

File tree

‎MODULE.bazel‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,15 @@ use_repo(
166166

167167
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
168168

169-
# rust-analyzer sources needed by the rust ast-generator (see `rust/ast-generator/README.md`)
169+
# rust-analyzer sources needed by the rust ast-generator (see `rust/ast-generator/README.md`).
170+
# These should be updated through the `rust/scripts/update_rust_analyzer.py` script.
170171
RUST_ANALYZER_SRC_TAG = "2026-04-13"
172+
RUST_ANALYZER_SRC_INTEGRITY = "sha256-UB/+EVx/6j4VGvnb7jfRqPaoc7Uwci3rEt6il+2J1Ds="
171173

172174
http_archive(
173175
name = "rust-analyzer-src",
174176
build_file = "//rust/ast-generator:BUILD.rust-analyzer-src.bazel",
175-
integrity = "sha256-UB/+EVx/6j4VGvnb7jfRqPaoc7Uwci3rEt6il+2J1Ds=",
177+
integrity = RUST_ANALYZER_SRC_INTEGRITY,
176178
patch_args = ["-p1"],
177179
patches = [
178180
"//rust/ast-generator:patches/rust-analyzer.patch",

‎rust/scripts/update_rust_analyzer.py‎

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python3
22

3+
import base64
4+
import hashlib
35
import json
6+
import re
47
import shlex
58
import subprocess
69
import sys
@@ -10,6 +13,7 @@
1013

1114

1215
REPO_ROOT = Path(__file__).resolve().parents[2]
16+
MODULE_BAZEL = REPO_ROOT / "MODULE.bazel"
1317
RUST_EXTRACTOR_MANIFEST = REPO_ROOT / "rust/extractor/Cargo.toml"
1418

1519
# The files that mention the fixed Rust toolchain version
@@ -32,7 +36,7 @@ def run_codegen() -> None:
3236
run("bazel", "run", "//rust/codegen")
3337
except subprocess.CalledProcessError as error:
3438
print(
35-
"\nCodegen failed. Carry out the instructions from step 4 in rust/updating-rust-analyzer.md manually.",
39+
"\nCodegen failed. Carry out the instructions from step 5 in rust/updating-rust-analyzer.md manually.",
3640
file=sys.stderr,
3741
flush=True,
3842
)
@@ -61,12 +65,48 @@ def fetch(url: str) -> bytes:
6165
return response.read()
6266

6367

64-
def get_compatible_rust_toolchain(rust_analyzer_version: str) -> str:
65-
"""Get the latest Rust toolchain released no later than rust-analyzer."""
66-
# Get the release date of the rust-analyzer version
68+
def get_rust_analyzer_release_date(rust_analyzer_version: str) -> str:
69+
"""Get the release date of a rust-analyzer crate version."""
6770
crate_url = f"https://crates.io/api/v1/crates/ra_ap_syntax/{rust_analyzer_version}"
6871
crate = json.loads(fetch(crate_url))
69-
rust_analyzer_release = crate["version"]["created_at"].split("T")[0]
72+
return crate["version"]["created_at"].split("T")[0]
73+
74+
75+
def update_rust_analyzer_sources(rust_analyzer_version: str) -> None:
76+
"""
77+
Update the rust-analyzer source archive used by the AST generator.
78+
79+
Concretly, this updates `RUST_ANALYZER_SRC_TAG` and
80+
`RUST_ANALYZER_SRC_INTEGRITY` in the MODULE.bazel file.
81+
"""
82+
83+
release_date = get_rust_analyzer_release_date(rust_analyzer_version)
84+
archive_url = (
85+
"https://github.com/rust-lang/rust-analyzer/archive/refs/tags/"
86+
f"{release_date}.tar.gz"
87+
)
88+
archive = fetch(archive_url)
89+
integrity = "sha256-" + base64.b64encode(hashlib.sha256(archive).digest()).decode()
90+
91+
module = MODULE_BAZEL.read_text()
92+
module = re.sub(
93+
r'RUST_ANALYZER_SRC_TAG = "[^"]+"',
94+
f'RUST_ANALYZER_SRC_TAG = "{release_date}"',
95+
module,
96+
count=1,
97+
)
98+
module = re.sub(
99+
r'RUST_ANALYZER_SRC_INTEGRITY = "[^"]+"',
100+
f'RUST_ANALYZER_SRC_INTEGRITY = "{integrity}"',
101+
module,
102+
count=1,
103+
)
104+
MODULE_BAZEL.write_text(module)
105+
106+
107+
def get_compatible_rust_toolchain(rust_analyzer_version: str) -> str:
108+
"""Get the latest Rust toolchain released no later than rust-analyzer."""
109+
rust_analyzer_release = get_rust_analyzer_release_date(rust_analyzer_version)
70110

71111
# `manifests.txt` is a list of all toolchains. The one we're interested in looks like
72112
# ```
@@ -168,20 +208,24 @@ def main() -> None:
168208
run("cargo", "update")
169209
commit_all("Cargo: Upgrade dependencies")
170210

171-
print_step(2, "Update the fixed Rust toolchain used by the extractor")
211+
print_step(2, "Update the rust-analyzer sources used by the AST generator")
212+
update_rust_analyzer_sources(new_rust_analyzer_version)
213+
commit_all("Rust: Update rust-analyzer sources")
214+
215+
print_step(3, "Update the fixed Rust toolchain used by the extractor")
172216
rust_toolchain = get_compatible_rust_toolchain(new_rust_analyzer_version)
173217
update_fixed_rust_toolchain_versions(rust_toolchain)
174218
commit_all("Rust: Update fixed toolchain")
175219

176-
print_step(3, "Regenerate vendored bazel files")
220+
print_step(4, "Regenerate vendored bazel files")
177221
run("misc/bazel/3rdparty/update_tree_sitter_extractors_deps.sh")
178222
commit_all("Bazel: Regenerate vendored cargo dependencies")
179223

180-
print_step(4, "Run codegen")
224+
print_step(5, "Run codegen")
181225
run_codegen()
182226
commit_all("Rust: Run codegen")
183227

184-
print_step(5, "Try compiling")
228+
print_step(6, "Try compiling")
185229
run("bazel", "run", "//rust:install")
186230

187231

‎rust/updating-rust-analyzer.md‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@ pkg-config`.
2828
git commit -am 'Cargo: upgrade dependencies' --no-verify
2929
```
3030
31-
2. Update the fixed Rust toolchain used by the extractor.
31+
2. Update the rust-analyzer sources used by the AST generator.
32+
33+
In `MODULE.bazel`, update `RUST_ANALYZER_SRC_TAG` to the release date of the
34+
new `ra_ap_` crates and update `RUST_ANALYZER_SRC_INTEGRITY` with the
35+
integrity of the corresponding source archive.
36+
37+
Commit the changes:
38+
`git commit -am 'Rust: Update rust-analyzer sources'`
39+
40+
3. Update the fixed Rust toolchain used by the extractor.
3241
3342
The version should be updated to the latest release that precedes the new
3443
rust-analyzer version.
@@ -38,23 +47,23 @@ pkg-config`.
3847
3948
Commit the changes: `git commit -am 'Rust: Update fixed toolchain'`
4049
41-
3. Regenerate vendored bazel files (these allow faster builds, particularly on
50+
4. Regenerate vendored bazel files (these allow faster builds, particularly on
4251
CI where it has to start from scratch each time), commit the changes:
4352
```
4453
misc/bazel/3rdparty/update_tree_sitter_extractors_deps.sh
4554
git add .
4655
git commit -am 'Bazel: regenerate vendored cargo dependencies' --no-verify
4756
```
4857
> [!NOTE]
49-
> If in step 5 you also bump `rules_rust` or the Rust toolchain used for
58+
> If in step 6 you also bump `rules_rust` or the Rust toolchain used for
5059
> building the extractor, those changes invalidate _all_ vendored files
5160
> (including the Python ones under `misc/bazel/3rdparty/py_deps`), not just
5261
> the tree-sitter ones. In that case run the umbrella script
5362
> `misc/bazel/3rdparty/update_cargo_deps.sh` instead (it regenerates both
5463
> `py_deps` and `tree_sitter_extractors_deps`, and runs `bazel mod tidy`),
5564
> then commit all the regenerated files.
5665
57-
4. Run codegen
66+
5. Run codegen
5867
```
5968
bazel run //rust/codegen
6069
```
@@ -70,7 +79,7 @@ pkg-config`.
7079
new tests and/or downgrade/upgrade scripts down the line.
7180
7281
73-
5. Try compiling
82+
6. Try compiling
7483
```
7584
bazel run //rust:install
7685
```
@@ -88,7 +97,7 @@ pkg-config`.
8897
to a more recent date while you're at it.
8998
* a toolchain and/or `rules_rust` bump invalidates the vendored files, so
9099
re-run `misc/bazel/3rdparty/update_cargo_deps.sh` (see the note in step
91-
3) and commit the regenerated files.
100+
4) and commit the regenerated files.
92101
* if it fails while compiling rust extractor code, you will need to adapt it
93102
to the new library version.
94103
* for example updating annotations in `annotations.py`, adding / removing
@@ -98,11 +107,11 @@ pkg-config`.
98107
toolchain, running `rust/lint.py` might reformat or apply new lints to the
99108
code.
100109
101-
6. Check with CI if everything is in order.
110+
7. Check with CI if everything is in order.
102111
103-
7. Run DCA with database caching disabled. Iterate on the code if needed.
112+
8. Run DCA with database caching disabled. Iterate on the code if needed.
104113
105-
8. If in step 4 the schema was updated, add upgrade/downgrade scripts and a
114+
9. If in step 5 the schema was updated, add upgrade/downgrade scripts and a
106115
change note. This is best done last to reduce the chance of merge conflicts
107116
(none of the other testing depends on having upgrade and downgrade scripts
108117
in place). See [Upgrading a language database

0 commit comments

Comments
 (0)