Skip to content

Commit

Permalink
Merge pull request #18484 from github/redsun82/rust-discover-once
Browse files Browse the repository at this point in the history
Rust: run sysroot discovery once
  • Loading branch information
redsun82 authored Jan 14, 2025
2 parents 8833019 + 7988729 commit 23612b4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
15 changes: 11 additions & 4 deletions rust/extractor/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -111,6 +117,7 @@ impl Config {

CargoConfig {
sysroot,
sysroot_src,
target_dir,
features,
target,
Expand Down
12 changes: 11 additions & 1 deletion rust/extractor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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")?;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 23612b4

Please sign in to comment.