1 //! Project loading & configuration updates
2 use std::{mem, sync::Arc};
4 use flycheck::{FlycheckConfig, FlycheckHandle};
5 use hir::db::DefDatabase;
7 use ide_db::base_db::{CrateGraph, SourceRoot, VfsPath};
8 use proc_macro_api::ProcMacroClient;
9 use project_model::{ProjectWorkspace, WorkspaceBuildScripts};
10 use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
13 config::{Config, FilesWatcher, LinkedProject},
14 global_state::GlobalState,
20 pub(crate) enum ProjectWorkspaceProgress {
23 End(Vec<anyhow::Result<ProjectWorkspace>>),
27 pub(crate) enum BuildDataProgress {
30 End((Arc<Vec<ProjectWorkspace>>, Vec<anyhow::Result<WorkspaceBuildScripts>>)),
34 pub(crate) fn is_quiescent(&self) -> bool {
35 !(self.fetch_workspaces_queue.op_in_progress()
36 || self.fetch_build_data_queue.op_in_progress()
37 || self.vfs_progress_config_version < self.vfs_config_version
38 || self.vfs_progress_n_done < self.vfs_progress_n_total)
41 pub(crate) fn update_configuration(&mut self, config: Config) {
42 let _p = profile::span("GlobalState::update_configuration");
43 let old_config = mem::replace(&mut self.config, Arc::new(config));
44 if self.config.lru_capacity() != old_config.lru_capacity() {
45 self.analysis_host.update_lru_capacity(self.config.lru_capacity());
47 if self.config.linked_projects() != old_config.linked_projects() {
48 self.fetch_workspaces_request()
49 } else if self.config.flycheck() != old_config.flycheck() {
50 self.reload_flycheck();
53 // Apply experimental feature flags.
56 .set_enable_proc_attr_macros(self.config.expand_proc_attr_macros());
58 pub(crate) fn maybe_refresh(&mut self, changes: &[(AbsPathBuf, ChangeKind)]) {
59 if !changes.iter().any(|(path, kind)| is_interesting(path, *kind)) {
63 "Requesting workspace reload because of the following changes: {}",
67 .filter(|(path, kind)| is_interesting(path, *kind))
68 .map(|(path, kind)| format!("{}: {:?}", path.display(), kind)),
72 self.fetch_workspaces_request();
74 fn is_interesting(path: &AbsPath, change_kind: ChangeKind) -> bool {
75 const IMPLICIT_TARGET_FILES: &[&str] = &["build.rs", "src/main.rs", "src/lib.rs"];
76 const IMPLICIT_TARGET_DIRS: &[&str] = &["src/bin", "examples", "tests", "benches"];
77 let file_name = path.file_name().unwrap_or_default();
79 if file_name == "Cargo.toml" || file_name == "Cargo.lock" {
82 if change_kind == ChangeKind::Modify {
85 if path.extension().unwrap_or_default() != "rs" {
88 if IMPLICIT_TARGET_FILES.iter().any(|it| path.as_ref().ends_with(it)) {
91 let parent = match path.parent() {
95 if IMPLICIT_TARGET_DIRS.iter().any(|it| parent.as_ref().ends_with(it)) {
98 if file_name == "main.rs" {
99 let grand_parent = match parent.parent() {
101 None => return false,
103 if IMPLICIT_TARGET_DIRS.iter().any(|it| grand_parent.as_ref().ends_with(it)) {
110 pub(crate) fn report_new_status_if_needed(&mut self) {
111 let mut status = lsp_ext::ServerStatusParams {
112 health: lsp_ext::Health::Ok,
113 quiescent: self.is_quiescent(),
117 if let Some(error) = self.fetch_build_data_error() {
118 status.health = lsp_ext::Health::Warning;
119 status.message = Some(error)
121 if !self.config.cargo_autoreload()
122 && self.is_quiescent()
123 && self.fetch_workspaces_queue.op_requested()
125 status.health = lsp_ext::Health::Warning;
126 status.message = Some("Workspace reload required".to_string())
129 if let Some(error) = self.fetch_workspace_error() {
130 status.health = lsp_ext::Health::Error;
131 status.message = Some(error)
134 if self.last_reported_status.as_ref() != Some(&status) {
135 self.last_reported_status = Some(status.clone());
137 if let (lsp_ext::Health::Error, Some(message)) = (status.health, &status.message) {
138 self.show_message(lsp_types::MessageType::Error, message.clone());
141 if self.config.server_status_notification() {
142 self.send_notification::<lsp_ext::ServerStatusNotification>(status);
147 pub(crate) fn fetch_workspaces_request(&mut self) {
148 self.fetch_workspaces_queue.request_op()
150 pub(crate) fn fetch_workspaces_if_needed(&mut self) {
151 if !self.fetch_workspaces_queue.should_start_op() {
154 log::info!("will fetch workspaces");
156 self.task_pool.handle.spawn_with_sender({
157 let linked_projects = self.config.linked_projects();
158 let detached_files = self.config.detached_files().to_vec();
159 let cargo_config = self.config.cargo();
163 let sender = sender.clone();
166 .send(Task::FetchWorkspace(ProjectWorkspaceProgress::Report(msg)))
171 sender.send(Task::FetchWorkspace(ProjectWorkspaceProgress::Begin)).unwrap();
173 let mut workspaces = linked_projects
175 .map(|project| match project {
176 LinkedProject::ProjectManifest(manifest) => {
177 project_model::ProjectWorkspace::load(
183 LinkedProject::InlineJsonProject(it) => {
184 project_model::ProjectWorkspace::load_inline(
186 cargo_config.target.as_deref(),
190 .collect::<Vec<_>>();
192 if !detached_files.is_empty() {
194 .push(project_model::ProjectWorkspace::load_detached_files(detached_files));
197 log::info!("did fetch workspaces {:?}", workspaces);
199 .send(Task::FetchWorkspace(ProjectWorkspaceProgress::End(workspaces)))
204 pub(crate) fn fetch_workspaces_completed(
206 workspaces: Vec<anyhow::Result<ProjectWorkspace>>,
208 self.fetch_workspaces_queue.op_completed(workspaces)
211 pub(crate) fn fetch_build_data_request(&mut self) {
212 self.fetch_build_data_queue.request_op();
214 pub(crate) fn fetch_build_data_if_needed(&mut self) {
215 if !self.fetch_build_data_queue.should_start_op() {
219 let workspaces = Arc::clone(&self.workspaces);
220 let config = self.config.cargo();
221 self.task_pool.handle.spawn_with_sender(move |sender| {
222 sender.send(Task::FetchBuildData(BuildDataProgress::Begin)).unwrap();
225 let sender = sender.clone();
227 sender.send(Task::FetchBuildData(BuildDataProgress::Report(msg))).unwrap()
230 let mut res = Vec::new();
231 for ws in workspaces.iter() {
232 res.push(ws.run_build_scripts(&config, &progress));
234 ProjectWorkspace::Cargo { cargo, .. } => cargo,
235 ProjectWorkspace::DetachedFiles { .. } | ProjectWorkspace::Json { .. } => {
236 res.push(Ok(WorkspaceBuildScripts::default()));
240 res.push(WorkspaceBuildScripts::run(&config, ws, &progress))
242 sender.send(Task::FetchBuildData(BuildDataProgress::End((workspaces, res)))).unwrap();
245 pub(crate) fn fetch_build_data_completed(
247 build_data: (Arc<Vec<ProjectWorkspace>>, Vec<anyhow::Result<WorkspaceBuildScripts>>),
249 self.fetch_build_data_queue.op_completed(build_data)
252 pub(crate) fn switch_workspaces(&mut self) {
253 let _p = profile::span("GlobalState::switch_workspaces");
254 log::info!("will switch workspaces");
256 if let Some(error_message) = self.fetch_workspace_error() {
257 log::error!("failed to switch workspaces: {}", error_message);
258 if !self.workspaces.is_empty() {
259 // It only makes sense to switch to a partially broken workspace
260 // if we don't have any workspace at all yet.
265 if let Some(error_message) = self.fetch_build_data_error() {
266 log::error!("failed to switch build data: {}", error_message);
269 let workspaces = self
270 .fetch_workspaces_queue
273 .filter_map(|res| res.as_ref().ok().cloned())
274 .collect::<Vec<_>>();
276 fn eq_ignore_build_data<'a>(
277 left: &'a ProjectWorkspace,
278 right: &'a ProjectWorkspace,
280 let key = |p: &'a ProjectWorkspace| match p {
281 ProjectWorkspace::Cargo {
289 } => Some((cargo, sysroot, rustc, rustc_cfg, cfg_overrides)),
292 match (key(left), key(right)) {
293 (Some(lk), Some(rk)) => lk == rk,
298 let same_workspaces = workspaces.len() == self.workspaces.len()
301 .zip(self.workspaces.iter())
302 .all(|(l, r)| eq_ignore_build_data(l, r));
305 let (workspaces, build_scripts) = self.fetch_build_data_queue.last_op_result();
306 if Arc::ptr_eq(&workspaces, &self.workspaces) {
307 let workspaces = workspaces
311 .map(|(mut ws, bs)| {
312 ws.set_build_scripts(bs.as_ref().ok().cloned().unwrap_or_default());
315 .collect::<Vec<_>>();
317 // Workspaces are the same, but we've updated build data.
318 self.workspaces = Arc::new(workspaces);
320 // Current build scripts do not match the version of the active
321 // workspace, so there's nothing for us to update.
325 // Here, we completely changed the workspace (Cargo.toml edit), so
326 // we don't care about build-script results, they are stale.
327 self.workspaces = Arc::new(workspaces)
330 if let FilesWatcher::Client = self.config.files().watcher {
331 if self.config.did_change_watched_files_dynamic_registration() {
332 let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
336 .flat_map(|ws| ws.to_roots())
337 .filter(|it| it.is_member)
339 root.include.into_iter().flat_map(|it| {
341 format!("{}/**/*.rs", it.display()),
342 format!("{}/**/Cargo.toml", it.display()),
343 format!("{}/**/Cargo.lock", it.display()),
347 .map(|glob_pattern| lsp_types::FileSystemWatcher {
353 let registration = lsp_types::Registration {
354 id: "workspace/didChangeWatchedFiles".to_string(),
355 method: "workspace/didChangeWatchedFiles".to_string(),
356 register_options: Some(serde_json::to_value(registration_options).unwrap()),
358 self.send_request::<lsp_types::request::RegisterCapability>(
359 lsp_types::RegistrationParams { registrations: vec![registration] },
365 let mut change = Change::new();
367 let files_config = self.config.files();
368 let project_folders = ProjectFolders::new(&self.workspaces, &files_config.exclude);
370 if self.proc_macro_client.is_none() {
371 self.proc_macro_client = match self.config.proc_macro_srv() {
373 Some((path, args)) => match ProcMacroClient::extern_process(path.clone(), args) {
377 "Failed to run proc_macro_srv from path {}, error: {:?}",
387 let watch = match files_config.watcher {
388 FilesWatcher::Client => vec![],
389 FilesWatcher::Notify => project_folders.watch,
391 self.vfs_config_version += 1;
392 self.loader.handle.set_config(vfs::loader::Config {
393 load: project_folders.load,
395 version: self.vfs_config_version,
398 // Create crate graph from all the workspaces
400 let proc_macro_client = self.proc_macro_client.as_ref();
401 let mut load_proc_macro = move |path: &AbsPath| {
402 proc_macro_client.map(|it| it.by_dylib_path(path)).unwrap_or_default()
405 let vfs = &mut self.vfs.write().0;
406 let loader = &mut self.loader;
407 let mem_docs = &self.mem_docs;
408 let mut load = move |path: &AbsPath| {
409 let _p = profile::span("GlobalState::load");
410 let vfs_path = vfs::VfsPath::from(path.to_path_buf());
411 if !mem_docs.contains(&vfs_path) {
412 let contents = loader.handle.load_sync(path);
413 vfs.set_file_contents(vfs_path.clone(), contents);
415 let res = vfs.file_id(&vfs_path);
417 log::warn!("failed to load {}", path.display())
422 let mut crate_graph = CrateGraph::default();
423 for ws in self.workspaces.iter() {
424 crate_graph.extend(ws.to_crate_graph(&mut load_proc_macro, &mut load));
428 change.set_crate_graph(crate_graph);
430 self.source_root_config = project_folders.source_root_config;
432 self.analysis_host.apply_change(change);
433 self.process_changes();
434 self.reload_flycheck();
435 log::info!("did switch workspaces");
438 fn fetch_workspace_error(&self) -> Option<String> {
439 let mut buf = String::new();
441 for ws in self.fetch_workspaces_queue.last_op_result() {
442 if let Err(err) = ws {
443 stdx::format_to!(buf, "rust-analyzer failed to load workspace: {:#}\n", err);
454 fn fetch_build_data_error(&self) -> Option<String> {
455 let mut buf = String::new();
457 for ws in &self.fetch_build_data_queue.last_op_result().1 {
458 if let Err(err) = ws {
459 stdx::format_to!(buf, "rust-analyzer failed to run custom build: {:#}\n", err);
470 fn reload_flycheck(&mut self) {
471 let _p = profile::span("GlobalState::reload_flycheck");
472 let config = match self.config.flycheck() {
475 self.flycheck = Vec::new();
480 let sender = self.flycheck_sender.clone();
485 .filter_map(|(id, w)| match w {
486 ProjectWorkspace::Cargo { cargo, .. } => Some((id, cargo.workspace_root())),
487 ProjectWorkspace::Json { project, .. } => {
488 // Enable flychecks for json projects if a custom flycheck command was supplied
489 // in the workspace configuration.
491 FlycheckConfig::CustomCommand { .. } => Some((id, project.path())),
495 ProjectWorkspace::DetachedFiles { .. } => None,
498 let sender = sender.clone();
499 FlycheckHandle::spawn(
501 Box::new(move |msg| sender.send(msg).unwrap()),
503 root.to_path_buf().into(),
511 pub(crate) struct ProjectFolders {
512 pub(crate) load: Vec<vfs::loader::Entry>,
513 pub(crate) watch: Vec<usize>,
514 pub(crate) source_root_config: SourceRootConfig,
517 impl ProjectFolders {
519 workspaces: &[ProjectWorkspace],
520 global_excludes: &[AbsPathBuf],
521 ) -> ProjectFolders {
522 let mut res = ProjectFolders::default();
523 let mut fsc = FileSetConfig::builder();
524 let mut local_filesets = vec![];
526 for root in workspaces.iter().flat_map(|ws| ws.to_roots()) {
527 let file_set_roots: Vec<VfsPath> =
528 root.include.iter().cloned().map(VfsPath::from).collect();
531 let mut dirs = vfs::loader::Directories::default();
532 dirs.extensions.push("rs".into());
533 dirs.include.extend(root.include);
534 dirs.exclude.extend(root.exclude);
535 for excl in global_excludes {
539 .any(|incl| incl.starts_with(excl) || excl.starts_with(incl))
541 dirs.exclude.push(excl.clone());
545 vfs::loader::Entry::Directories(dirs)
549 res.watch.push(res.load.len());
551 res.load.push(entry);
554 local_filesets.push(fsc.len());
556 fsc.add_file_set(file_set_roots)
559 let fsc = fsc.build();
560 res.source_root_config = SourceRootConfig { fsc, local_filesets };
566 #[derive(Default, Debug)]
567 pub(crate) struct SourceRootConfig {
568 pub(crate) fsc: FileSetConfig,
569 pub(crate) local_filesets: Vec<usize>,
572 impl SourceRootConfig {
573 pub(crate) fn partition(&self, vfs: &vfs::Vfs) -> Vec<SourceRoot> {
574 let _p = profile::span("SourceRootConfig::partition");
579 .map(|(idx, file_set)| {
580 let is_local = self.local_filesets.contains(&idx);
582 SourceRoot::new_local(file_set)
584 SourceRoot::new_library(file_set)