diff --git a/crates/project_model/src/build_data.rs b/crates/project_model/src/build_data.rs index 3ff347e2c2e6..ce5b92217098 100644 --- a/crates/project_model/src/build_data.rs +++ b/crates/project_model/src/build_data.rs @@ -5,10 +5,11 @@ use std::{ io::BufReader, path::{Path, PathBuf}, process::{Command, Stdio}, + sync::Arc, }; use anyhow::Result; -use cargo_metadata::{BuildScript, Message, Package, PackageId}; +use cargo_metadata::{BuildScript, Message}; use itertools::Itertools; use paths::{AbsPath, AbsPathBuf}; use rustc_hash::FxHashMap; @@ -16,10 +17,6 @@ use stdx::JodChild; use crate::{cfg_flag::CfgFlag, CargoConfig}; -#[derive(Debug, Clone, Default)] -pub(crate) struct BuildDataMap { - data: FxHashMap, -} #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct BuildData { /// List of config flags defined by this package's build script @@ -35,131 +32,171 @@ pub struct BuildData { pub proc_macro_dylib_path: Option, } -impl BuildDataMap { - pub(crate) fn new( - cargo_toml: &AbsPath, - cargo_features: &CargoConfig, - packages: &Vec, - progress: &dyn Fn(String), - ) -> Result { - let mut cmd = Command::new(toolchain::cargo()); - cmd.args(&["check", "--workspace", "--message-format=json", "--manifest-path"]) - .arg(cargo_toml.as_ref()); - - // --all-targets includes tests, benches and examples in addition to the - // default lib and bins. This is an independent concept from the --targets - // flag below. - cmd.arg("--all-targets"); - - if let Some(target) = &cargo_features.target { - cmd.args(&["--target", target]); - } +#[derive(Clone, Debug)] +pub(crate) struct BuildDataConfig { + pub(crate) workspace_root: AbsPathBuf, + pub(crate) cargo_toml: AbsPathBuf, + pub(crate) cargo_features: CargoConfig, + pub(crate) packages: Arc>, +} - if cargo_features.all_features { - cmd.arg("--all-features"); - } else { - if cargo_features.no_default_features { - // FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures` - // https://github.com/oli-obk/cargo_metadata/issues/79 - cmd.arg("--no-default-features"); - } - if !cargo_features.features.is_empty() { - cmd.arg("--features"); - cmd.arg(cargo_features.features.join(" ")); - } - } +impl PartialEq for BuildDataConfig { + fn eq(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.packages, &other.packages) + } +} - cmd.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null()); - - let mut child = cmd.spawn().map(JodChild)?; - let child_stdout = child.stdout.take().unwrap(); - let stdout = BufReader::new(child_stdout); - - let mut res = BuildDataMap::default(); - for message in cargo_metadata::Message::parse_stream(stdout) { - if let Ok(message) = message { - match message { - Message::BuildScriptExecuted(BuildScript { - package_id, - out_dir, - cfgs, - env, - .. - }) => { - let cfgs = { - let mut acc = Vec::new(); - for cfg in cfgs { - match cfg.parse::() { - Ok(it) => acc.push(it), - Err(err) => { - anyhow::bail!("invalid cfg from cargo-metadata: {}", err) - } - }; - } - acc - }; - let res = res.data.entry(package_id.clone()).or_default(); - // cargo_metadata crate returns default (empty) path for - // older cargos, which is not absolute, so work around that. - if out_dir != PathBuf::default() { - let out_dir = AbsPathBuf::assert(out_dir); - res.out_dir = Some(out_dir); - res.cfgs = cfgs; - } +impl Eq for BuildDataConfig {} - res.envs = env; - } - Message::CompilerArtifact(message) => { - progress(format!("metadata {}", message.target.name)); - - if message.target.kind.contains(&"proc-macro".to_string()) { - let package_id = message.package_id; - // Skip rmeta file - if let Some(filename) = - message.filenames.iter().find(|name| is_dylib(name)) - { - let filename = AbsPathBuf::assert(filename.clone()); - let res = res.data.entry(package_id.clone()).or_default(); - res.proc_macro_dylib_path = Some(filename); - } - } - } - Message::CompilerMessage(message) => { - progress(message.target.name.clone()); - } - Message::Unknown => (), - Message::BuildFinished(_) => {} - Message::TextLine(_) => {} - } - } +#[derive(Debug, Default)] +pub struct BuildDataCollector { + configs: FxHashMap, +} + +#[derive(Debug, Default)] +pub struct BuildDataResult { + data: FxHashMap, +} + +pub type BuildDataMap = FxHashMap; + +impl BuildDataCollector { + pub(crate) fn add_config(&mut self, config: BuildDataConfig) { + self.configs.insert(config.workspace_root.to_path_buf().clone(), config); + } + + pub fn collect(&mut self, progress: &dyn Fn(String)) -> Result { + let mut res = BuildDataResult::default(); + for (path, config) in self.configs.iter() { + res.data.insert( + path.clone(), + collect_from_workspace( + &config.cargo_toml, + &config.cargo_features, + &config.packages, + progress, + )?, + ); } - res.inject_cargo_env(packages); Ok(res) } +} + +impl BuildDataResult { + pub(crate) fn get(&self, root: &AbsPath) -> Option<&BuildDataMap> { + self.data.get(&root.to_path_buf()) + } +} - pub(crate) fn with_cargo_env(packages: &Vec) -> Self { - let mut res = Self::default(); - res.inject_cargo_env(packages); - res +fn collect_from_workspace( + cargo_toml: &AbsPath, + cargo_features: &CargoConfig, + packages: &Vec, + progress: &dyn Fn(String), +) -> Result { + let mut cmd = Command::new(toolchain::cargo()); + cmd.args(&["check", "--workspace", "--message-format=json", "--manifest-path"]) + .arg(cargo_toml.as_ref()); + + // --all-targets includes tests, benches and examples in addition to the + // default lib and bins. This is an independent concept from the --targets + // flag below. + cmd.arg("--all-targets"); + + if let Some(target) = &cargo_features.target { + cmd.args(&["--target", target]); } - pub(crate) fn get(&self, id: &PackageId) -> Option<&BuildData> { - self.data.get(id) + if cargo_features.all_features { + cmd.arg("--all-features"); + } else { + if cargo_features.no_default_features { + // FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures` + // https://github.com/oli-obk/cargo_metadata/issues/79 + cmd.arg("--no-default-features"); + } + if !cargo_features.features.is_empty() { + cmd.arg("--features"); + cmd.arg(cargo_features.features.join(" ")); + } } - fn inject_cargo_env(&mut self, packages: &Vec) { - for meta_pkg in packages { - let resource = self.data.entry(meta_pkg.id.clone()).or_default(); - inject_cargo_env(meta_pkg, &mut resource.envs); + cmd.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null()); + + let mut child = cmd.spawn().map(JodChild)?; + let child_stdout = child.stdout.take().unwrap(); + let stdout = BufReader::new(child_stdout); + + let mut res = BuildDataMap::default(); + for message in cargo_metadata::Message::parse_stream(stdout) { + if let Ok(message) = message { + match message { + Message::BuildScriptExecuted(BuildScript { + package_id, + out_dir, + cfgs, + env, + .. + }) => { + let cfgs = { + let mut acc = Vec::new(); + for cfg in cfgs { + match cfg.parse::() { + Ok(it) => acc.push(it), + Err(err) => { + anyhow::bail!("invalid cfg from cargo-metadata: {}", err) + } + }; + } + acc + }; + let res = res.entry(package_id.repr.clone()).or_default(); + // cargo_metadata crate returns default (empty) path for + // older cargos, which is not absolute, so work around that. + if out_dir != PathBuf::default() { + let out_dir = AbsPathBuf::assert(out_dir); + res.out_dir = Some(out_dir); + res.cfgs = cfgs; + } - if let Some(out_dir) = &resource.out_dir { - // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!() - if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) { - resource.envs.push(("OUT_DIR".to_string(), out_dir)); + res.envs = env; } + Message::CompilerArtifact(message) => { + progress(format!("metadata {}", message.target.name)); + + if message.target.kind.contains(&"proc-macro".to_string()) { + let package_id = message.package_id; + // Skip rmeta file + if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name)) + { + let filename = AbsPathBuf::assert(filename.clone()); + let res = res.entry(package_id.repr.clone()).or_default(); + res.proc_macro_dylib_path = Some(filename); + } + } + } + Message::CompilerMessage(message) => { + progress(message.target.name.clone()); + } + Message::Unknown => (), + Message::BuildFinished(_) => {} + Message::TextLine(_) => {} + } + } + } + + for package in packages { + let build_data = res.entry(package.id.repr.clone()).or_default(); + inject_cargo_env(package, build_data); + if let Some(out_dir) = &build_data.out_dir { + // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!() + if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) { + build_data.envs.push(("OUT_DIR".to_string(), out_dir)); } } } + + Ok(res) } // FIXME: File a better way to know if it is a dylib @@ -173,7 +210,9 @@ fn is_dylib(path: &Path) -> bool { /// Recreates the compile-time environment variables that Cargo sets. /// /// Should be synced with -fn inject_cargo_env(package: &cargo_metadata::Package, env: &mut Vec<(String, String)>) { +fn inject_cargo_env(package: &cargo_metadata::Package, build_data: &mut BuildData) { + let env = &mut build_data.envs; + // FIXME: Missing variables: // CARGO_PKG_HOMEPAGE, CARGO_CRATE_NAME, CARGO_BIN_NAME, CARGO_BIN_EXE_ diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs index c8a5333c4409..a6fed10cd129 100644 --- a/crates/project_model/src/cargo_workspace.rs +++ b/crates/project_model/src/cargo_workspace.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use std::{convert::TryInto, ops, process::Command}; +use std::{convert::TryInto, ops, process::Command, sync::Arc}; use anyhow::{Context, Result}; use base_db::Edition; @@ -9,7 +9,7 @@ use la_arena::{Arena, Idx}; use paths::{AbsPath, AbsPathBuf}; use rustc_hash::FxHashMap; -use crate::build_data::{BuildData, BuildDataMap}; +use crate::build_data::BuildDataConfig; use crate::utf8_stdout; /// `CargoWorkspace` represents the logical structure of, well, a Cargo @@ -26,7 +26,7 @@ use crate::utf8_stdout; pub struct CargoWorkspace { packages: Arena, targets: Arena, - workspace_root: AbsPathBuf, + build_data_config: BuildDataConfig, } impl ops::Index for CargoWorkspace { @@ -55,9 +55,6 @@ pub struct CargoConfig { /// This will be ignored if `cargo_all_features` is true. pub features: Vec, - /// Runs cargo check on launch to figure out the correct values of OUT_DIR - pub load_out_dirs_from_check: bool, - /// rustc target pub target: Option, @@ -94,8 +91,8 @@ pub struct PackageData { pub features: FxHashMap>, /// List of features enabled on this package pub active_features: Vec, - /// Build script related data for this package - pub build_data: BuildData, + // String representation of package id + pub id: String, } #[derive(Debug, Clone, Eq, PartialEq)] @@ -228,12 +225,6 @@ impl CargoWorkspace { ) })?; - let resources = if config.load_out_dirs_from_check { - BuildDataMap::new(cargo_toml, config, &meta.packages, progress)? - } else { - BuildDataMap::with_cargo_env(&meta.packages) - }; - let mut pkg_by_id = FxHashMap::default(); let mut packages = Arena::default(); let mut targets = Arena::default(); @@ -241,10 +232,7 @@ impl CargoWorkspace { let ws_members = &meta.workspace_members; meta.packages.sort_by(|a, b| a.id.cmp(&b.id)); - for meta_pkg in meta.packages { - let id = meta_pkg.id.clone(); - let build_data = resources.get(&id).cloned().unwrap_or_default(); - + for meta_pkg in &meta.packages { let cargo_metadata::Package { id, edition, name, manifest_path, version, .. } = meta_pkg; let is_member = ws_members.contains(&id); @@ -252,24 +240,24 @@ impl CargoWorkspace { .parse::() .with_context(|| format!("Failed to parse edition {}", edition))?; let pkg = packages.alloc(PackageData { - name, + id: id.repr.clone(), + name: name.clone(), version: version.to_string(), - manifest: AbsPathBuf::assert(manifest_path), + manifest: AbsPathBuf::assert(manifest_path.clone()), targets: Vec::new(), is_member, edition, dependencies: Vec::new(), - features: meta_pkg.features.into_iter().collect(), + features: meta_pkg.features.clone().into_iter().collect(), active_features: Vec::new(), - build_data, }); let pkg_data = &mut packages[pkg]; pkg_by_id.insert(id, pkg); - for meta_tgt in meta_pkg.targets { + for meta_tgt in &meta_pkg.targets { let is_proc_macro = meta_tgt.kind.as_slice() == ["proc-macro"]; let tgt = targets.alloc(TargetData { package: pkg, - name: meta_tgt.name, + name: meta_tgt.name.clone(), root: AbsPathBuf::assert(meta_tgt.src_path.clone()), kind: TargetKind::new(meta_tgt.kind.as_slice()), is_proc_macro, @@ -308,7 +296,14 @@ impl CargoWorkspace { } let workspace_root = AbsPathBuf::assert(meta.workspace_root); - Ok(CargoWorkspace { packages, targets, workspace_root: workspace_root }) + let build_data_config = BuildDataConfig { + workspace_root: workspace_root.clone(), + cargo_toml: cargo_toml.to_path_buf(), + cargo_features: config.clone(), + packages: Arc::new(meta.packages.clone()), + }; + + Ok(CargoWorkspace { packages, targets, build_data_config }) } pub fn packages<'a>(&'a self) -> impl Iterator + ExactSizeIterator + 'a { @@ -323,7 +318,7 @@ impl CargoWorkspace { } pub fn workspace_root(&self) -> &AbsPath { - &self.workspace_root + &self.build_data_config.workspace_root } pub fn package_flag(&self, package: &PackageData) -> String { @@ -334,6 +329,10 @@ impl CargoWorkspace { } } + pub(crate) fn build_data_config(&self) -> &BuildDataConfig { + &self.build_data_config + } + fn is_unique(&self, name: &str) -> bool { self.packages.iter().filter(|(_, v)| v.name == name).count() == 1 } diff --git a/crates/project_model/src/lib.rs b/crates/project_model/src/lib.rs index 525c336e6347..d712095a6917 100644 --- a/crates/project_model/src/lib.rs +++ b/crates/project_model/src/lib.rs @@ -19,6 +19,7 @@ use paths::{AbsPath, AbsPathBuf}; use rustc_hash::FxHashSet; pub use crate::{ + build_data::{BuildDataCollector, BuildDataResult}, cargo_workspace::{ CargoConfig, CargoWorkspace, Package, PackageData, PackageDependency, Target, TargetData, TargetKind, diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs index bc5041e5a3d5..966731d06b0a 100644 --- a/crates/project_model/src/workspace.rs +++ b/crates/project_model/src/workspace.rs @@ -16,8 +16,13 @@ use proc_macro_api::ProcMacroClient; use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ - cargo_workspace, cfg_flag::CfgFlag, rustc_cfg, sysroot::SysrootCrate, utf8_stdout, CargoConfig, - CargoWorkspace, ProjectJson, ProjectManifest, Sysroot, TargetKind, + build_data::{BuildData, BuildDataMap, BuildDataResult}, + cargo_workspace, + cfg_flag::CfgFlag, + rustc_cfg, + sysroot::SysrootCrate, + utf8_stdout, BuildDataCollector, CargoConfig, CargoWorkspace, ProjectJson, ProjectManifest, + Sysroot, TargetKind, }; /// `PackageRoot` describes a package root folder. @@ -152,7 +157,7 @@ impl ProjectWorkspace { /// Returns the roots for the current `ProjectWorkspace` /// The return type contains the path and whether or not /// the root is a member of the current workspace - pub fn to_roots(&self) -> Vec { + pub fn to_roots(&self, build_data: Option<&BuildDataResult>) -> Vec { match self { ProjectWorkspace::Json { project, sysroot, rustc_cfg: _ } => project .crates() @@ -178,7 +183,12 @@ impl ProjectWorkspace { let pkg_root = cargo[pkg].root().to_path_buf(); let mut include = vec![pkg_root.clone()]; - include.extend(cargo[pkg].build_data.out_dir.clone()); + include.extend( + build_data + .and_then(|it| it.get(cargo.workspace_root())) + .and_then(|map| map.get(&cargo[pkg].id)) + .and_then(|it| it.out_dir.clone()), + ); let mut exclude = vec![pkg_root.join(".git")]; if is_member { @@ -218,6 +228,7 @@ impl ProjectWorkspace { pub fn to_crate_graph( &self, + build_data: Option<&BuildDataResult>, proc_macro_client: Option<&ProcMacroClient>, load: &mut dyn FnMut(&AbsPath) -> Option, ) -> CrateGraph { @@ -240,8 +251,10 @@ impl ProjectWorkspace { &proc_macro_loader, load, cargo, + build_data.and_then(|it| it.get(cargo.workspace_root())), sysroot, rustc, + rustc.as_ref().zip(build_data).and_then(|(it, map)| map.get(it.workspace_root())), ), }; if crate_graph.patch_cfg_if() { @@ -251,6 +264,18 @@ impl ProjectWorkspace { } crate_graph } + + pub fn collect_build_data_configs(&self, loader: &mut BuildDataCollector) { + match self { + ProjectWorkspace::Cargo { cargo, rustc, .. } => { + loader.add_config(cargo.build_data_config().clone()); + if let Some(rustc) = rustc { + loader.add_config(rustc.build_data_config().clone()); + } + } + _ => {} + } + } } fn project_json_to_crate_graph( @@ -323,8 +348,10 @@ fn cargo_to_crate_graph( proc_macro_loader: &dyn Fn(&Path) -> Vec, load: &mut dyn FnMut(&AbsPath) -> Option, cargo: &CargoWorkspace, + build_data_map: Option<&BuildDataMap>, sysroot: &Sysroot, rustc: &Option, + rustc_build_data_map: Option<&BuildDataMap>, ) -> CrateGraph { let _p = profile::span("cargo_to_crate_graph"); let mut crate_graph = CrateGraph::default(); @@ -350,6 +377,7 @@ fn cargo_to_crate_graph( let crate_id = add_target_crate_root( &mut crate_graph, &cargo[pkg], + build_data_map.and_then(|it| it.get(&cargo[pkg].id)), &cfg_options, proc_macro_loader, file_id, @@ -426,6 +454,7 @@ fn cargo_to_crate_graph( let crate_id = add_target_crate_root( &mut crate_graph, &rustc_workspace[pkg], + rustc_build_data_map.and_then(|it| it.get(&rustc_workspace[pkg].id)), &cfg_options, proc_macro_loader, file_id, @@ -474,6 +503,7 @@ fn cargo_to_crate_graph( fn add_target_crate_root( crate_graph: &mut CrateGraph, pkg: &cargo_workspace::PackageData, + build_data: Option<&BuildData>, cfg_options: &CfgOptions, proc_macro_loader: &dyn Fn(&Path) -> Vec, file_id: FileId, @@ -484,19 +514,22 @@ fn add_target_crate_root( for feature in pkg.active_features.iter() { opts.insert_key_value("feature".into(), feature.into()); } - opts.extend(pkg.build_data.cfgs.iter().cloned()); + if let Some(cfgs) = build_data.as_ref().map(|it| &it.cfgs) { + opts.extend(cfgs.iter().cloned()); + } opts }; let mut env = Env::default(); - for (k, v) in &pkg.build_data.envs { - env.set(k, v.clone()); + if let Some(envs) = build_data.map(|it| &it.envs) { + for (k, v) in envs { + env.set(k, v.clone()); + } } - let proc_macro = pkg - .build_data - .proc_macro_dylib_path + let proc_macro = build_data .as_ref() + .and_then(|it| it.proc_macro_dylib_path.as_ref()) .map(|it| proc_macro_loader(&it)) .unwrap_or_default(); diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs index dbab4f5f4638..86756a00696a 100644 --- a/crates/rust-analyzer/src/cli/load_cargo.rs +++ b/crates/rust-analyzer/src/cli/load_cargo.rs @@ -6,7 +6,9 @@ use anyhow::Result; use crossbeam_channel::{unbounded, Receiver}; use ide::{AnalysisHost, Change}; use ide_db::base_db::CrateGraph; -use project_model::{CargoConfig, ProcMacroClient, ProjectManifest, ProjectWorkspace}; +use project_model::{ + BuildDataCollector, CargoConfig, ProcMacroClient, ProjectManifest, ProjectWorkspace, +}; use vfs::{loader::Handle, AbsPath, AbsPathBuf}; use crate::reload::{ProjectFolders, SourceRootConfig}; @@ -18,11 +20,7 @@ pub fn load_cargo( ) -> Result<(AnalysisHost, vfs::Vfs)> { let root = AbsPathBuf::assert(std::env::current_dir()?.join(root)); let root = ProjectManifest::discover_single(&root)?; - let ws = ProjectWorkspace::load( - root, - &CargoConfig { load_out_dirs_from_check, ..Default::default() }, - &|_| {}, - )?; + let ws = ProjectWorkspace::load(root, &CargoConfig::default(), &|_| {})?; let (sender, receiver) = unbounded(); let mut vfs = vfs::Vfs::default(); @@ -39,14 +37,26 @@ pub fn load_cargo( None }; - let crate_graph = ws.to_crate_graph(proc_macro_client.as_ref(), &mut |path: &AbsPath| { - let contents = loader.load_sync(path); - let path = vfs::VfsPath::from(path.to_path_buf()); - vfs.set_file_contents(path.clone(), contents); - vfs.file_id(&path) - }); + let build_data = if load_out_dirs_from_check { + let mut loader = BuildDataCollector::default(); + ws.collect_build_data_configs(&mut loader); + Some(loader.collect(&|_| {})?) + } else { + None + }; + + let crate_graph = ws.to_crate_graph( + build_data.as_ref(), + proc_macro_client.as_ref(), + &mut |path: &AbsPath| { + let contents = loader.load_sync(path); + let path = vfs::VfsPath::from(path.to_path_buf()); + vfs.set_file_contents(path.clone(), contents); + vfs.file_id(&path) + }, + ); - let project_folders = ProjectFolders::new(&[ws], &[]); + let project_folders = ProjectFolders::new(&[ws], &[], build_data); loader.set_config(vfs::loader::Config { load: project_folders.load, watch: vec![] }); log::debug!("crate graph: {:?}", crate_graph); diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 37487b6ac37a..cc0b22bffb16 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -469,6 +469,9 @@ impl Config { pub fn cargo_autoreload(&self) -> bool { self.data.cargo_autoreload } + pub fn load_out_dirs_from_check(&self) -> bool { + self.data.cargo_loadOutDirsFromCheck + } pub fn cargo(&self) -> CargoConfig { let rustc_source = self.data.rustcSource.as_ref().map(|it| self.root_path.join(&it)); @@ -476,7 +479,6 @@ impl Config { no_default_features: self.data.cargo_noDefaultFeatures, all_features: self.data.cargo_allFeatures, features: self.data.cargo_features.clone(), - load_out_dirs_from_check: self.data.cargo_loadOutDirsFromCheck, target: self.data.cargo_target.clone(), rustc_source, no_sysroot: self.data.cargo_noSysroot, diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 442fbd14c6eb..8f446edfaa60 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -11,7 +11,9 @@ use ide::{Analysis, AnalysisHost, Change, FileId}; use ide_db::base_db::{CrateId, VfsPath}; use lsp_types::{SemanticTokens, Url}; use parking_lot::{Mutex, RwLock}; -use project_model::{CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target}; +use project_model::{ + BuildDataCollector, BuildDataResult, CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target, +}; use rustc_hash::FxHashMap; use vfs::AnchoredPathBuf; @@ -80,6 +82,9 @@ pub(crate) struct GlobalState { pub(crate) proc_macro_client: Option, pub(crate) workspaces: Arc>, pub(crate) fetch_workspaces_queue: OpQueue, + pub(crate) build_data_result: Option, + pub(crate) build_data_loader: Option, + pub(crate) fetch_build_data_queue: OpQueue, latest_requests: Arc>, } @@ -133,6 +138,9 @@ impl GlobalState { proc_macro_client: None, workspaces: Arc::new(Vec::new()), fetch_workspaces_queue: OpQueue::default(), + build_data_result: None, + build_data_loader: None, + fetch_build_data_queue: OpQueue::default(), latest_requests: Default::default(), } } diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 6d2475a590a7..67570da55014 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -21,7 +21,7 @@ use crate::{ global_state::{file_id_to_url, url_to_file_id, GlobalState, Status}, handlers, lsp_ext, lsp_utils::{apply_document_changes, is_canceled, notification_is, Progress}, - reload::ProjectWorkspaceProgress, + reload::{BuildDataProgress, ProjectWorkspaceProgress}, Result, }; @@ -63,6 +63,7 @@ pub(crate) enum Task { Diagnostics(Vec<(FileId, Vec)>), PrimeCaches(PrimeCachesProgress), FetchWorkspace(ProjectWorkspaceProgress), + FetchBuildData(BuildDataProgress), } impl fmt::Debug for Event { @@ -232,6 +233,22 @@ impl GlobalState { }; self.report_progress("fetching", state, msg, None); } + Task::FetchBuildData(progress) => { + let (state, msg) = match progress { + BuildDataProgress::Begin => (Some(Progress::Begin), None), + BuildDataProgress::Report(msg) => { + (Some(Progress::Report), Some(msg)) + } + BuildDataProgress::End(loader) => { + self.fetch_build_data_completed(); + self.update_build_data(loader); + (Some(Progress::End), None) + } + }; + if let Some(state) = state { + self.report_progress("loading", state, msg, None); + } + } } // Coalesce multiple task events into one loop turn task = match self.task_pool.receiver.try_recv() { @@ -408,6 +425,7 @@ impl GlobalState { } self.fetch_workspaces_if_needed(); + self.fetch_build_data_if_needed(); let loop_duration = loop_start.elapsed(); if loop_duration > Duration::from_millis(100) { diff --git a/crates/rust-analyzer/src/reload.rs b/crates/rust-analyzer/src/reload.rs index 0507186dce52..c9356fedf830 100644 --- a/crates/rust-analyzer/src/reload.rs +++ b/crates/rust-analyzer/src/reload.rs @@ -4,7 +4,7 @@ use std::{mem, sync::Arc}; use flycheck::{FlycheckConfig, FlycheckHandle}; use ide::Change; use ide_db::base_db::{CrateGraph, SourceRoot, VfsPath}; -use project_model::{ProcMacroClient, ProjectWorkspace}; +use project_model::{BuildDataCollector, BuildDataResult, ProcMacroClient, ProjectWorkspace}; use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind}; use crate::{ @@ -22,6 +22,13 @@ pub(crate) enum ProjectWorkspaceProgress { End(Vec>), } +#[derive(Debug)] +pub(crate) enum BuildDataProgress { + Begin, + Report(String), + End(anyhow::Result), +} + impl GlobalState { pub(crate) fn update_configuration(&mut self, config: Config) { let _p = profile::span("GlobalState::update_configuration"); @@ -99,6 +106,56 @@ impl GlobalState { } } + pub(crate) fn fetch_build_data_request(&mut self, build_data_lo: BuildDataCollector) { + self.build_data_loader = Some(build_data_lo); + self.fetch_build_data_queue.request_op(); + } + + pub(crate) fn fetch_build_data_if_needed(&mut self) { + if !self.fetch_build_data_queue.should_start_op() { + return; + } + let mut build_data_loader = match self.build_data_loader.take() { + Some(it) => it, + None => return, + }; + self.task_pool.handle.spawn_with_sender(move |sender| { + sender.send(Task::FetchBuildData(BuildDataProgress::Begin)).unwrap(); + + let progress = { + let sender = sender.clone(); + move |msg| { + sender.send(Task::FetchBuildData(BuildDataProgress::Report(msg))).unwrap() + } + }; + let res = build_data_loader.collect(&progress); + sender.send(Task::FetchBuildData(BuildDataProgress::End(res))).unwrap(); + }); + } + pub(crate) fn fetch_build_data_completed(&mut self) { + self.fetch_build_data_queue.op_completed() + } + pub(crate) fn update_build_data(&mut self, build_data_result: anyhow::Result) { + self.build_data_result = match build_data_result { + Ok(it) => Some(it), + Err(err) => { + log::error!("failed to fetch build data: {:#}", err); + self.show_message( + lsp_types::MessageType::Error, + format!("rust-analyzer failed to fetch build data: {:#}", err), + ); + return; + } + }; + + if self.workspaces.len() > 0 { + let workspaces = (*self.workspaces).clone(); + // Force workspace reload + self.workspaces = Arc::new(vec![]); + self.switch_workspaces(workspaces.into_iter().map(|it| Ok(it)).collect()); + } + } + pub(crate) fn fetch_workspaces_request(&mut self) { self.fetch_workspaces_queue.request_op() } @@ -189,7 +246,7 @@ impl GlobalState { let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions { watchers: workspaces .iter() - .flat_map(ProjectWorkspace::to_roots) + .flat_map(|it| it.to_roots(self.build_data_result.as_ref())) .filter(|it| it.is_member) .flat_map(|root| { root.include.into_iter().map(|it| format!("{}/**/*.rs", it.display())) @@ -215,7 +272,11 @@ impl GlobalState { let mut change = Change::new(); let files_config = self.config.files(); - let project_folders = ProjectFolders::new(&workspaces, &files_config.exclude); + let project_folders = ProjectFolders::new( + &workspaces, + &files_config.exclude, + self.build_data_result.as_ref(), + ); self.proc_macro_client = match self.config.proc_macro_srv() { None => None, @@ -257,13 +318,25 @@ impl GlobalState { res }; for ws in workspaces.iter() { - crate_graph.extend(ws.to_crate_graph(self.proc_macro_client.as_ref(), &mut load)); + crate_graph.extend(ws.to_crate_graph( + self.build_data_result.as_ref(), + self.proc_macro_client.as_ref(), + &mut load, + )); } crate_graph }; change.set_crate_graph(crate_graph); + if self.config.load_out_dirs_from_check() && self.build_data_result.is_none() { + let mut loader = BuildDataCollector::default(); + for ws in &workspaces { + ws.collect_build_data_configs(&mut loader); + } + self.fetch_build_data_request(loader) + } + self.source_root_config = project_folders.source_root_config; self.workspaces = Arc::new(workspaces); @@ -323,12 +396,13 @@ impl ProjectFolders { pub(crate) fn new( workspaces: &[ProjectWorkspace], global_excludes: &[AbsPathBuf], + build_data: Option<&BuildDataResult>, ) -> ProjectFolders { let mut res = ProjectFolders::default(); let mut fsc = FileSetConfig::builder(); let mut local_filesets = vec![]; - for root in workspaces.iter().flat_map(|it| it.to_roots()) { + for root in workspaces.iter().flat_map(|it| it.to_roots(build_data)) { let file_set_roots: Vec = root.include.iter().cloned().map(VfsPath::from).collect(); diff --git a/crates/rust-analyzer/tests/rust-analyzer/main.rs b/crates/rust-analyzer/tests/rust-analyzer/main.rs index 80bde29b9e68..fc853fc1aa09 100644 --- a/crates/rust-analyzer/tests/rust-analyzer/main.rs +++ b/crates/rust-analyzer/tests/rust-analyzer/main.rs @@ -577,7 +577,7 @@ fn main() { "cargo": { "loadOutDirsFromCheck": true } })) .server() - .wait_until_workspace_is_loaded(); + .wait_until_workspace_and_build_data_are_loaded(); let res = server.send_request::(HoverParams { text_document_position_params: TextDocumentPositionParams::new( @@ -726,7 +726,7 @@ pub fn foo(_input: TokenStream) -> TokenStream { .root("foo") .root("bar") .server() - .wait_until_workspace_is_loaded(); + .wait_until_workspace_and_build_data_are_loaded(); let res = server.send_request::(HoverParams { text_document_position_params: TextDocumentPositionParams::new( diff --git a/crates/rust-analyzer/tests/rust-analyzer/support.rs b/crates/rust-analyzer/tests/rust-analyzer/support.rs index 2658ee1859c7..25898c95683d 100644 --- a/crates/rust-analyzer/tests/rust-analyzer/support.rs +++ b/crates/rust-analyzer/tests/rust-analyzer/support.rs @@ -233,6 +233,70 @@ impl Server { }); self } + + pub(crate) fn wait_until_workspace_and_build_data_are_loaded(self) -> Server { + // The number of 'roots scanned' end events are depended on thread order, + // We reset it after build_data fetched. It is because `fetch_build_data` will + // trigger `switch_workspace`, and it will trigger "roots scanned" event. + let is_end_build_data = Cell::new(false); + let is_end_roots_scan = Cell::new(false); + let roots_scan_in_progress = Cell::new(0); + let skip_next_roots_scan = Cell::new(false); + + // let's wait for prime cache finished, to make sure no other thread is runnning + let is_index_end = Cell::new(false); + + self.wait_for_message_cond(1, &|msg: &Message| match msg { + Message::Notification(n) if n.method == "$/progress" => { + match n.clone().extract::("$/progress").unwrap() { + ProgressParams { + token: lsp_types::ProgressToken::String(ref token), + value: ProgressParamsValue::WorkDone(WorkDoneProgress::Begin(_)), + } => { + match token.as_str() { + "rustAnalyzer/roots scanned" => { + roots_scan_in_progress.set(roots_scan_in_progress.get() + 1) + } + "rustAnalyzer/indexing" => is_index_end.set(false), + _ => (), + } + false + } + ProgressParams { + token: lsp_types::ProgressToken::String(ref token), + value: ProgressParamsValue::WorkDone(WorkDoneProgress::End(_)), + } => { + match token.as_str() { + "rustAnalyzer/loading" => { + is_end_build_data.set(true); + is_end_roots_scan.set(false); + if roots_scan_in_progress.get() > 0 { + skip_next_roots_scan.set(true); + } + } + "rustAnalyzer/roots scanned" => { + roots_scan_in_progress.set(roots_scan_in_progress.get() - 1); + if !skip_next_roots_scan.replace(false) + && roots_scan_in_progress.get() == 0 + { + is_end_roots_scan.set(true); + is_index_end.set(false); + } + } + "rustAnalyzer/indexing" => { + is_index_end.set(true); + } + _ => (), + } + is_end_build_data.get() && is_end_build_data.get() && is_index_end.get() + } + _ => false, + } + } + _ => false, + }); + self + } fn wait_for_message_cond(&self, n: usize, cond: &dyn Fn(&Message) -> bool) { let mut total = 0; for msg in self.messages.borrow().iter() {