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, ProjectManifest,
14 use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
15 use rustc_hash::{FxHashMap, FxHashSet};
17 use crate::vfs_glob::RustPackageFilterBuilder;
19 fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId {
22 fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId {
28 load_out_dirs_from_check: bool,
29 with_proc_macro: bool,
30 ) -> Result<(AnalysisHost, FxHashMap<SourceRootId, PackageRoot>)> {
31 let root = std::env::current_dir()?.join(root);
32 let root = ProjectManifest::discover_single(&root)?;
33 let ws = ProjectWorkspace::load(
35 &CargoConfig { load_out_dirs_from_check, ..Default::default() },
39 let mut extern_dirs = FxHashSet::default();
40 extern_dirs.extend(ws.out_dirs());
42 let mut project_roots = ws.to_roots();
43 project_roots.extend(extern_dirs.iter().cloned().map(PackageRoot::new_non_member));
45 let (sender, receiver) = unbounded();
46 let sender = Box::new(move |t| sender.send(t).unwrap());
47 let (mut vfs, roots) = Vfs::new(
52 pkg_root.path().to_owned(),
53 RustPackageFilterBuilder::default()
54 .set_member(pkg_root.is_member())
63 let source_roots = roots
66 let source_root_id = vfs_root_to_id(vfs_root);
67 let project_root = project_roots
69 .find(|it| it.path() == vfs.root2path(vfs_root))
72 (source_root_id, project_root)
74 .collect::<FxHashMap<_, _>>();
76 let proc_macro_client = if !with_proc_macro {
77 ProcMacroClient::dummy()
79 let path = std::env::current_exe()?;
80 ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap()
82 let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs, &proc_macro_client);
83 Ok((host, source_roots))
87 source_roots: &FxHashMap<SourceRootId, PackageRoot>,
90 receiver: Receiver<VfsTask>,
91 extern_dirs: FxHashSet<PathBuf>,
92 proc_macro_client: &ProcMacroClient,
94 let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
95 let mut host = AnalysisHost::new(lru_cap);
96 let mut analysis_change = AnalysisChange::new();
98 // wait until Vfs has loaded all roots
99 let mut roots_loaded = FxHashSet::default();
100 let mut extern_source_roots = FxHashMap::default();
101 for task in receiver {
102 vfs.handle_task(task);
103 let mut done = false;
104 for change in vfs.commit_changes() {
106 VfsChange::AddRoot { root, files } => {
107 let source_root_id = vfs_root_to_id(root);
108 let is_local = source_roots[&source_root_id].is_member();
110 "loaded source root {:?} with path {:?}",
114 analysis_change.add_root(source_root_id, is_local);
115 analysis_change.set_debug_root_path(
117 source_roots[&source_root_id].path().display().to_string(),
120 let vfs_root_path = vfs.root2path(root);
121 if extern_dirs.contains(&vfs_root_path) {
122 extern_source_roots.insert(vfs_root_path, ExternSourceId(root.0));
125 let mut file_map = FxHashMap::default();
126 for (vfs_file, path, text) in files {
127 let file_id = vfs_file_to_id(vfs_file);
128 analysis_change.add_file(source_root_id, file_id, path.clone(), text);
129 file_map.insert(path, file_id);
131 roots_loaded.insert(source_root_id);
132 if roots_loaded.len() == vfs.n_roots() {
136 VfsChange::AddFile { root, file, path, text } => {
137 let source_root_id = vfs_root_to_id(root);
138 let file_id = vfs_file_to_id(file);
139 analysis_change.add_file(source_root_id, file_id, path, text);
141 VfsChange::RemoveFile { .. } | VfsChange::ChangeFile { .. } => {
142 // We just need the first scan, so just ignore these
151 // FIXME: cfg options?
152 let default_cfg_options = {
153 let mut opts = get_rustc_cfg_options(None);
154 opts.insert_atom("test".into());
155 opts.insert_atom("debug_assertion".into());
159 let crate_graph = ws.to_crate_graph(
160 &default_cfg_options,
161 &extern_source_roots,
164 // Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs
165 let path = path.canonicalize().ok()?;
166 let vfs_file = vfs.load(&path);
167 log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
168 vfs_file.map(vfs_file_to_id)
171 log::debug!("crate graph: {:?}", crate_graph);
172 analysis_change.set_crate_graph(crate_graph);
174 host.apply_change(analysis_change);
185 fn test_loading_rust_analyzer() {
186 let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
187 let (host, _roots) = load_cargo(path, false, false).unwrap();
188 let n_crates = Crate::all(host.raw_database()).len();
189 // RA has quite a few crates, but the exact count doesn't matter
190 assert!(n_crates > 20);