Skip to content

Commit e37391e

Browse files
authored
Merge pull request #22647 from github/rust/rust-analyzer-update-docs
Rust: Update rust-analyzer update instructions and update script
2 parents eb88f92 + 1e1e174 commit e37391e

5 files changed

Lines changed: 89 additions & 23 deletions

File tree

‎.github/workflows/update-rust-analyzer.lock.yml‎

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

‎.github/workflows/update-rust-analyzer.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ safe-outputs:
7575
- "ruby/extractor/Cargo.toml"
7676
- "unified/extractor/Cargo.toml"
7777
- "unified/swift-syntax-rs/Cargo.toml"
78+
# A rust-analyzer update can require a newer Rust toolchain whose Clippy
79+
# lints require compatibility fixes in any of the Rust sources.
80+
# rust-analyzer dependency updates can also update tree-sitter APIs,
81+
# requiring compatibility fixes in the shared extractor implementation.
82+
- "shared/yeast/src/**"
83+
- "shared/yeast-macros/src/**"
84+
- "shared/yeast-schema/src/**"
85+
- "shared/tree-sitter-extractor/src/**"
7886
- "MODULE.bazel"
7987
- "MODULE.bazel.lock"
8088
- "rust-toolchain.toml"

‎MODULE.bazel‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,16 @@ use_repo(
167167

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

170-
# rust-analyzer sources needed by the rust ast-generator (see `rust/ast-generator/README.md`)
170+
# rust-analyzer sources needed by the rust ast-generator (see `rust/ast-generator/README.md`).
171+
# These should be updated through the `rust/scripts/update_rust_analyzer.py` script.
171172
RUST_ANALYZER_SRC_TAG = "2026-09-14"
172173

174+
RUST_ANALYZER_SRC_INTEGRITY = "sha256-X08+CNiaj0xCaqv01pODxfdcroEMKuhsg2CvBQmWldU="
175+
173176
http_archive(
174177
name = "rust-analyzer-src",
175178
build_file = "//rust/ast-generator:BUILD.rust-analyzer-src.bazel",
176-
integrity = "sha256-X08+CNiaj0xCaqv01pODxfdcroEMKuhsg2CvBQmWldU=",
179+
integrity = RUST_ANALYZER_SRC_INTEGRITY,
177180
patch_args = ["-p1"],
178181
patches = [
179182
"//rust/ast-generator:patches/rust-analyzer.patch",

‎rust/scripts/update_rust_analyzer.py‎

Lines changed: 55 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,50 @@ 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+
Concretely, 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, tag_replacements = re.subn(
93+
r'RUST_ANALYZER_SRC_TAG = "[^"]+"',
94+
f'RUST_ANALYZER_SRC_TAG = "{release_date}"',
95+
module,
96+
)
97+
if tag_replacements != 1:
98+
raise RuntimeError("expected exactly one RUST_ANALYZER_SRC_TAG assignment")
99+
module, integrity_replacements = re.subn(
100+
r'RUST_ANALYZER_SRC_INTEGRITY = "[^"]+"',
101+
f'RUST_ANALYZER_SRC_INTEGRITY = "{integrity}"',
102+
module,
103+
)
104+
if integrity_replacements != 1:
105+
raise RuntimeError("expected exactly one RUST_ANALYZER_SRC_INTEGRITY assignment")
106+
MODULE_BAZEL.write_text(module)
107+
108+
109+
def get_compatible_rust_toolchain(rust_analyzer_version: str) -> str:
110+
"""Get the latest Rust toolchain released no later than rust-analyzer."""
111+
rust_analyzer_release = get_rust_analyzer_release_date(rust_analyzer_version)
70112

71113
# `manifests.txt` is a list of all toolchains. The one we're interested in looks like
72114
# ```
@@ -168,20 +210,24 @@ def main() -> None:
168210
run("cargo", "update")
169211
commit_all("Cargo: Upgrade dependencies")
170212

171-
print_step(2, "Update the fixed Rust toolchain used by the extractor")
213+
print_step(2, "Update the rust-analyzer sources used by the AST generator")
214+
update_rust_analyzer_sources(new_rust_analyzer_version)
215+
commit_all("Rust: Update rust-analyzer sources")
216+
217+
print_step(3, "Update the fixed Rust toolchain used by the extractor")
172218
rust_toolchain = get_compatible_rust_toolchain(new_rust_analyzer_version)
173219
update_fixed_rust_toolchain_versions(rust_toolchain)
174220
commit_all("Rust: Update fixed toolchain")
175221

176-
print_step(3, "Regenerate vendored bazel files")
222+
print_step(4, "Regenerate vendored bazel files")
177223
run("misc/bazel/3rdparty/update_tree_sitter_extractors_deps.sh")
178224
commit_all("Bazel: Regenerate vendored cargo dependencies")
179225

180-
print_step(4, "Run codegen")
226+
print_step(5, "Run codegen")
181227
run_codegen()
182228
commit_all("Rust: Run codegen")
183229

184-
print_step(5, "Try compiling")
230+
print_step(6, "Try compiling")
185231
run("bazel", "run", "//rust:install")
186232

187233

0 commit comments

Comments
 (0)