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 ) -> Result<(AnalysisHost, FxHashMap<SourceRootId, PackageRoot>)> {
29 let root = std::env::current_dir()?.join(root);
30 let root = ProjectRoot::discover_single(&root)?;
31 let ws = ProjectWorkspace::load(
33 &CargoConfig { load_out_dirs_from_check, ..Default::default() },
37 let mut extern_dirs = FxHashSet::default();
38 extern_dirs.extend(ws.out_dirs());
40 let mut project_roots = ws.to_roots();
41 project_roots.extend(extern_dirs.iter().cloned().map(PackageRoot::new_non_member));
43 let (sender, receiver) = unbounded();
44 let sender = Box::new(move |t| sender.send(t).unwrap());
45 let (mut vfs, roots) = Vfs::new(
50 pkg_root.path().to_owned(),
51 RustPackageFilterBuilder::default()
52 .set_member(pkg_root.is_member())
61 let source_roots = roots
64 let source_root_id = vfs_root_to_id(vfs_root);
65 let project_root = project_roots
67 .find(|it| it.path() == vfs.root2path(vfs_root))
70 (source_root_id, project_root)
72 .collect::<FxHashMap<_, _>>();
74 let proc_macro_client = ProcMacroClient::dummy();
75 let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs, &proc_macro_client);
76 Ok((host, source_roots))
80 source_roots: &FxHashMap<SourceRootId, PackageRoot>,
83 receiver: Receiver<VfsTask>,
84 extern_dirs: FxHashSet<PathBuf>,
85 proc_macro_client: &ProcMacroClient,
87 let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
88 let mut host = AnalysisHost::new(lru_cap);
89 let mut analysis_change = AnalysisChange::new();
91 // wait until Vfs has loaded all roots
92 let mut roots_loaded = FxHashSet::default();
93 let mut extern_source_roots = FxHashMap::default();
94 for task in receiver {
95 vfs.handle_task(task);
97 for change in vfs.commit_changes() {
99 VfsChange::AddRoot { root, files } => {
100 let source_root_id = vfs_root_to_id(root);
101 let is_local = source_roots[&source_root_id].is_member();
103 "loaded source root {:?} with path {:?}",
107 analysis_change.add_root(source_root_id, is_local);
108 analysis_change.set_debug_root_path(
110 source_roots[&source_root_id].path().display().to_string(),
113 let vfs_root_path = vfs.root2path(root);
114 if extern_dirs.contains(&vfs_root_path) {
115 extern_source_roots.insert(vfs_root_path, ExternSourceId(root.0));
118 let mut file_map = FxHashMap::default();
119 for (vfs_file, path, text) in files {
120 let file_id = vfs_file_to_id(vfs_file);
121 analysis_change.add_file(source_root_id, file_id, path.clone(), text);
122 file_map.insert(path, file_id);
124 roots_loaded.insert(source_root_id);
125 if roots_loaded.len() == vfs.n_roots() {
129 VfsChange::AddFile { root, file, path, text } => {
130 let source_root_id = vfs_root_to_id(root);
131 let file_id = vfs_file_to_id(file);
132 analysis_change.add_file(source_root_id, file_id, path, text);
134 VfsChange::RemoveFile { .. } | VfsChange::ChangeFile { .. } => {
135 // We just need the first scan, so just ignore these
144 // FIXME: cfg options?
145 let default_cfg_options = {
146 let mut opts = get_rustc_cfg_options();
147 opts.insert_atom("test".into());
148 opts.insert_atom("debug_assertion".into());
152 let crate_graph = ws.to_crate_graph(
153 &default_cfg_options,
154 &extern_source_roots,
157 // Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs
158 let path = path.canonicalize().ok()?;
159 let vfs_file = vfs.load(&path);
160 log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
161 vfs_file.map(vfs_file_to_id)
164 log::debug!("crate graph: {:?}", crate_graph);
165 analysis_change.set_crate_graph(crate_graph);
167 host.apply_change(analysis_change);
178 fn test_loading_rust_analyzer() {
179 let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
180 let (host, _roots) = load_cargo(path, false).unwrap();
181 let n_crates = Crate::all(host.raw_database()).len();
182 // RA has quite a few crates, but the exact count doesn't matter
183 assert!(n_crates > 20);