Merge pull request #18484 from github/redsun82/rust-discover-once

Rust: run sysroot discovery once
This commit is contained in:
Paolo Tranquilli
2025-01-14 09:54:58 +01:00
committed by GitHub
2 changed files with 22 additions and 5 deletions

View File

@@ -12,9 +12,10 @@ use figment::{
use itertools::Itertools;
use num_traits::Zero;
use ra_ap_cfg::{CfgAtom, CfgDiff};
use ra_ap_ide_db::FxHashMap;
use ra_ap_intern::Symbol;
use ra_ap_paths::Utf8PathBuf;
use ra_ap_project_model::{CargoConfig, CargoFeatures, CfgOverrides, RustLibSource};
use ra_ap_paths::{AbsPath, Utf8PathBuf};
use ra_ap_project_model::{CargoConfig, CargoFeatures, CfgOverrides, RustLibSource, Sysroot};
use rust_extractor_macros::extractor_cli_config;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
@@ -85,8 +86,13 @@ impl Config {
figment.extract().context("loading configuration")
}
pub fn to_cargo_config(&self) -> CargoConfig {
let sysroot = Some(RustLibSource::Discover);
pub fn to_cargo_config(&self, dir: &AbsPath) -> CargoConfig {
let sysroot = Sysroot::discover(dir, &FxHashMap::default());
let sysroot_src = sysroot.src_root().map(ToOwned::to_owned);
let sysroot = sysroot
.root()
.map(ToOwned::to_owned)
.map(RustLibSource::Path);
let target_dir = self
.cargo_target_dir
@@ -111,6 +117,7 @@ impl Config {
CargoConfig {
sysroot,
sysroot_src,
target_dir,
features,
target,

View File

@@ -7,6 +7,7 @@ use log::{info, warn};
use ra_ap_hir::Semantics;
use ra_ap_ide_db::line_index::{LineCol, LineIndex};
use ra_ap_ide_db::RootDatabase;
use ra_ap_paths::{AbsPathBuf, Utf8PathBuf};
use ra_ap_project_model::{CargoConfig, ProjectManifest};
use ra_ap_vfs::Vfs;
use rust_analyzer::{ParseResult, RustAnalyzer};
@@ -161,6 +162,15 @@ impl<'a> Extractor<'a> {
}
}
fn cwd() -> anyhow::Result<AbsPathBuf> {
let path = std::env::current_dir().context("current directory")?;
let utf8_path = Utf8PathBuf::from_path_buf(path)
.map_err(|p| anyhow::anyhow!("{} is not a valid UTF-8 path", p.display()))?;
let abs_path = AbsPathBuf::try_from(utf8_path)
.map_err(|p| anyhow::anyhow!("{} is not absolute", p.as_str()))?;
Ok(abs_path)
}
fn main() -> anyhow::Result<()> {
let start = Instant::now();
let mut cfg = config::Config::extract().context("failed to load configuration")?;
@@ -204,7 +214,7 @@ fn main() -> anyhow::Result<()> {
}
extractor.extract_without_semantics(file, "no manifest found");
}
let cargo_config = cfg.to_cargo_config();
let cargo_config = cfg.to_cargo_config(&cwd()?);
for (manifest, files) in map.values().filter(|(_, files)| !files.is_empty()) {
if let Some((ref db, ref vfs)) = extractor.load_manifest(manifest, &cargo_config) {
let semantics = Semantics::new(db);