1 //! Loads a Cargo project into a static instance of analysis, without support
2 //! for incorporating changes.
4 use std::path::{Path, PathBuf};
7 use crossbeam_channel::{unbounded, Receiver};
8 use ra_db::{ExternSourceId, FileId, SourceRootId};
9 use ra_ide::{AnalysisChange, AnalysisHost};
10 use ra_project_model::{
11 CargoConfig, PackageRoot, ProcMacroClient, ProjectManifest, ProjectWorkspace,
13 use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
14 use rustc_hash::{FxHashMap, FxHashSet};
16 use crate::vfs_glob::RustPackageFilterBuilder;
18 fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId {
21 fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId {
27 load_out_dirs_from_check: bool,
28 with_proc_macro: bool,
29 ) -> Result<(AnalysisHost, FxHashMap<SourceRootId, PackageRoot>)> {
30 let root = std::env::current_dir()?.join(root);
31 let root = ProjectManifest::discover_single(&root)?;
32 let ws = ProjectWorkspace::load(
34 &CargoConfig { load_out_dirs_from_check, ..Default::default() },
38 let mut extern_dirs = FxHashSet::default();
40 let (sender, receiver) = unbounded();
41 let sender = Box::new(move |t| sender.send(t).unwrap());
43 let mut roots = Vec::new();
44 let project_roots = ws.to_roots();
45 for root in &project_roots {
46 roots.push(RootEntry::new(
47 root.path().to_owned(),
48 RustPackageFilterBuilder::default().set_member(root.is_member()).into_vfs_filter(),
51 if let Some(out_dir) = root.out_dir() {
52 extern_dirs.insert(out_dir.to_path_buf());
53 roots.push(RootEntry::new(
55 RustPackageFilterBuilder::default().set_member(root.is_member()).into_vfs_filter(),
60 let (mut vfs, roots) = Vfs::new(roots, sender, Watch(false));
62 let source_roots = roots
65 let source_root_id = vfs_root_to_id(vfs_root);
66 let project_root = project_roots
68 .find(|it| it.path() == vfs.root2path(vfs_root))
71 (source_root_id, project_root)
73 .collect::<FxHashMap<_, _>>();
75 let proc_macro_client = if !with_proc_macro {
76 ProcMacroClient::dummy()
78 let path = std::env::current_exe()?;
79 ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap()
81 let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs, &proc_macro_client);
82 Ok((host, source_roots))
86 source_roots: &FxHashMap<SourceRootId, PackageRoot>,
89 receiver: Receiver<VfsTask>,
90 extern_dirs: FxHashSet<PathBuf>,
91 proc_macro_client: &ProcMacroClient,
93 let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
94 let mut host = AnalysisHost::new(lru_cap);
95 let mut analysis_change = AnalysisChange::new();
97 // wait until Vfs has loaded all roots
98 let mut roots_loaded = FxHashSet::default();
99 let mut extern_source_roots = FxHashMap::default();
100 for task in receiver {
101 vfs.handle_task(task);
102 let mut done = false;
103 for change in vfs.commit_changes() {
105 VfsChange::AddRoot { root, files } => {
106 let source_root_id = vfs_root_to_id(root);
107 let is_local = source_roots[&source_root_id].is_member();
109 "loaded source root {:?} with path {:?}",
113 analysis_change.add_root(source_root_id, is_local);
115 let vfs_root_path = vfs.root2path(root);
116 if extern_dirs.contains(&vfs_root_path) {
117 extern_source_roots.insert(vfs_root_path, ExternSourceId(root.0));
120 let mut file_map = FxHashMap::default();
121 for (vfs_file, path, text) in files {
122 let file_id = vfs_file_to_id(vfs_file);
123 analysis_change.add_file(source_root_id, file_id, path.clone(), text);
124 file_map.insert(path, file_id);
126 roots_loaded.insert(source_root_id);
127 if roots_loaded.len() == vfs.n_roots() {
131 VfsChange::AddFile { root, file, path, text } => {
132 let source_root_id = vfs_root_to_id(root);
133 let file_id = vfs_file_to_id(file);
134 analysis_change.add_file(source_root_id, file_id, path, text);
136 VfsChange::RemoveFile { .. } | VfsChange::ChangeFile { .. } => {
137 // We just need the first scan, so just ignore these
147 ws.to_crate_graph(None, &extern_source_roots, proc_macro_client, &mut |path: &Path| {
148 // Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs
149 let path = path.canonicalize().ok()?;
150 let vfs_file = vfs.load(&path);
151 log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
152 vfs_file.map(vfs_file_to_id)
154 log::debug!("crate graph: {:?}", crate_graph);
155 analysis_change.set_crate_graph(crate_graph);
157 host.apply_change(analysis_change);
168 fn test_loading_rust_analyzer() {
169 let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
170 let (host, _roots) = load_cargo(path, false, false).unwrap();
171 let n_crates = Crate::all(host.raw_database()).len();
172 // RA has quite a few crates, but the exact count doesn't matter
173 assert!(n_crates > 20);