1 //! Project loading & configuration updates
2 use std::{mem, sync::Arc};
4 use flycheck::{FlycheckConfig, FlycheckHandle};
6 use ide_db::base_db::{CrateGraph, SourceRoot, VfsPath};
7 use project_model::{BuildDataCollector, BuildDataResult, ProcMacroClient, ProjectWorkspace};
8 use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
11 config::{Config, FilesWatcher, LinkedProject},
12 global_state::GlobalState,
18 pub(crate) enum ProjectWorkspaceProgress {
21 End(Vec<anyhow::Result<ProjectWorkspace>>),
25 pub(crate) enum BuildDataProgress {
28 End(anyhow::Result<BuildDataResult>),
32 pub(crate) fn is_quiescent(&self) -> bool {
33 !(self.fetch_workspaces_queue.op_in_progress()
34 || self.fetch_build_data_queue.op_in_progress()
35 || self.vfs_progress_config_version < self.vfs_config_version
36 || self.vfs_progress_n_done < self.vfs_progress_n_total)
39 pub(crate) fn update_configuration(&mut self, config: Config) {
40 let _p = profile::span("GlobalState::update_configuration");
41 let old_config = mem::replace(&mut self.config, Arc::new(config));
42 if self.config.lru_capacity() != old_config.lru_capacity() {
43 self.analysis_host.update_lru_capacity(self.config.lru_capacity());
45 if self.config.linked_projects() != old_config.linked_projects() {
46 self.fetch_workspaces_request()
47 } else if self.config.flycheck() != old_config.flycheck() {
48 self.reload_flycheck();
51 pub(crate) fn maybe_refresh(&mut self, changes: &[(AbsPathBuf, ChangeKind)]) {
52 if !changes.iter().any(|(path, kind)| is_interesting(path, *kind)) {
56 "Requesting workspace reload because of the following changes: {}",
60 .filter(|(path, kind)| is_interesting(path, *kind))
61 .map(|(path, kind)| format!("{}: {:?}", path.display(), kind)),
65 self.fetch_workspaces_request();
67 fn is_interesting(path: &AbsPath, change_kind: ChangeKind) -> bool {
68 const IMPLICIT_TARGET_FILES: &[&str] = &["build.rs", "src/main.rs", "src/lib.rs"];
69 const IMPLICIT_TARGET_DIRS: &[&str] = &["src/bin", "examples", "tests", "benches"];
71 if path.ends_with("Cargo.toml") || path.ends_with("Cargo.lock") {
74 if change_kind == ChangeKind::Modify {
77 if path.extension().unwrap_or_default() != "rs" {
80 if IMPLICIT_TARGET_FILES.iter().any(|it| path.ends_with(it)) {
83 let parent = match path.parent() {
87 if IMPLICIT_TARGET_DIRS.iter().any(|it| parent.ends_with(it)) {
90 if path.ends_with("main.rs") {
91 let grand_parent = match parent.parent() {
95 if IMPLICIT_TARGET_DIRS.iter().any(|it| grand_parent.ends_with(it)) {
102 pub(crate) fn report_new_status_if_needed(&mut self) {
103 let mut status = lsp_ext::ServerStatusParams {
104 health: lsp_ext::Health::Ok,
105 quiescent: self.is_quiescent(),
109 if let Some(error) = self.build_data_error() {
110 status.health = lsp_ext::Health::Warning;
111 status.message = Some(error)
113 if !self.config.cargo_autoreload()
114 && self.is_quiescent()
115 && self.fetch_workspaces_queue.op_requested()
117 status.health = lsp_ext::Health::Warning;
118 status.message = Some("Workspace reload required".to_string())
121 if let Some(error) = self.fetch_workspace_error() {
122 status.health = lsp_ext::Health::Error;
123 status.message = Some(error)
126 if self.last_reported_status.as_ref() != Some(&status) {
127 self.last_reported_status = Some(status.clone());
129 if let (lsp_ext::Health::Error, Some(message)) = (status.health, &status.message) {
130 self.show_message(lsp_types::MessageType::Error, message.clone());
133 if self.config.server_status_notification() {
134 self.send_notification::<lsp_ext::ServerStatusNotification>(status);
139 pub(crate) fn fetch_workspaces_request(&mut self) {
140 self.fetch_workspaces_queue.request_op(())
142 pub(crate) fn fetch_workspaces_if_needed(&mut self) {
143 if self.fetch_workspaces_queue.should_start_op().is_none() {
146 log::info!("will fetch workspaces");
148 self.task_pool.handle.spawn_with_sender({
149 let linked_projects = self.config.linked_projects();
150 let detached_files = self.config.detached_files().to_vec();
151 let cargo_config = self.config.cargo();
155 let sender = sender.clone();
158 .send(Task::FetchWorkspace(ProjectWorkspaceProgress::Report(msg)))
163 sender.send(Task::FetchWorkspace(ProjectWorkspaceProgress::Begin)).unwrap();
165 let mut workspaces = linked_projects
167 .map(|project| match project {
168 LinkedProject::ProjectManifest(manifest) => {
169 project_model::ProjectWorkspace::load(
175 LinkedProject::InlineJsonProject(it) => {
176 project_model::ProjectWorkspace::load_inline(
178 cargo_config.target.as_deref(),
182 .collect::<Vec<_>>();
184 if !detached_files.is_empty() {
186 .push(project_model::ProjectWorkspace::load_detached_files(detached_files));
189 log::info!("did fetch workspaces {:?}", workspaces);
191 .send(Task::FetchWorkspace(ProjectWorkspaceProgress::End(workspaces)))
196 pub(crate) fn fetch_workspaces_completed(
198 workspaces: Vec<anyhow::Result<ProjectWorkspace>>,
200 self.fetch_workspaces_queue.op_completed(workspaces)
203 pub(crate) fn fetch_build_data_request(&mut self, build_data_collector: BuildDataCollector) {
204 self.fetch_build_data_queue.request_op(build_data_collector);
206 pub(crate) fn fetch_build_data_if_needed(&mut self) {
207 let mut build_data_collector = match self.fetch_build_data_queue.should_start_op() {
211 self.task_pool.handle.spawn_with_sender(move |sender| {
212 sender.send(Task::FetchBuildData(BuildDataProgress::Begin)).unwrap();
215 let sender = sender.clone();
217 sender.send(Task::FetchBuildData(BuildDataProgress::Report(msg))).unwrap()
220 let res = build_data_collector.collect(&progress);
221 sender.send(Task::FetchBuildData(BuildDataProgress::End(res))).unwrap();
224 pub(crate) fn fetch_build_data_completed(
226 build_data: anyhow::Result<BuildDataResult>,
228 self.fetch_build_data_queue.op_completed(Some(build_data))
231 pub(crate) fn switch_workspaces(&mut self) {
232 let _p = profile::span("GlobalState::switch_workspaces");
233 log::info!("will switch workspaces");
235 if let Some(error_message) = self.fetch_workspace_error() {
236 log::error!("failed to switch workspaces: {}", error_message);
237 if !self.workspaces.is_empty() {
242 if let Some(error_message) = self.build_data_error() {
243 log::error!("failed to switch build data: {}", error_message);
246 let workspaces = self
247 .fetch_workspaces_queue
250 .filter_map(|res| res.as_ref().ok().cloned())
251 .collect::<Vec<_>>();
253 let workspace_build_data = match self.fetch_build_data_queue.last_op_result() {
254 Some(Ok(it)) => Some(it.clone()),
255 None | Some(Err(_)) => None,
258 if *self.workspaces == workspaces && self.workspace_build_data == workspace_build_data {
262 if let FilesWatcher::Client = self.config.files().watcher {
263 if self.config.did_change_watched_files_dynamic_registration() {
264 let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
267 .flat_map(|it| it.to_roots(workspace_build_data.as_ref()))
268 .filter(|it| it.is_member)
270 root.include.into_iter().map(|it| format!("{}/**/*.rs", it.display()))
272 .map(|glob_pattern| lsp_types::FileSystemWatcher {
278 let registration = lsp_types::Registration {
279 id: "workspace/didChangeWatchedFiles".to_string(),
280 method: "workspace/didChangeWatchedFiles".to_string(),
281 register_options: Some(serde_json::to_value(registration_options).unwrap()),
283 self.send_request::<lsp_types::request::RegisterCapability>(
284 lsp_types::RegistrationParams { registrations: vec![registration] },
290 let mut change = Change::new();
292 let files_config = self.config.files();
293 let project_folders =
294 ProjectFolders::new(&workspaces, &files_config.exclude, workspace_build_data.as_ref());
296 if self.proc_macro_client.is_none() {
297 self.proc_macro_client = match self.config.proc_macro_srv() {
299 Some((path, args)) => match ProcMacroClient::extern_process(path.clone(), args) {
303 "Failed to run proc_macro_srv from path {}, error: {:?}",
313 let watch = match files_config.watcher {
314 FilesWatcher::Client => vec![],
315 FilesWatcher::Notify => project_folders.watch,
317 self.vfs_config_version += 1;
318 self.loader.handle.set_config(vfs::loader::Config {
319 load: project_folders.load,
321 version: self.vfs_config_version,
324 // Create crate graph from all the workspaces
326 let mut crate_graph = CrateGraph::default();
327 let vfs = &mut self.vfs.write().0;
328 let loader = &mut self.loader;
329 let mem_docs = &self.mem_docs;
330 let mut load = |path: &AbsPath| {
331 let _p = profile::span("GlobalState::load");
332 let vfs_path = vfs::VfsPath::from(path.to_path_buf());
333 if !mem_docs.contains_key(&vfs_path) {
334 let contents = loader.handle.load_sync(path);
335 vfs.set_file_contents(vfs_path.clone(), contents);
337 let res = vfs.file_id(&vfs_path);
339 log::warn!("failed to load {}", path.display())
343 for ws in workspaces.iter() {
344 crate_graph.extend(ws.to_crate_graph(
345 workspace_build_data.as_ref(),
346 self.proc_macro_client.as_ref(),
353 change.set_crate_graph(crate_graph);
355 self.source_root_config = project_folders.source_root_config;
356 self.workspaces = Arc::new(workspaces);
357 self.workspace_build_data = workspace_build_data;
359 self.analysis_host.apply_change(change);
360 self.process_changes();
361 self.reload_flycheck();
362 log::info!("did switch workspaces");
365 fn fetch_workspace_error(&self) -> Option<String> {
366 let mut buf = String::new();
368 for ws in self.fetch_workspaces_queue.last_op_result() {
369 if let Err(err) = ws {
370 stdx::format_to!(buf, "rust-analyzer failed to load workspace: {:#}\n", err);
381 fn build_data_error(&self) -> Option<String> {
382 match self.fetch_build_data_queue.last_op_result() {
384 Some(format!("rust-analyzer failed to fetch build data: {:#}\n", err))
386 Some(Ok(data)) => data.error(),
391 fn reload_flycheck(&mut self) {
392 let _p = profile::span("GlobalState::reload_flycheck");
393 let config = match self.config.flycheck() {
396 self.flycheck = Vec::new();
401 let sender = self.flycheck_sender.clone();
406 .filter_map(|(id, w)| match w {
407 ProjectWorkspace::Cargo { cargo, .. } => Some((id, cargo.workspace_root())),
408 ProjectWorkspace::Json { project, .. } => {
409 // Enable flychecks for json projects if a custom flycheck command was supplied
410 // in the workspace configuration.
412 FlycheckConfig::CustomCommand { .. } => Some((id, project.path())),
416 ProjectWorkspace::DetachedFiles { .. } => None,
419 let sender = sender.clone();
420 FlycheckHandle::spawn(
422 Box::new(move |msg| sender.send(msg).unwrap()),
424 root.to_path_buf().into(),
432 pub(crate) struct ProjectFolders {
433 pub(crate) load: Vec<vfs::loader::Entry>,
434 pub(crate) watch: Vec<usize>,
435 pub(crate) source_root_config: SourceRootConfig,
438 impl ProjectFolders {
440 workspaces: &[ProjectWorkspace],
441 global_excludes: &[AbsPathBuf],
442 build_data: Option<&BuildDataResult>,
443 ) -> ProjectFolders {
444 let mut res = ProjectFolders::default();
445 let mut fsc = FileSetConfig::builder();
446 let mut local_filesets = vec![];
448 for root in workspaces.iter().flat_map(|it| it.to_roots(build_data)) {
449 let file_set_roots: Vec<VfsPath> =
450 root.include.iter().cloned().map(VfsPath::from).collect();
453 let mut dirs = vfs::loader::Directories::default();
454 dirs.extensions.push("rs".into());
455 dirs.include.extend(root.include);
456 dirs.exclude.extend(root.exclude);
457 for excl in global_excludes {
458 if dirs.include.iter().any(|incl| incl.starts_with(excl)) {
459 dirs.exclude.push(excl.clone());
463 vfs::loader::Entry::Directories(dirs)
467 res.watch.push(res.load.len());
469 res.load.push(entry);
472 local_filesets.push(fsc.len());
474 fsc.add_file_set(file_set_roots)
477 let fsc = fsc.build();
478 res.source_root_config = SourceRootConfig { fsc, local_filesets };
484 #[derive(Default, Debug)]
485 pub(crate) struct SourceRootConfig {
486 pub(crate) fsc: FileSetConfig,
487 pub(crate) local_filesets: Vec<usize>,
490 impl SourceRootConfig {
491 pub(crate) fn partition(&self, vfs: &vfs::Vfs) -> Vec<SourceRoot> {
492 let _p = profile::span("SourceRootConfig::partition");
497 .map(|(idx, file_set)| {
498 let is_local = self.local_filesets.contains(&idx);
500 SourceRoot::new_local(file_set)
502 SourceRoot::new_library(file_set)