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