]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/reload.rs
Rename `is_member` to `is_local`
[rust.git] / crates / rust-analyzer / src / reload.rs
1 //! Project loading & configuration updates
2 use std::{mem, sync::Arc};
3
4 use flycheck::{FlycheckConfig, FlycheckHandle};
5 use hir::db::DefDatabase;
6 use ide::Change;
7 use ide_db::base_db::{
8     CrateGraph, Env, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind,
9     SourceRoot, VfsPath,
10 };
11 use proc_macro_api::{MacroDylib, ProcMacroServer};
12 use project_model::{ProjectWorkspace, WorkspaceBuildScripts};
13 use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
14
15 use crate::{
16     config::{Config, FilesWatcher, LinkedProject},
17     global_state::GlobalState,
18     lsp_ext,
19     main_loop::Task,
20 };
21
22 #[derive(Debug)]
23 pub(crate) enum ProjectWorkspaceProgress {
24     Begin,
25     Report(String),
26     End(Vec<anyhow::Result<ProjectWorkspace>>),
27 }
28
29 #[derive(Debug)]
30 pub(crate) enum BuildDataProgress {
31     Begin,
32     Report(String),
33     End((Arc<Vec<ProjectWorkspace>>, Vec<anyhow::Result<WorkspaceBuildScripts>>)),
34 }
35
36 impl GlobalState {
37     pub(crate) fn is_quiescent(&self) -> bool {
38         !(self.fetch_workspaces_queue.op_in_progress()
39             || self.fetch_build_data_queue.op_in_progress()
40             || self.vfs_progress_config_version < self.vfs_config_version
41             || self.vfs_progress_n_done < self.vfs_progress_n_total)
42     }
43
44     pub(crate) fn update_configuration(&mut self, config: Config) {
45         let _p = profile::span("GlobalState::update_configuration");
46         let old_config = mem::replace(&mut self.config, Arc::new(config));
47         if self.config.lru_capacity() != old_config.lru_capacity() {
48             self.analysis_host.update_lru_capacity(self.config.lru_capacity());
49         }
50         if self.config.linked_projects() != old_config.linked_projects() {
51             self.fetch_workspaces_queue.request_op()
52         } else if self.config.flycheck() != old_config.flycheck() {
53             self.reload_flycheck();
54         }
55
56         // Apply experimental feature flags.
57         self.analysis_host
58             .raw_database_mut()
59             .set_enable_proc_attr_macros(self.config.expand_proc_attr_macros());
60     }
61     pub(crate) fn maybe_refresh(&mut self, changes: &[(AbsPathBuf, ChangeKind)]) {
62         if !changes.iter().any(|(path, kind)| is_interesting(path, *kind)) {
63             return;
64         }
65         tracing::info!(
66             "Requesting workspace reload because of the following changes: {}",
67             itertools::join(
68                 changes
69                     .iter()
70                     .filter(|(path, kind)| is_interesting(path, *kind))
71                     .map(|(path, kind)| format!("{}: {:?}", path.display(), kind)),
72                 ", "
73             )
74         );
75         self.fetch_workspaces_queue.request_op();
76
77         fn is_interesting(path: &AbsPath, change_kind: ChangeKind) -> bool {
78             const IMPLICIT_TARGET_FILES: &[&str] = &["build.rs", "src/main.rs", "src/lib.rs"];
79             const IMPLICIT_TARGET_DIRS: &[&str] = &["src/bin", "examples", "tests", "benches"];
80             let file_name = path.file_name().unwrap_or_default();
81
82             if file_name == "Cargo.toml" || file_name == "Cargo.lock" {
83                 return true;
84             }
85             if change_kind == ChangeKind::Modify {
86                 return false;
87             }
88             if path.extension().unwrap_or_default() != "rs" {
89                 return false;
90             }
91             if IMPLICIT_TARGET_FILES.iter().any(|it| path.as_ref().ends_with(it)) {
92                 return true;
93             }
94             let parent = match path.parent() {
95                 Some(it) => it,
96                 None => return false,
97             };
98             if IMPLICIT_TARGET_DIRS.iter().any(|it| parent.as_ref().ends_with(it)) {
99                 return true;
100             }
101             if file_name == "main.rs" {
102                 let grand_parent = match parent.parent() {
103                     Some(it) => it,
104                     None => return false,
105                 };
106                 if IMPLICIT_TARGET_DIRS.iter().any(|it| grand_parent.as_ref().ends_with(it)) {
107                     return true;
108                 }
109             }
110             false
111         }
112     }
113
114     pub(crate) fn current_status(&self) -> lsp_ext::ServerStatusParams {
115         let mut status = lsp_ext::ServerStatusParams {
116             health: lsp_ext::Health::Ok,
117             quiescent: self.is_quiescent(),
118             message: None,
119         };
120
121         if let Some(error) = self.fetch_build_data_error() {
122             status.health = lsp_ext::Health::Warning;
123             status.message = Some(error)
124         }
125         if !self.config.cargo_autoreload()
126             && self.is_quiescent()
127             && self.fetch_workspaces_queue.op_requested()
128         {
129             status.health = lsp_ext::Health::Warning;
130             status.message = Some("Workspace reload required".to_string())
131         }
132
133         if let Some(error) = self.fetch_workspace_error() {
134             status.health = lsp_ext::Health::Error;
135             status.message = Some(error)
136         }
137         status
138     }
139
140     pub(crate) fn fetch_workspaces(&mut self) {
141         tracing::info!("will fetch workspaces");
142
143         self.task_pool.handle.spawn_with_sender({
144             let linked_projects = self.config.linked_projects();
145             let detached_files = self.config.detached_files().to_vec();
146             let cargo_config = self.config.cargo();
147
148             move |sender| {
149                 let progress = {
150                     let sender = sender.clone();
151                     move |msg| {
152                         sender
153                             .send(Task::FetchWorkspace(ProjectWorkspaceProgress::Report(msg)))
154                             .unwrap()
155                     }
156                 };
157
158                 sender.send(Task::FetchWorkspace(ProjectWorkspaceProgress::Begin)).unwrap();
159
160                 let mut workspaces = linked_projects
161                     .iter()
162                     .map(|project| match project {
163                         LinkedProject::ProjectManifest(manifest) => {
164                             project_model::ProjectWorkspace::load(
165                                 manifest.clone(),
166                                 &cargo_config,
167                                 &progress,
168                             )
169                         }
170                         LinkedProject::InlineJsonProject(it) => {
171                             project_model::ProjectWorkspace::load_inline(
172                                 it.clone(),
173                                 cargo_config.target.as_deref(),
174                             )
175                         }
176                     })
177                     .collect::<Vec<_>>();
178
179                 if !detached_files.is_empty() {
180                     workspaces
181                         .push(project_model::ProjectWorkspace::load_detached_files(detached_files));
182                 }
183
184                 tracing::info!("did fetch workspaces {:?}", workspaces);
185                 sender
186                     .send(Task::FetchWorkspace(ProjectWorkspaceProgress::End(workspaces)))
187                     .unwrap();
188             }
189         });
190     }
191
192     pub(crate) fn fetch_build_data(&mut self) {
193         let workspaces = Arc::clone(&self.workspaces);
194         let config = self.config.cargo();
195         self.task_pool.handle.spawn_with_sender(move |sender| {
196             sender.send(Task::FetchBuildData(BuildDataProgress::Begin)).unwrap();
197
198             let progress = {
199                 let sender = sender.clone();
200                 move |msg| {
201                     sender.send(Task::FetchBuildData(BuildDataProgress::Report(msg))).unwrap()
202                 }
203             };
204             let mut res = Vec::new();
205             for ws in workspaces.iter() {
206                 res.push(ws.run_build_scripts(&config, &progress));
207             }
208             sender.send(Task::FetchBuildData(BuildDataProgress::End((workspaces, res)))).unwrap();
209         });
210     }
211
212     pub(crate) fn switch_workspaces(&mut self) {
213         let _p = profile::span("GlobalState::switch_workspaces");
214         tracing::info!("will switch workspaces");
215
216         if let Some(error_message) = self.fetch_workspace_error() {
217             tracing::error!("failed to switch workspaces: {}", error_message);
218             if !self.workspaces.is_empty() {
219                 // It only makes sense to switch to a partially broken workspace
220                 // if we don't have any workspace at all yet.
221                 return;
222             }
223         }
224
225         if let Some(error_message) = self.fetch_build_data_error() {
226             tracing::error!("failed to switch build data: {}", error_message);
227         }
228
229         let workspaces = self
230             .fetch_workspaces_queue
231             .last_op_result()
232             .iter()
233             .filter_map(|res| res.as_ref().ok().cloned())
234             .collect::<Vec<_>>();
235
236         fn eq_ignore_build_data<'a>(
237             left: &'a ProjectWorkspace,
238             right: &'a ProjectWorkspace,
239         ) -> bool {
240             let key = |p: &'a ProjectWorkspace| match p {
241                 ProjectWorkspace::Cargo {
242                     cargo,
243                     sysroot,
244                     rustc,
245                     rustc_cfg,
246                     cfg_overrides,
247
248                     build_scripts: _,
249                 } => Some((cargo, sysroot, rustc, rustc_cfg, cfg_overrides)),
250                 _ => None,
251             };
252             match (key(left), key(right)) {
253                 (Some(lk), Some(rk)) => lk == rk,
254                 _ => left == right,
255             }
256         }
257
258         let same_workspaces = workspaces.len() == self.workspaces.len()
259             && workspaces
260                 .iter()
261                 .zip(self.workspaces.iter())
262                 .all(|(l, r)| eq_ignore_build_data(l, r));
263
264         if same_workspaces {
265             let (workspaces, build_scripts) = self.fetch_build_data_queue.last_op_result();
266             if Arc::ptr_eq(&workspaces, &self.workspaces) {
267                 let workspaces = workspaces
268                     .iter()
269                     .cloned()
270                     .zip(build_scripts)
271                     .map(|(mut ws, bs)| {
272                         ws.set_build_scripts(bs.as_ref().ok().cloned().unwrap_or_default());
273                         ws
274                     })
275                     .collect::<Vec<_>>();
276
277                 // Workspaces are the same, but we've updated build data.
278                 self.workspaces = Arc::new(workspaces);
279             } else {
280                 // Current build scripts do not match the version of the active
281                 // workspace, so there's nothing for us to update.
282                 return;
283             }
284         } else {
285             // Here, we completely changed the workspace (Cargo.toml edit), so
286             // we don't care about build-script results, they are stale.
287             self.workspaces = Arc::new(workspaces)
288         }
289
290         if let FilesWatcher::Client = self.config.files().watcher {
291             if self.config.did_change_watched_files_dynamic_registration() {
292                 let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
293                     watchers: self
294                         .workspaces
295                         .iter()
296                         .flat_map(|ws| ws.to_roots())
297                         .filter(|it| it.is_local)
298                         .flat_map(|root| {
299                             root.include.into_iter().flat_map(|it| {
300                                 [
301                                     format!("{}/**/*.rs", it.display()),
302                                     format!("{}/**/Cargo.toml", it.display()),
303                                     format!("{}/**/Cargo.lock", it.display()),
304                                 ]
305                             })
306                         })
307                         .map(|glob_pattern| lsp_types::FileSystemWatcher {
308                             glob_pattern,
309                             kind: None,
310                         })
311                         .collect(),
312                 };
313                 let registration = lsp_types::Registration {
314                     id: "workspace/didChangeWatchedFiles".to_string(),
315                     method: "workspace/didChangeWatchedFiles".to_string(),
316                     register_options: Some(serde_json::to_value(registration_options).unwrap()),
317                 };
318                 self.send_request::<lsp_types::request::RegisterCapability>(
319                     lsp_types::RegistrationParams { registrations: vec![registration] },
320                     |_, _| (),
321                 );
322             }
323         }
324
325         let mut change = Change::new();
326
327         let files_config = self.config.files();
328         let project_folders = ProjectFolders::new(&self.workspaces, &files_config.exclude);
329
330         if self.proc_macro_client.is_none() {
331             self.proc_macro_client = match self.config.proc_macro_srv() {
332                 None => None,
333                 Some((path, args)) => match ProcMacroServer::spawn(path.clone(), args) {
334                     Ok(it) => Some(it),
335                     Err(err) => {
336                         tracing::error!(
337                             "Failed to run proc_macro_srv from path {}, error: {:?}",
338                             path.display(),
339                             err
340                         );
341                         None
342                     }
343                 },
344             };
345         }
346
347         let watch = match files_config.watcher {
348             FilesWatcher::Client => vec![],
349             FilesWatcher::Notify => project_folders.watch,
350         };
351         self.vfs_config_version += 1;
352         self.loader.handle.set_config(vfs::loader::Config {
353             load: project_folders.load,
354             watch,
355             version: self.vfs_config_version,
356         });
357
358         // Create crate graph from all the workspaces
359         let crate_graph = {
360             let proc_macro_client = self.proc_macro_client.as_ref();
361             let mut load_proc_macro =
362                 move |path: &AbsPath| load_proc_macro(proc_macro_client, path);
363
364             let vfs = &mut self.vfs.write().0;
365             let loader = &mut self.loader;
366             let mem_docs = &self.mem_docs;
367             let mut load = move |path: &AbsPath| {
368                 let _p = profile::span("GlobalState::load");
369                 let vfs_path = vfs::VfsPath::from(path.to_path_buf());
370                 if !mem_docs.contains(&vfs_path) {
371                     let contents = loader.handle.load_sync(path);
372                     vfs.set_file_contents(vfs_path.clone(), contents);
373                 }
374                 let res = vfs.file_id(&vfs_path);
375                 if res.is_none() {
376                     tracing::warn!("failed to load {}", path.display())
377                 }
378                 res
379             };
380
381             let mut crate_graph = CrateGraph::default();
382             for ws in self.workspaces.iter() {
383                 crate_graph.extend(ws.to_crate_graph(&mut load_proc_macro, &mut load));
384             }
385             crate_graph
386         };
387         change.set_crate_graph(crate_graph);
388
389         self.source_root_config = project_folders.source_root_config;
390
391         self.analysis_host.apply_change(change);
392         self.process_changes();
393         self.reload_flycheck();
394         tracing::info!("did switch workspaces");
395     }
396
397     fn fetch_workspace_error(&self) -> Option<String> {
398         let mut buf = String::new();
399
400         for ws in self.fetch_workspaces_queue.last_op_result() {
401             if let Err(err) = ws {
402                 stdx::format_to!(buf, "rust-analyzer failed to load workspace: {:#}\n", err);
403             }
404         }
405
406         if buf.is_empty() {
407             return None;
408         }
409
410         Some(buf)
411     }
412
413     fn fetch_build_data_error(&self) -> Option<String> {
414         let mut buf = "rust-analyzer failed to run build scripts:\n".to_string();
415         let mut has_errors = false;
416
417         for ws in &self.fetch_build_data_queue.last_op_result().1 {
418             match ws {
419                 Ok(data) => {
420                     if let Some(err) = data.error() {
421                         has_errors = true;
422                         stdx::format_to!(buf, "{:#}\n", err);
423                     }
424                 }
425                 Err(err) => {
426                     has_errors = true;
427                     stdx::format_to!(buf, "{:#}\n", err);
428                 }
429             }
430         }
431
432         if has_errors {
433             Some(buf)
434         } else {
435             None
436         }
437     }
438
439     fn reload_flycheck(&mut self) {
440         let _p = profile::span("GlobalState::reload_flycheck");
441         let config = match self.config.flycheck() {
442             Some(it) => it,
443             None => {
444                 self.flycheck = Vec::new();
445                 return;
446             }
447         };
448
449         let sender = self.flycheck_sender.clone();
450         self.flycheck = self
451             .workspaces
452             .iter()
453             .enumerate()
454             .filter_map(|(id, w)| match w {
455                 ProjectWorkspace::Cargo { cargo, .. } => Some((id, cargo.workspace_root())),
456                 ProjectWorkspace::Json { project, .. } => {
457                     // Enable flychecks for json projects if a custom flycheck command was supplied
458                     // in the workspace configuration.
459                     match config {
460                         FlycheckConfig::CustomCommand { .. } => Some((id, project.path())),
461                         _ => None,
462                     }
463                 }
464                 ProjectWorkspace::DetachedFiles { .. } => None,
465             })
466             .map(|(id, root)| {
467                 let sender = sender.clone();
468                 FlycheckHandle::spawn(
469                     id,
470                     Box::new(move |msg| sender.send(msg).unwrap()),
471                     config.clone(),
472                     root.to_path_buf().into(),
473                 )
474             })
475             .collect();
476     }
477 }
478
479 #[derive(Default)]
480 pub(crate) struct ProjectFolders {
481     pub(crate) load: Vec<vfs::loader::Entry>,
482     pub(crate) watch: Vec<usize>,
483     pub(crate) source_root_config: SourceRootConfig,
484 }
485
486 impl ProjectFolders {
487     pub(crate) fn new(
488         workspaces: &[ProjectWorkspace],
489         global_excludes: &[AbsPathBuf],
490     ) -> ProjectFolders {
491         let mut res = ProjectFolders::default();
492         let mut fsc = FileSetConfig::builder();
493         let mut local_filesets = vec![];
494
495         for root in workspaces.iter().flat_map(|ws| ws.to_roots()) {
496             let file_set_roots: Vec<VfsPath> =
497                 root.include.iter().cloned().map(VfsPath::from).collect();
498
499             let entry = {
500                 let mut dirs = vfs::loader::Directories::default();
501                 dirs.extensions.push("rs".into());
502                 dirs.include.extend(root.include);
503                 dirs.exclude.extend(root.exclude);
504                 for excl in global_excludes {
505                     if dirs
506                         .include
507                         .iter()
508                         .any(|incl| incl.starts_with(excl) || excl.starts_with(incl))
509                     {
510                         dirs.exclude.push(excl.clone());
511                     }
512                 }
513
514                 vfs::loader::Entry::Directories(dirs)
515             };
516
517             if root.is_local {
518                 res.watch.push(res.load.len());
519             }
520             res.load.push(entry);
521
522             if root.is_local {
523                 local_filesets.push(fsc.len());
524             }
525             fsc.add_file_set(file_set_roots)
526         }
527
528         let fsc = fsc.build();
529         res.source_root_config = SourceRootConfig { fsc, local_filesets };
530
531         res
532     }
533 }
534
535 #[derive(Default, Debug)]
536 pub(crate) struct SourceRootConfig {
537     pub(crate) fsc: FileSetConfig,
538     pub(crate) local_filesets: Vec<usize>,
539 }
540
541 impl SourceRootConfig {
542     pub(crate) fn partition(&self, vfs: &vfs::Vfs) -> Vec<SourceRoot> {
543         let _p = profile::span("SourceRootConfig::partition");
544         self.fsc
545             .partition(vfs)
546             .into_iter()
547             .enumerate()
548             .map(|(idx, file_set)| {
549                 let is_local = self.local_filesets.contains(&idx);
550                 if is_local {
551                     SourceRoot::new_local(file_set)
552                 } else {
553                     SourceRoot::new_library(file_set)
554                 }
555             })
556             .collect()
557     }
558 }
559
560 pub(crate) fn load_proc_macro(client: Option<&ProcMacroServer>, path: &AbsPath) -> Vec<ProcMacro> {
561     let dylib = match MacroDylib::new(path.to_path_buf()) {
562         Ok(it) => it,
563         Err(err) => {
564             // FIXME: that's not really right -- we store this error in a
565             // persistent status.
566             tracing::warn!("failed to load proc macro: {}", err);
567             return Vec::new();
568         }
569     };
570
571     return client
572         .map(|it| it.load_dylib(dylib))
573         .into_iter()
574         .flat_map(|it| match it {
575             Ok(Ok(macros)) => macros,
576             Err(err) => {
577                 tracing::error!("proc macro server crashed: {}", err);
578                 Vec::new()
579             }
580             Ok(Err(err)) => {
581                 // FIXME: that's not really right -- we store this error in a
582                 // persistent status.
583                 tracing::warn!("failed to load proc macro: {}", err);
584                 Vec::new()
585             }
586         })
587         .map(expander_to_proc_macro)
588         .collect();
589
590     fn expander_to_proc_macro(expander: proc_macro_api::ProcMacro) -> ProcMacro {
591         let name = expander.name().into();
592         let kind = match expander.kind() {
593             proc_macro_api::ProcMacroKind::CustomDerive => ProcMacroKind::CustomDerive,
594             proc_macro_api::ProcMacroKind::FuncLike => ProcMacroKind::FuncLike,
595             proc_macro_api::ProcMacroKind::Attr => ProcMacroKind::Attr,
596         };
597         let expander = Arc::new(Expander(expander));
598         ProcMacro { name, kind, expander }
599     }
600
601     #[derive(Debug)]
602     struct Expander(proc_macro_api::ProcMacro);
603
604     impl ProcMacroExpander for Expander {
605         fn expand(
606             &self,
607             subtree: &tt::Subtree,
608             attrs: Option<&tt::Subtree>,
609             env: &Env,
610         ) -> Result<tt::Subtree, ProcMacroExpansionError> {
611             let env = env.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect();
612             match self.0.expand(subtree, attrs, env) {
613                 Ok(Ok(subtree)) => Ok(subtree),
614                 Ok(Err(err)) => Err(ProcMacroExpansionError::Panic(err.0)),
615                 Err(err) => Err(ProcMacroExpansionError::System(err.to_string())),
616             }
617         }
618     }
619 }