]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/cli/load_cargo.rs
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer
[rust.git] / crates / rust-analyzer / src / cli / load_cargo.rs
1 //! Loads a Cargo project into a static instance of analysis, without support
2 //! for incorporating changes.
3
4 use std::path::{Path, PathBuf};
5
6 use anyhow::Result;
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,
12 };
13 use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
14 use rustc_hash::{FxHashMap, FxHashSet};
15
16 use crate::vfs_glob::RustPackageFilterBuilder;
17
18 fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId {
19     FileId(f.0)
20 }
21 fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId {
22     SourceRootId(r.0)
23 }
24
25 pub(crate) fn load_cargo(
26     root: &Path,
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(
33         root,
34         &CargoConfig { load_out_dirs_from_check, ..Default::default() },
35         true,
36     )?;
37
38     let mut extern_dirs = FxHashSet::default();
39     extern_dirs.extend(ws.out_dirs());
40
41     let mut project_roots = ws.to_roots();
42     project_roots.extend(extern_dirs.iter().cloned().map(PackageRoot::new_non_member));
43
44     let (sender, receiver) = unbounded();
45     let sender = Box::new(move |t| sender.send(t).unwrap());
46     let (mut vfs, roots) = Vfs::new(
47         project_roots
48             .iter()
49             .map(|pkg_root| {
50                 RootEntry::new(
51                     pkg_root.path().to_owned(),
52                     RustPackageFilterBuilder::default()
53                         .set_member(pkg_root.is_member())
54                         .into_vfs_filter(),
55                 )
56             })
57             .collect(),
58         sender,
59         Watch(false),
60     );
61
62     let source_roots = roots
63         .into_iter()
64         .map(|vfs_root| {
65             let source_root_id = vfs_root_to_id(vfs_root);
66             let project_root = project_roots
67                 .iter()
68                 .find(|it| it.path() == vfs.root2path(vfs_root))
69                 .unwrap()
70                 .clone();
71             (source_root_id, project_root)
72         })
73         .collect::<FxHashMap<_, _>>();
74
75     let proc_macro_client = if !with_proc_macro {
76         ProcMacroClient::dummy()
77     } else {
78         let mut path = std::env::current_exe()?;
79         path.pop();
80         path.push("rust-analyzer");
81         ProcMacroClient::extern_process(&path, &["proc-macro"]).unwrap()
82     };
83     let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs, &proc_macro_client);
84     Ok((host, source_roots))
85 }
86
87 pub(crate) fn load(
88     source_roots: &FxHashMap<SourceRootId, PackageRoot>,
89     ws: ProjectWorkspace,
90     vfs: &mut Vfs,
91     receiver: Receiver<VfsTask>,
92     extern_dirs: FxHashSet<PathBuf>,
93     proc_macro_client: &ProcMacroClient,
94 ) -> AnalysisHost {
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();
98
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() {
106             match change {
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();
110                     log::debug!(
111                         "loaded source root {:?} with path {:?}",
112                         source_root_id,
113                         vfs.root2path(root)
114                     );
115                     analysis_change.add_root(source_root_id, is_local);
116                     analysis_change.set_debug_root_path(
117                         source_root_id,
118                         source_roots[&source_root_id].path().display().to_string(),
119                     );
120
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));
124                     }
125
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);
131                     }
132                     roots_loaded.insert(source_root_id);
133                     if roots_loaded.len() == vfs.n_roots() {
134                         done = true;
135                     }
136                 }
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);
141                 }
142                 VfsChange::RemoveFile { .. } | VfsChange::ChangeFile { .. } => {
143                     // We just need the first scan, so just ignore these
144                 }
145             }
146         }
147         if done {
148             break;
149         }
150     }
151
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());
157         opts
158     };
159
160     let crate_graph = ws.to_crate_graph(
161         &default_cfg_options,
162         &extern_source_roots,
163         proc_macro_client,
164         &mut |path: &Path| {
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)
170         },
171     );
172     log::debug!("crate graph: {:?}", crate_graph);
173     analysis_change.set_crate_graph(crate_graph);
174
175     host.apply_change(analysis_change);
176     host
177 }
178
179 #[cfg(test)]
180 mod tests {
181     use super::*;
182
183     use hir::Crate;
184
185     #[test]
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);
192     }
193 }