rust/crates/rust-analyzer/src/cli/load_cargo.rs

166 lines
5.6 KiB
Rust
Raw Normal View History

2020-02-18 11:11:32 +00:00
//! Loads a Cargo project into a static instance of analysis, without support
//! for incorporating changes.
2020-06-24 13:52:07 +00:00
use std::{path::Path, sync::Arc};
2019-02-05 21:54:17 +00:00
2020-02-18 11:11:32 +00:00
use anyhow::Result;
2019-08-25 10:04:56 +00:00
use crossbeam_channel::{unbounded, Receiver};
use hir::db::DefDatabase;
use ide::{AnalysisHost, Change};
2020-10-24 08:39:57 +00:00
use ide_db::base_db::CrateGraph;
use proc_macro_api::ProcMacroClient;
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, WorkspaceBuildScripts};
2020-07-08 16:22:57 +00:00
use vfs::{loader::Handle, AbsPath, AbsPathBuf};
2020-02-17 18:07:30 +00:00
use crate::reload::{load_proc_macro, ProjectFolders, SourceRootConfig};
2019-02-09 12:06:12 +00:00
// Note: Since this type is used by external tools that use rust-analyzer as a library
// what otherwise would be `pub(crate)` has to be `pub` here instead.
pub struct LoadCargoConfig {
pub load_out_dirs_from_check: bool,
pub with_proc_macro: bool,
pub prefill_caches: bool,
}
// Note: Since this function is used by external tools that use rust-analyzer as a library
// what otherwise would be `pub(crate)` has to be `pub` here instead.
pub fn load_workspace_at(
root: &Path,
cargo_config: &CargoConfig,
load_config: &LoadCargoConfig,
progress: &dyn Fn(String),
) -> Result<(AnalysisHost, vfs::Vfs, Option<ProcMacroClient>)> {
let root = AbsPathBuf::assert(std::env::current_dir()?.join(root));
2020-06-03 10:05:50 +00:00
let root = ProjectManifest::discover_single(&root)?;
let workspace = ProjectWorkspace::load(root, cargo_config, progress)?;
2020-03-16 13:51:44 +00:00
load_workspace(workspace, cargo_config, load_config, progress)
}
// Note: Since this function is used by external tools that use rust-analyzer as a library
// what otherwise would be `pub(crate)` has to be `pub` here instead.
//
// The reason both, `load_workspace_at` and `load_workspace` are `pub` is that some of
// these tools need access to `ProjectWorkspace`, too, which `load_workspace_at` hides.
pub fn load_workspace(
mut ws: ProjectWorkspace,
cargo_config: &CargoConfig,
load_config: &LoadCargoConfig,
progress: &dyn Fn(String),
) -> Result<(AnalysisHost, vfs::Vfs, Option<ProcMacroClient>)> {
2019-08-25 10:04:56 +00:00
let (sender, receiver) = unbounded();
2020-06-11 09:04:09 +00:00
let mut vfs = vfs::Vfs::default();
let mut loader = {
let loader =
2020-06-25 06:59:55 +00:00
vfs_notify::NotifyHandle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
2020-06-11 09:04:09 +00:00
Box::new(loader)
};
2020-03-18 12:56:46 +00:00
let proc_macro_client = if load_config.with_proc_macro {
let path = AbsPathBuf::assert(std::env::current_exe()?);
Some(ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap())
2020-06-11 09:04:09 +00:00
} else {
None
};
2020-06-11 09:04:09 +00:00
ws.set_build_scripts(if load_config.load_out_dirs_from_check {
ws.run_build_scripts(cargo_config, progress)?
} else {
WorkspaceBuildScripts::default()
});
2021-01-28 15:33:02 +00:00
let crate_graph = ws.to_crate_graph(
&mut |path: &AbsPath| load_proc_macro(proc_macro_client.as_ref(), path),
&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)
},
);
2020-06-11 09:04:09 +00:00
let project_folders = ProjectFolders::new(&[ws], &[]);
loader.set_config(vfs::loader::Config {
load: project_folders.load,
watch: vec![],
version: 0,
});
2020-06-11 09:04:09 +00:00
log::debug!("crate graph: {:?}", crate_graph);
let host =
load_crate_graph(crate_graph, project_folders.source_root_config, &mut vfs, &receiver);
2021-06-10 22:35:14 +00:00
if load_config.prefill_caches {
host.analysis().prime_caches(|_| {})?;
}
Ok((host, vfs, proc_macro_client))
2019-06-15 13:29:23 +00:00
}
2019-02-09 12:06:12 +00:00
fn load_crate_graph(
2020-06-11 09:04:09 +00:00
crate_graph: CrateGraph,
source_root_config: SourceRootConfig,
vfs: &mut vfs::Vfs,
receiver: &Receiver<vfs::loader::Message>,
) -> AnalysisHost {
2019-06-15 13:29:23 +00:00
let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
2020-03-10 17:56:15 +00:00
let mut host = AnalysisHost::new(lru_cap);
let mut analysis_change = Change::new();
2019-06-15 13:29:23 +00:00
host.raw_database_mut().set_enable_proc_attr_macros(true);
2019-06-15 13:29:23 +00:00
// wait until Vfs has loaded all roots
for task in receiver {
2020-06-11 09:04:09 +00:00
match task {
vfs::loader::Message::Progress { n_done, n_total, config_version: _ } => {
2020-06-24 14:58:49 +00:00
if n_done == n_total {
2020-06-11 09:04:09 +00:00
break;
}
2020-06-11 09:04:09 +00:00
}
vfs::loader::Message::Loaded { files } => {
for (path, contents) in files {
vfs.set_file_contents(path.into(), contents);
2019-06-15 13:29:23 +00:00
}
2019-02-09 12:06:12 +00:00
}
}
2020-06-11 09:04:09 +00:00
}
let changes = vfs.take_changes();
for file in changes {
if file.exists() {
let contents = vfs.file_contents(file.file_id).to_vec();
if let Ok(text) = String::from_utf8(contents) {
analysis_change.change_file(file.file_id, Some(Arc::new(text)))
}
2019-06-15 13:29:23 +00:00
}
2019-02-09 12:06:12 +00:00
}
2021-06-13 03:54:16 +00:00
let source_roots = source_root_config.partition(vfs);
2020-06-11 09:04:09 +00:00
analysis_change.set_roots(source_roots);
2019-02-09 12:06:12 +00:00
2020-03-16 13:51:44 +00:00
analysis_change.set_crate_graph(crate_graph);
2019-06-15 13:29:23 +00:00
host.apply_change(analysis_change);
host
2019-02-09 12:06:12 +00:00
}
2019-02-10 10:44:53 +00:00
#[cfg(test)]
mod tests {
use super::*;
2020-02-17 16:31:09 +00:00
use hir::Crate;
2019-02-10 10:44:53 +00:00
#[test]
fn test_loading_rust_analyzer() {
2019-04-04 09:57:10 +00:00
let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
let cargo_config = CargoConfig::default();
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: false,
with_proc_macro: false,
prefill_caches: false,
};
let (host, _vfs, _proc_macro) =
load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {}).unwrap();
let n_crates = Crate::all(host.raw_database()).len();
2019-02-10 10:44:53 +00:00
// RA has quite a few crates, but the exact count doesn't matter
2019-02-17 18:05:33 +00:00
assert!(n_crates > 20);
2019-02-10 10:44:53 +00:00
}
}