11#!/usr/bin/env python3
22
3+ import base64
4+ import hashlib
35import json
6+ import re
47import shlex
58import subprocess
69import sys
1013
1114
1215REPO_ROOT = Path (__file__ ).resolve ().parents [2 ]
16+ MODULE_BAZEL = REPO_ROOT / "MODULE.bazel"
1317RUST_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- "\n Codegen failed. Carry out the instructions from step 4 in rust/updating-rust-analyzer.md manually." ,
39+ "\n Codegen 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
0 commit comments