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 get_rustc_cfg_options, CargoConfig, PackageRoot, ProcMacroClient, ProjectRoot, 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 {
25 pub(crate) fn load_cargo(
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 = ProjectRoot::discover_single(&root)?;
32 let ws = ProjectWorkspace::load(
34 &CargoConfig { load_out_dirs_from_check, ..Default::default() },
38 let mut extern_dirs = FxHashSet::default();
39 extern_dirs.extend(ws.out_dirs());
41 let mut project_roots = ws.to_roots();
42 project_roots.extend(extern_dirs.iter().cloned().map(PackageRoot::new_non_member));
44 let (sender, receiver) = unbounded();
45 let sender = Box::new(move |t| sender.send(t).unwrap());
46 let (mut vfs, roots) = Vfs::new(
51 pkg_root.path().to_owned(),
52 RustPackageFilterBuilder::default()
53 .set_member(pkg_root.is_member())
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 mut path = std::env::current_exe()?;
80 path.push("rust-analyzer");
81 ProcMacroClient::extern_process(&path, &["proc-macro"]).unwrap()
83 let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs, &proc_macro_client);
84 Ok((host, source_roots))
88 source_roots: &FxHashMap<SourceRootId, PackageRoot>,
91 receiver: Receiver<VfsTask>,
92 extern_dirs: FxHashSet<PathBuf>,
93 proc_macro_client: &ProcMacroClient,
95 let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
96 let mut host = AnalysisHost::new(lru_cap);
97 let mut analysis_change = AnalysisChange::new();
99 // wait until Vfs has loaded all roots
100 let mut roots_loaded = FxHashSet::default();
101 let mut extern_source_roots = FxHashMap::default();
102 for task in receiver {
103 vfs.handle_task(task);
104 let mut done = false;
105 for change in vfs.commit_changes() {
107 VfsChange::AddRoot { root, files } => {
108 let source_root_id = vfs_root_to_id(root);
109 let is_local = source_roots[&source_root_id].is_member();
111 "loaded source root {:?} with path {:?}",
115 analysis_change.add_root(source_root_id, is_local);
116 analysis_change.set_debug_root_path(
118 source_roots[&source_root_id].path().display().to_string(),
121 let vfs_root_path = vfs.root2path(root);
122 if extern_dirs.contains(&vfs_root_path) {
123 extern_source_roots.insert(vfs_root_path, ExternSourceId(root.0));
126 let mut file_map = FxHashMap::default();
127 for (vfs_file, path, text) in files {
128 let file_id = vfs_file_to_id(vfs_file);
129 analysis_change.add_file(source_root_id, file_id, path.clone(), text);
130 file_map.insert(path, file_id);
132 roots_loaded.insert(source_root_id);
133 if roots_loaded.len() == vfs.n_roots() {
137 VfsChange::AddFile { root, file, path, text } => {
138 let source_root_id = vfs_root_to_id(root);
139 let file_id = vfs_file_to_id(file);
140 analysis_change.add_file(source_root_id, file_id, path, text);
142 VfsChange::RemoveFile { .. } | VfsChange::ChangeFile { .. } => {
143 // We just need the first scan, so just ignore these
152 // FIXME: cfg options?
153 let default_cfg_options = {
154 let mut opts = get_rustc_cfg_options();
155 opts.insert_atom("test".into());
156 opts.insert_atom("debug_assertion".into());
160 let crate_graph = ws.to_crate_graph(
161 &default_cfg_options,
162 &extern_source_roots,
165 // Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs
166 let path = path.canonicalize().ok()?;
167 let vfs_file = vfs.load(&path);
168 log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
169 vfs_file.map(vfs_file_to_id)
172 log::debug!("crate graph: {:?}", crate_graph);
173 analysis_change.set_crate_graph(crate_graph);
175 host.apply_change(analysis_change);
186 fn test_loading_rust_analyzer() {
187 let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
188 let (host, _roots) = load_cargo(path, false, false).unwrap();
189 let n_crates = Crate::all(host.raw_database()).len();
190 // RA has quite a few crates, but the exact count doesn't matter
191 assert!(n_crates > 20);