From: Jesung Yang via B4 Relay <devnull+y.j3ms.n.gmail.com@kernel.org>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Tamir Duberstein" <tamird@kernel.org>
Cc: Eliot Courtney <ecourtney@nvidia.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
Jesung Yang <y.j3ms.n@gmail.com>
Subject: [PATCH v7 1/2] scripts: generate_rust_analyzer.py: add versioning infrastructure
Date: Thu, 07 May 2026 23:47:24 +0900 [thread overview]
Message-ID: <20260507-ra-fix-primitive-v7-1-cb8e3791f2b5@gmail.com> (raw)
In-Reply-To: <20260507-ra-fix-primitive-v7-0-cb8e3791f2b5@gmail.com>
From: Jesung Yang <y.j3ms.n@gmail.com>
Introduce multi-version support for rust-analyzer. The script now
executes `rust-analyzer --version` to query the version string.
This is a preparatory patch to address inherent method resolution
failures for primitive types occurring in rust-analyzer v0.3.2693
(2025-11-24) or later when used with our current `rust-project.json`
generation logic. Since the actual fix requires using the `sysroot_src`
field with a feature only available in rust-analyzer v0.3.2727
(2025-12-22) or later, this infrastructure is necessary to maintain
compatibility with older rust-analyzer releases.
Signed-off-by: Jesung Yang <y.j3ms.n@gmail.com>
---
scripts/generate_rust_analyzer.py | 186 +++++++++++++++++++++++++++++++++++++-
1 file changed, 182 insertions(+), 4 deletions(-)
diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index d5f9a0ca742c..79c69004aed1 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -4,6 +4,9 @@
"""
import argparse
+from datetime import date
+import enum
+import re
import json
import logging
import os
@@ -343,6 +346,161 @@ def generate_crates(
return crates
+
+Version = tuple[int, int, int]
+
+
+@enum.unique
+class RaVersionInfo(enum.Enum):
+ """
+ Represents rust-analyzer compatibility baselines. Concrete versions are
+ mapped to the most recent baseline they have reached. Must be in release
+ order.
+ """
+
+ # NOTE:
+ # This rust-analyzer release should be kept in sync with our MSRV (currently
+ # 1.85.0). When the MSRV is bumped, follow the steps below to retrieve the
+ # information needed to update this:
+ #
+ # 1) Clone both Rust and rust-analyzer repositories.
+ # ```console
+ # $ git clone https://github.com/rust-lang/rust.git
+ # $ git clone https://github.com/rust-lang/rust-analyzer.git
+ # ```
+ # 2) Run the following script, providing the new MSRV as an argument. It
+ # prints a link to the matching [1] rust-analyzer release page.
+ # ```bash
+ # #!/usr/bin/env bash
+ #
+ # set -euo pipefail
+ #
+ # RUST_VERSION=$1
+ #
+ # grep_args=()
+ # while IFS= read -r subject; do
+ # grep_args+=(--grep "$subject")
+ # done < <(git -C rust log \
+ # --fixed-strings \
+ # --format='%s' \
+ # --grep 'Merge pull request #' \
+ # --merges \
+ # --no-follow \
+ # -n 10 \
+ # "$RUST_VERSION" \
+ # -- src/tools/rust-analyzer
+ # )
+ #
+ # tag_predates=$(
+ # git -C rust-analyzer log \
+ # --fixed-strings \
+ # --format='%(describe:tags,abbrev=0)' \
+ # --merges \
+ # -n 1 \
+ # "${grep_args[@]}"
+ # )
+ #
+ # link_prefix="https://github.com/rust-lang/rust-analyzer/releases/tag"
+ # echo "$link_prefix/$tag_predates"
+ # ```
+ # 3) Grab the release date and the version string.
+ #
+ # [1] Note that rust-analyzer releases may not perfectly align with those
+ # shipped in upstream Rust. We take a conservative approach here: use
+ # the tag that directly predates the latest merge commit found upstream.
+ #
+ # v0.3.2228, released on 2024-12-23;
+ # shipped with the rustup 1.85.0 toolchain.
+ MSRV = (
+ date(2024, 12, 23),
+ (0, 3, 2228),
+ (1, 85, 0),
+ )
+
+ def __init__(
+ self,
+ release_date: date,
+ ra_version: Version,
+ rust_version: Version,
+ ) -> None:
+ self.release_date = release_date
+ self.ra_version = ra_version
+ self.rust_version = rust_version
+
+
+class RustProject(TypedDict):
+ crates: List[Crate]
+ sysroot: str
+
+
+def generate_rust_project(
+ _version_info: RaVersionInfo,
+ srctree: pathlib.Path,
+ objtree: pathlib.Path,
+ sysroot: pathlib.Path,
+ sysroot_src: pathlib.Path,
+ external_src: Optional[pathlib.Path],
+ cfgs: List[str],
+ core_edition: str,
+) -> RustProject:
+ rust_project: RustProject = {
+ "crates": generate_crates(
+ srctree, objtree, sysroot_src, external_src, cfgs, core_edition
+ ),
+ "sysroot": str(sysroot),
+ }
+
+ return rust_project
+
+def query_ra_version() -> Optional[str]:
+ try:
+ # Use the rust-analyzer binary found in $PATH.
+ ra_version_output = (
+ subprocess.check_output(
+ ["rust-analyzer", "--version"],
+ stdin=subprocess.DEVNULL,
+ )
+ .decode("utf-8")
+ .strip()
+ )
+ return ra_version_output
+ except FileNotFoundError:
+ return None
+
+def map_ra_version_baseline(ra_version_output: str) -> RaVersionInfo:
+ baselines = list(reversed(RaVersionInfo))
+
+ version_match = re.search(r"\d+\.\d+\.\d+", ra_version_output)
+ if version_match:
+ version_string = version_match.group()
+ found_version = tuple(map(int, version_string.split(".")))
+
+ # `rust-analyzer --version` shows a different version string depending
+ # on how the binary is built: it may print either the Rust version or
+ # the rust-analyzer version itself. To distinguish between them, we
+ # leverage rust-analyzer's versioning convention.
+ #
+ # See:
+ # - https://github.com/rust-lang/rust-analyzer/blob/fad5c3d2d642/xtask/src/dist.rs#L19-L21
+ is_ra_version = version_string.startswith(("0.3", "0.4", "0.5"))
+ if is_ra_version:
+ for info in baselines:
+ if found_version >= info.ra_version:
+ return info
+ else:
+ for info in baselines:
+ if found_version >= info.rust_version:
+ return info
+
+ date_match = re.search(r"\d{4}-\d{2}-\d{2}", ra_version_output)
+ if date_match:
+ found_date = date.fromisoformat(date_match.group())
+ for info in baselines:
+ if found_date >= info.release_date:
+ return info
+
+ return RaVersionInfo.MSRV
+
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', action='store_true')
@@ -371,10 +529,30 @@ def main() -> None:
level=logging.INFO if args.verbose else logging.WARNING
)
- rust_project = {
- "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.core_edition),
- "sysroot": str(args.sysroot),
- }
+ ra_version_output = query_ra_version()
+ if ra_version_output:
+ compatible_ra_version = map_ra_version_baseline(ra_version_output)
+ else:
+ logging.warning(
+ "Failed to find rust-analyzer in $PATH; " \
+ "falling back to `rust-project.json` for rust-analyzer " \
+ "%s, %s (shipped with Rust %s)",
+ ".".join(map(str, RaVersionInfo.MSRV.ra_version)),
+ RaVersionInfo.MSRV.release_date,
+ ".".join(map(str, RaVersionInfo.MSRV.rust_version)),
+ )
+ compatible_ra_version = RaVersionInfo.MSRV
+
+ rust_project = generate_rust_project(
+ compatible_ra_version,
+ args.srctree,
+ args.objtree,
+ args.sysroot,
+ args.sysroot_src,
+ args.exttree,
+ args.cfgs,
+ args.core_edition,
+ )
json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)
--
2.53.0
next prev parent reply other threads:[~2026-05-07 14:47 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-07 14:47 [PATCH v7 0/2] rust: take advantage of newer rust-analyzer features Jesung Yang via B4 Relay
2026-05-07 14:47 ` Jesung Yang via B4 Relay [this message]
2026-05-07 16:18 ` [PATCH v7 1/2] scripts: generate_rust_analyzer.py: add versioning infrastructure Tamir Duberstein
2026-05-07 14:47 ` [PATCH v7 2/2] scripts: generate_rust_analyzer.py: fix IDE support for primitive types Jesung Yang via B4 Relay
2026-05-07 16:19 ` Tamir Duberstein
2026-05-08 23:02 ` Jesung Yang
2026-05-11 18:11 ` Tamir Duberstein
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260507-ra-fix-primitive-v7-1-cb8e3791f2b5@gmail.com \
--to=devnull+y.j3ms.n.gmail.com@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=ecourtney@nvidia.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=y.j3ms.n@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox