Skip to content

Commit 73f18b5

Browse files
committed
Rust: Force fixed rust-analyzer compatible toolchain
1 parent 8203f08 commit 73f18b5

10 files changed

Lines changed: 104 additions & 10 deletions

File tree

Cargo.lock

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

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ use_repo(
139139
"vendor_ts__ra_ap_stdx-0.0.347",
140140
"vendor_ts__ra_ap_syntax-0.0.347",
141141
"vendor_ts__ra_ap_syntax-bridge-0.0.347",
142+
"vendor_ts__ra_ap_toolchain-0.0.347",
142143
"vendor_ts__ra_ap_vfs-0.0.347",
143144
"vendor_ts__rand-0.10.2",
144145
"vendor_ts__rayon-1.12.0",

misc/bazel/3rdparty/tree_sitter_extractors_deps/BUILD.bazel

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

misc/bazel/3rdparty/tree_sitter_extractors_deps/crates.bzl

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

misc/bazel/3rdparty/tree_sitter_extractors_deps/ra_ap_toolchain-0.0.347/BUILD.bazel

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

misc/bazel/3rdparty/tree_sitter_extractors_deps/ra_ap_toolchain/BUILD.bazel

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

rust/extractor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ ra_ap_paths = "0.0.347"
2121
ra_ap_project_model = "0.0.347"
2222
ra_ap_syntax = "0.0.347"
2323
ra_ap_syntax-bridge = "0.0.347"
24+
ra_ap_toolchain = "0.0.347"
2425
ra_ap_vfs = "0.0.347"
2526
ra_ap_parser = "0.0.347"
2627
ra_ap_span = "0.0.347"

rust/extractor/src/config.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use std::collections::HashSet;
2323
use std::fmt::Debug;
2424
use std::ops::Not;
2525
use std::path::{Path, PathBuf};
26+
use tracing::{info, warn};
2627

2728
#[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, Clone, Copy, clap::ValueEnum)]
2829
#[serde(rename_all = "lowercase")]
@@ -140,9 +141,27 @@ impl Config {
140141
);
141142
}
142143
extra_env.extend(self.cargo_extra_env.clone());
144+
extra_env.insert(
145+
"RUSTUP_TOOLCHAIN".to_owned(),
146+
Some(crate::select_toolchain().to_owned()),
147+
);
143148
extra_env
144149
}
145150

151+
pub(crate) fn log_project_toolchain(&self, dir: &AbsPath) {
152+
match crate::project_toolchain(dir.as_str()) {
153+
Ok(output) if output.status.success() => info!(
154+
"project Rust toolchain: {}",
155+
String::from_utf8_lossy(&output.stdout).trim()
156+
),
157+
Ok(output) => warn!(
158+
"unable to determine project Rust toolchain: {}",
159+
String::from_utf8_lossy(&output.stderr).trim()
160+
),
161+
Err(error) => warn!("unable to determine project Rust toolchain: {error}"),
162+
}
163+
}
164+
146165
fn sysroot(&self, dir: &AbsPath) -> Sysroot {
147166
let sysroot_input = self.sysroot.as_ref().map(|p| join_path_buf(dir, p));
148167
let sysroot_src_input = self.sysroot_src.as_ref().map(|p| join_path_buf(dir, p));
@@ -186,6 +205,7 @@ impl Config {
186205

187206
pub fn to_cargo_config(&self, dir: &AbsPath) -> (CargoConfig, LoadCargoConfig) {
188207
let sysroot = self.sysroot(dir);
208+
info!("Using sysroot: {:?}", sysroot.root());
189209
(
190210
CargoConfig {
191211
all_targets: self.cargo_all_targets,

rust/extractor/src/main.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ use ra_ap_vfs::Vfs;
1515
use rust_analyzer::{ParseResult, RustAnalyzer};
1616
use std::collections::HashSet;
1717
use std::hash::RandomState;
18+
use std::io;
1819
use std::time::Instant;
1920
use std::{
2021
collections::HashMap,
2122
path::{Path, PathBuf},
2223
};
23-
use std::{env, fs};
24+
use std::{env, fs, process};
2425
use tracing::{error, info, warn};
2526
use tracing_subscriber::layer::SubscriberExt;
2627
use tracing_subscriber::util::SubscriberInitExt;
@@ -35,6 +36,39 @@ mod rust_analyzer;
3536
mod translate;
3637
pub mod trap;
3738

39+
/// The toolchain target by the extractor. When rust-analyzer is updated this
40+
/// number should be bumped to the last Rust toolchain release that preceedes
41+
/// the rust-analyzer release.
42+
const DEFAULT_RUST_TOOLCHAIN: &str = "1.97.0";
43+
44+
/// The command output of asking `rustup` which toolchain is used by the Rust
45+
/// project in `dir`.
46+
///
47+
/// Examples of what the stdout might look like when the command is successful:
48+
/// - `nightly-aarch64-apple-darwin (overridden by '/path/to/project/rust-toolchain.toml')`
49+
/// - `stable-aarch64-apple-darwin (default)`
50+
/// - `1.80.1-aarch64-apple-darwin (overridden by '/path/to/project/rust-toolchain.toml')`
51+
fn project_toolchain(dir: impl AsRef<Path>) -> io::Result<process::Output> {
52+
process::Command::new("rustup")
53+
.current_dir(dir)
54+
.args(["show", "active-toolchain"])
55+
.output()
56+
}
57+
58+
fn select_toolchain() -> &'static str {
59+
let uses_nightly = project_toolchain(".")
60+
.ok()
61+
.filter(|output| output.status.success())
62+
.and_then(|output| String::from_utf8(output.stdout).ok())
63+
.is_some_and(|toolchain| toolchain.starts_with("nightly"));
64+
65+
if uses_nightly {
66+
"nightly"
67+
} else {
68+
DEFAULT_RUST_TOOLCHAIN
69+
}
70+
}
71+
3872
struct Extractor<'a> {
3973
archiver: &'a Archiver,
4074
traps: &'a trap::TrapFileProvider,
@@ -269,6 +303,7 @@ fn main() -> anyhow::Result<()> {
269303
);
270304
}
271305
let cwd = cwd()?;
306+
cfg.log_project_toolchain(&cwd);
272307
let (cargo_config, load_cargo_config) = cfg.to_cargo_config(&cwd);
273308
let library_mode = if cfg.extract_dependencies_as_source {
274309
SourceKind::Source

rust/extractor/src/qltest.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use itertools::Itertools;
55
use std::ffi::OsStr;
66
use std::fs;
77
use std::path::Path;
8-
use std::process::Command;
98
use tracing::info;
109

1110
const DEFAULT_EDITION: &str = "2021";
@@ -91,15 +90,8 @@ fn set_sources(config: &mut Config) -> anyhow::Result<()> {
9190
}
9291

9392
fn cargo_check(config: &Config) -> anyhow::Result<()> {
94-
let mut command = Command::new("cargo");
93+
let mut command = ra_ap_toolchain::command("cargo", ".", &config.get_extra_env());
9594
command.env("CARGO_TARGET_DIR", config.cargo_target_dir());
96-
// Pass the extra environment variables to the initial `cargo check`.
97-
for (key, value) in config.get_extra_env() {
98-
match value {
99-
Some(value) => command.env(key, value),
100-
None => command.env_remove(key),
101-
};
102-
}
10395
let status = command
10496
.arg("check")
10597
.arg("-q")

0 commit comments

Comments
 (0)