]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/cli/load_cargo.rs
Unmix error handling when discovering workspaces
[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 ) -> 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(
32         root,
33         &CargoConfig { load_out_dirs_from_check, ..Default::default() },
34         true,
35     )?;
36
37     let mut extern_dirs = FxHashSet::default();
38     extern_dirs.extend(ws.out_dirs());
39
40     let mut project_roots = ws.to_roots();
41     project_roots.extend(extern_dirs.iter().cloned().map(PackageRoot::new_non_member));
42
43     let (sender, receiver) = unbounded();
44     let sender = Box::new(move |t| sender.send(t).unwrap());
45     let (mut vfs, roots) = Vfs::new(
46         project_roots
47             .iter()
48             .map(|pkg_root| {
49                 RootEntry::new(
50                     pkg_root.path().to_owned(),
51                     RustPackageFilterBuilder::default()
52                         .set_member(pkg_root.is_member())
53                         .into_vfs_filter(),
54                 )
55             })
56             .collect(),
57         sender,
58         Watch(false),
59     );
60
61     let source_roots = roots
62         .into_iter()
63         .map(|vfs_root| {
64             let source_root_id = vfs_root_to_id(vfs_root);
65             let project_root = project_roots
66                 .iter()
67                 .find(|it| it.path() == vfs.root2path(vfs_root))
68                 .unwrap()
69                 .clone();
70             (source_root_id, project_root)
71         })
72         .collect::<FxHashMap<_, _>>();
73
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))
77 }
78
79 pub(crate) fn load(
80     source_roots: &FxHashMap<SourceRootId, PackageRoot>,
81     ws: ProjectWorkspace,
82     vfs: &mut Vfs,
83     receiver: Receiver<VfsTask>,
84     extern_dirs: FxHashSet<PathBuf>,
85     proc_macro_client: &ProcMacroClient,
86 ) -> AnalysisHost {
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();
90
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);
96         let mut done = false;
97         for change in vfs.commit_changes() {
98             match change {
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();
102                     log::debug!(
103                         "loaded source root {:?} with path {:?}",
104                         source_root_id,
105                         vfs.root2path(root)
106                     );
107                     analysis_change.add_root(source_root_id, is_local);
108                     analysis_change.set_debug_root_path(
109                         source_root_id,
110                         source_roots[&source_root_id].path().display().to_string(),
111                     );
112
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));
116                     }
117
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);
123                     }
124                     roots_loaded.insert(source_root_id);
125                     if roots_loaded.len() == vfs.n_roots() {
126                         done = true;
127                     }
128                 }
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);
133                 }
134                 VfsChange::RemoveFile { .. } | VfsChange::ChangeFile { .. } => {
135                     // We just need the first scan, so just ignore these
136                 }
137             }
138         }
139         if done {
140             break;
141         }
142     }
143
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());
149         opts
150     };
151
152     let crate_graph = ws.to_crate_graph(
153         &default_cfg_options,
154         &extern_source_roots,
155         proc_macro_client,
156         &mut |path: &Path| {
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)
162         },
163     );
164     log::debug!("crate graph: {:?}", crate_graph);
165     analysis_change.set_crate_graph(crate_graph);
166
167     host.apply_change(analysis_change);
168     host
169 }
170
171 #[cfg(test)]
172 mod tests {
173     use super::*;
174
175     use hir::Crate;
176
177     #[test]
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);
184     }
185 }