1 //! The context or environment in which the language server functions. In our
2 //! server implementation this is know as the `WorldState`.
3 //!
4 //! Each tick provides an immutable snapshot of the state as `WorldSnapshot`.
5
6 use std::time::Instant;
7
8 use crossbeam_channel::{unbounded, Receiver, Sender};
9 use flycheck::FlycheckHandle;
10 use ide::{Analysis, AnalysisHost, Cancellable, Change, FileId};
11 use ide_db::base_db::{CrateId, FileLoader, ProcMacroPaths, SourceDatabase};
12 use lsp_types::{SemanticTokens, Url};
13 use nohash_hasher::IntMap;
14 use parking_lot::{Mutex, RwLock};
15 use proc_macro_api::ProcMacroServer;
16 use project_model::{CargoWorkspace, ProjectWorkspace, Target, WorkspaceBuildScripts};
17 use rustc_hash::{FxHashMap, FxHashSet};
18 use triomphe::Arc;
19 use vfs::AnchoredPathBuf;
20
21 use crate::{
22 config::{Config, ConfigError},
23 diagnostics::{CheckFixes, DiagnosticCollection},
24 from_proto,
25 line_index::{LineEndings, LineIndex},
26 lsp_ext,
27 main_loop::Task,
28 mem_docs::MemDocs,
29 op_queue::OpQueue,
30 reload::{self, SourceRootConfig},
31 task_pool::TaskPool,
32 to_proto::url_from_abs_path,
33 Result,
34 };
35
36 // Enforces drop order
37 pub(crate) struct Handle<H, C> {
38 pub(crate) handle: H,
39 pub(crate) receiver: C,
40 }
41
42 pub(crate) type ReqHandler = fn(&mut GlobalState, lsp_server::Response);
43 pub(crate) type ReqQueue = lsp_server::ReqQueue<(String, Instant), ReqHandler>;
44
45 /// `GlobalState` is the primary mutable state of the language server
46 ///
47 /// The most interesting components are `vfs`, which stores a consistent
48 /// snapshot of the file systems, and `analysis_host`, which stores our
49 /// incremental salsa database.
50 ///
51 /// Note that this struct has more than one impl in various modules!
52 pub(crate) struct GlobalState {
53 sender: Sender<lsp_server::Message>,
54 req_queue: ReqQueue,
55
56 pub(crate) task_pool: Handle<TaskPool<Task>, Receiver<Task>>,
57 pub(crate) fmt_pool: Handle<TaskPool<Task>, Receiver<Task>>,
58
59 pub(crate) config: Arc<Config>,
60 pub(crate) config_errors: Option<ConfigError>,
61 pub(crate) analysis_host: AnalysisHost,
62 pub(crate) diagnostics: DiagnosticCollection,
63 pub(crate) mem_docs: MemDocs,
64 pub(crate) source_root_config: SourceRootConfig,
65 pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>,
66
67 // status
68 pub(crate) shutdown_requested: bool,
69 pub(crate) last_reported_status: Option<lsp_ext::ServerStatusParams>,
70
71 // proc macros
72 pub(crate) proc_macro_changed: bool,
73 pub(crate) proc_macro_clients: Arc<[anyhow::Result<ProcMacroServer>]>,
74
75 // Flycheck
76 pub(crate) flycheck: Arc<[FlycheckHandle]>,
77 pub(crate) flycheck_sender: Sender<flycheck::Message>,
78 pub(crate) flycheck_receiver: Receiver<flycheck::Message>,
79 pub(crate) last_flycheck_error: Option<String>,
80
81 // VFS
82 pub(crate) loader: Handle<Box<dyn vfs::loader::Handle>, Receiver<vfs::loader::Message>>,
83 pub(crate) vfs: Arc<RwLock<(vfs::Vfs, IntMap<FileId, LineEndings>)>>,
84 pub(crate) vfs_config_version: u32,
85 pub(crate) vfs_progress_config_version: u32,
86 pub(crate) vfs_progress_n_total: usize,
87 pub(crate) vfs_progress_n_done: usize,
88
89 /// `workspaces` field stores the data we actually use, while the `OpQueue`
90 /// stores the result of the last fetch.
91 ///
92 /// If the fetch (partially) fails, we do not update the current value.
93 ///
94 /// The handling of build data is subtle. We fetch workspace in two phases:
95 ///
96 /// *First*, we run `cargo metadata`, which gives us fast results for
97 /// initial analysis.
98 ///
99 /// *Second*, we run `cargo check` which runs build scripts and compiles
100 /// proc macros.
101 ///
102 /// We need both for the precise analysis, but we want rust-analyzer to be
103 /// at least partially available just after the first phase. That's because
104 /// first phase is much faster, and is much less likely to fail.
105 ///
106 /// This creates a complication -- by the time the second phase completes,
107 /// the results of the first phase could be invalid. That is, while we run
108 /// `cargo check`, the user edits `Cargo.toml`, we notice this, and the new
109 /// `cargo metadata` completes before `cargo check`.
110 ///
111 /// An additional complication is that we want to avoid needless work. When
112 /// the user just adds comments or whitespace to Cargo.toml, we do not want
113 /// to invalidate any salsa caches.
114 pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
115 pub(crate) crate_graph_file_dependencies: FxHashSet<vfs::VfsPath>,
116
117 // op queues
118 pub(crate) fetch_workspaces_queue:
119 OpQueue<bool, Option<(Vec<anyhow::Result<ProjectWorkspace>>, bool)>>,
120 pub(crate) fetch_build_data_queue:
121 OpQueue<(), (Arc<Vec<ProjectWorkspace>>, Vec<anyhow::Result<WorkspaceBuildScripts>>)>,
122 pub(crate) fetch_proc_macros_queue: OpQueue<Vec<ProcMacroPaths>, bool>,
123 pub(crate) prime_caches_queue: OpQueue,
124 }
125
126 /// An immutable snapshot of the world's state at a point in time.
127 pub(crate) struct GlobalStateSnapshot {
128 pub(crate) config: Arc<Config>,
129 pub(crate) analysis: Analysis,
130 pub(crate) check_fixes: CheckFixes,
131 mem_docs: MemDocs,
132 pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>,
133 vfs: Arc<RwLock<(vfs::Vfs, IntMap<FileId, LineEndings>)>>,
134 pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
135 // used to signal semantic highlighting to fall back to syntax based highlighting until proc-macros have been loaded
136 pub(crate) proc_macros_loaded: bool,
137 pub(crate) flycheck: Arc<[FlycheckHandle]>,
138 }
139
140 impl std::panic::UnwindSafe for GlobalStateSnapshot {}
141
142 impl GlobalState {
new(sender: Sender<lsp_server::Message>, config: Config) -> GlobalState143 pub(crate) fn new(sender: Sender<lsp_server::Message>, config: Config) -> GlobalState {
144 let loader = {
145 let (sender, receiver) = unbounded::<vfs::loader::Message>();
146 let handle: vfs_notify::NotifyHandle =
147 vfs::loader::Handle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
148 let handle = Box::new(handle) as Box<dyn vfs::loader::Handle>;
149 Handle { handle, receiver }
150 };
151
152 let task_pool = {
153 let (sender, receiver) = unbounded();
154 let handle = TaskPool::new_with_threads(sender, config.main_loop_num_threads());
155 Handle { handle, receiver }
156 };
157 let fmt_pool = {
158 let (sender, receiver) = unbounded();
159 let handle = TaskPool::new_with_threads(sender, 1);
160 Handle { handle, receiver }
161 };
162
163 let mut analysis_host = AnalysisHost::new(config.lru_parse_query_capacity());
164 if let Some(capacities) = config.lru_query_capacities() {
165 analysis_host.update_lru_capacities(capacities);
166 }
167 let (flycheck_sender, flycheck_receiver) = unbounded();
168 let mut this = GlobalState {
169 sender,
170 req_queue: ReqQueue::default(),
171 task_pool,
172 fmt_pool,
173 loader,
174 config: Arc::new(config.clone()),
175 analysis_host,
176 diagnostics: Default::default(),
177 mem_docs: MemDocs::default(),
178 semantic_tokens_cache: Arc::new(Default::default()),
179 shutdown_requested: false,
180 last_reported_status: None,
181 source_root_config: SourceRootConfig::default(),
182 config_errors: Default::default(),
183
184 proc_macro_changed: false,
185 // FIXME: use `Arc::from_iter` when it becomes available
186 proc_macro_clients: Arc::from(Vec::new()),
187
188 // FIXME: use `Arc::from_iter` when it becomes available
189 flycheck: Arc::from(Vec::new()),
190 flycheck_sender,
191 flycheck_receiver,
192 last_flycheck_error: None,
193
194 vfs: Arc::new(RwLock::new((vfs::Vfs::default(), IntMap::default()))),
195 vfs_config_version: 0,
196 vfs_progress_config_version: 0,
197 vfs_progress_n_total: 0,
198 vfs_progress_n_done: 0,
199
200 workspaces: Arc::new(Vec::new()),
201 crate_graph_file_dependencies: FxHashSet::default(),
202 fetch_workspaces_queue: OpQueue::default(),
203 fetch_build_data_queue: OpQueue::default(),
204 fetch_proc_macros_queue: OpQueue::default(),
205
206 prime_caches_queue: OpQueue::default(),
207 };
208 // Apply any required database inputs from the config.
209 this.update_configuration(config);
210 this
211 }
212
process_changes(&mut self) -> bool213 pub(crate) fn process_changes(&mut self) -> bool {
214 let _p = profile::span("GlobalState::process_changes");
215
216 let mut file_changes = FxHashMap::default();
217 let (change, changed_files, workspace_structure_change) = {
218 let mut change = Change::new();
219 let (vfs, line_endings_map) = &mut *self.vfs.write();
220 let changed_files = vfs.take_changes();
221 if changed_files.is_empty() {
222 return false;
223 }
224
225 // We need to fix up the changed events a bit. If we have a create or modify for a file
226 // id that is followed by a delete we actually skip observing the file text from the
227 // earlier event, to avoid problems later on.
228 for changed_file in changed_files {
229 use vfs::ChangeKind::*;
230
231 file_changes
232 .entry(changed_file.file_id)
233 .and_modify(|(change, just_created)| {
234 // None -> Delete => keep
235 // Create -> Delete => collapse
236 //
237 match (change, just_created, changed_file.change_kind) {
238 // latter `Delete` wins
239 (change, _, Delete) => *change = Delete,
240 // merge `Create` with `Create` or `Modify`
241 (Create, _, Create | Modify) => {}
242 // collapse identical `Modify`es
243 (Modify, _, Modify) => {}
244 // equivalent to `Modify`
245 (change @ Delete, just_created, Create) => {
246 *change = Modify;
247 *just_created = true;
248 }
249 // shouldn't occur, but collapse into `Create`
250 (change @ Delete, just_created, Modify) => {
251 *change = Create;
252 *just_created = true;
253 }
254 // shouldn't occur, but collapse into `Modify`
255 (Modify, _, Create) => {}
256 }
257 })
258 .or_insert((
259 changed_file.change_kind,
260 matches!(changed_file.change_kind, Create),
261 ));
262 }
263
264 let changed_files: Vec<_> = file_changes
265 .into_iter()
266 .filter(|(_, (change_kind, just_created))| {
267 !matches!((change_kind, just_created), (vfs::ChangeKind::Delete, true))
268 })
269 .map(|(file_id, (change_kind, _))| vfs::ChangedFile { file_id, change_kind })
270 .collect();
271
272 let mut workspace_structure_change = None;
273 // A file was added or deleted
274 let mut has_structure_changes = false;
275 for file in &changed_files {
276 let vfs_path = &vfs.file_path(file.file_id);
277 if let Some(path) = vfs_path.as_path() {
278 let path = path.to_path_buf();
279 if reload::should_refresh_for_change(&path, file.change_kind) {
280 workspace_structure_change = Some((path.clone(), false));
281 }
282 if file.is_created_or_deleted() {
283 has_structure_changes = true;
284 workspace_structure_change =
285 Some((path, self.crate_graph_file_dependencies.contains(vfs_path)));
286 }
287 }
288
289 // Clear native diagnostics when their file gets deleted
290 if !file.exists() {
291 self.diagnostics.clear_native_for(file.file_id);
292 }
293
294 let text = if file.exists() {
295 let bytes = vfs.file_contents(file.file_id).to_vec();
296 String::from_utf8(bytes).ok().and_then(|text| {
297 let (text, line_endings) = LineEndings::normalize(text);
298 line_endings_map.insert(file.file_id, line_endings);
299 Some(Arc::from(text))
300 })
301 } else {
302 None
303 };
304 change.change_file(file.file_id, text);
305 }
306 if has_structure_changes {
307 let roots = self.source_root_config.partition(vfs);
308 change.set_roots(roots);
309 }
310 (change, changed_files, workspace_structure_change)
311 };
312
313 self.analysis_host.apply_change(change);
314
315 {
316 let raw_database = self.analysis_host.raw_database();
317 // FIXME: ideally we should only trigger a workspace fetch for non-library changes
318 // but something's going wrong with the source root business when we add a new local
319 // crate see https://github.com/rust-lang/rust-analyzer/issues/13029
320 if let Some((path, force_crate_graph_reload)) = workspace_structure_change {
321 self.fetch_workspaces_queue.request_op(
322 format!("workspace vfs file change: {}", path.display()),
323 force_crate_graph_reload,
324 );
325 }
326 self.proc_macro_changed =
327 changed_files.iter().filter(|file| !file.is_created_or_deleted()).any(|file| {
328 let crates = raw_database.relevant_crates(file.file_id);
329 let crate_graph = raw_database.crate_graph();
330
331 crates.iter().any(|&krate| crate_graph[krate].is_proc_macro)
332 });
333 }
334
335 true
336 }
337
snapshot(&self) -> GlobalStateSnapshot338 pub(crate) fn snapshot(&self) -> GlobalStateSnapshot {
339 GlobalStateSnapshot {
340 config: Arc::clone(&self.config),
341 workspaces: Arc::clone(&self.workspaces),
342 analysis: self.analysis_host.analysis(),
343 vfs: Arc::clone(&self.vfs),
344 check_fixes: Arc::clone(&self.diagnostics.check_fixes),
345 mem_docs: self.mem_docs.clone(),
346 semantic_tokens_cache: Arc::clone(&self.semantic_tokens_cache),
347 proc_macros_loaded: !self.config.expand_proc_macros()
348 || *self.fetch_proc_macros_queue.last_op_result(),
349 flycheck: self.flycheck.clone(),
350 }
351 }
352
send_request<R: lsp_types::request::Request>( &mut self, params: R::Params, handler: ReqHandler, )353 pub(crate) fn send_request<R: lsp_types::request::Request>(
354 &mut self,
355 params: R::Params,
356 handler: ReqHandler,
357 ) {
358 let request = self.req_queue.outgoing.register(R::METHOD.to_string(), params, handler);
359 self.send(request.into());
360 }
361
complete_request(&mut self, response: lsp_server::Response)362 pub(crate) fn complete_request(&mut self, response: lsp_server::Response) {
363 let handler = self
364 .req_queue
365 .outgoing
366 .complete(response.id.clone())
367 .expect("received response for unknown request");
368 handler(self, response)
369 }
370
send_notification<N: lsp_types::notification::Notification>( &self, params: N::Params, )371 pub(crate) fn send_notification<N: lsp_types::notification::Notification>(
372 &self,
373 params: N::Params,
374 ) {
375 let not = lsp_server::Notification::new(N::METHOD.to_string(), params);
376 self.send(not.into());
377 }
378
register_request( &mut self, request: &lsp_server::Request, request_received: Instant, )379 pub(crate) fn register_request(
380 &mut self,
381 request: &lsp_server::Request,
382 request_received: Instant,
383 ) {
384 self.req_queue
385 .incoming
386 .register(request.id.clone(), (request.method.clone(), request_received));
387 }
388
respond(&mut self, response: lsp_server::Response)389 pub(crate) fn respond(&mut self, response: lsp_server::Response) {
390 if let Some((method, start)) = self.req_queue.incoming.complete(response.id.clone()) {
391 if let Some(err) = &response.error {
392 if err.message.starts_with("server panicked") {
393 self.poke_rust_analyzer_developer(format!("{}, check the log", err.message))
394 }
395 }
396
397 let duration = start.elapsed();
398 tracing::debug!("handled {} - ({}) in {:0.2?}", method, response.id, duration);
399 self.send(response.into());
400 }
401 }
402
cancel(&mut self, request_id: lsp_server::RequestId)403 pub(crate) fn cancel(&mut self, request_id: lsp_server::RequestId) {
404 if let Some(response) = self.req_queue.incoming.cancel(request_id) {
405 self.send(response.into());
406 }
407 }
408
is_completed(&self, request: &lsp_server::Request) -> bool409 pub(crate) fn is_completed(&self, request: &lsp_server::Request) -> bool {
410 self.req_queue.incoming.is_completed(&request.id)
411 }
412
send(&self, message: lsp_server::Message)413 fn send(&self, message: lsp_server::Message) {
414 self.sender.send(message).unwrap()
415 }
416 }
417
418 impl Drop for GlobalState {
drop(&mut self)419 fn drop(&mut self) {
420 self.analysis_host.request_cancellation();
421 }
422 }
423
424 impl GlobalStateSnapshot {
url_to_file_id(&self, url: &Url) -> Result<FileId>425 pub(crate) fn url_to_file_id(&self, url: &Url) -> Result<FileId> {
426 url_to_file_id(&self.vfs.read().0, url)
427 }
428
file_id_to_url(&self, id: FileId) -> Url429 pub(crate) fn file_id_to_url(&self, id: FileId) -> Url {
430 file_id_to_url(&self.vfs.read().0, id)
431 }
432
file_line_index(&self, file_id: FileId) -> Cancellable<LineIndex>433 pub(crate) fn file_line_index(&self, file_id: FileId) -> Cancellable<LineIndex> {
434 let endings = self.vfs.read().1[&file_id];
435 let index = self.analysis.file_line_index(file_id)?;
436 let res = LineIndex { index, endings, encoding: self.config.position_encoding() };
437 Ok(res)
438 }
439
url_file_version(&self, url: &Url) -> Option<i32>440 pub(crate) fn url_file_version(&self, url: &Url) -> Option<i32> {
441 let path = from_proto::vfs_path(url).ok()?;
442 Some(self.mem_docs.get(&path)?.version)
443 }
444
anchored_path(&self, path: &AnchoredPathBuf) -> Url445 pub(crate) fn anchored_path(&self, path: &AnchoredPathBuf) -> Url {
446 let mut base = self.vfs.read().0.file_path(path.anchor);
447 base.pop();
448 let path = base.join(&path.path).unwrap();
449 let path = path.as_path().unwrap();
450 url_from_abs_path(path)
451 }
452
file_id_to_file_path(&self, file_id: FileId) -> vfs::VfsPath453 pub(crate) fn file_id_to_file_path(&self, file_id: FileId) -> vfs::VfsPath {
454 self.vfs.read().0.file_path(file_id)
455 }
456
cargo_target_for_crate_root( &self, crate_id: CrateId, ) -> Option<(&CargoWorkspace, Target)>457 pub(crate) fn cargo_target_for_crate_root(
458 &self,
459 crate_id: CrateId,
460 ) -> Option<(&CargoWorkspace, Target)> {
461 let file_id = self.analysis.crate_root(crate_id).ok()?;
462 let path = self.vfs.read().0.file_path(file_id);
463 let path = path.as_path()?;
464 self.workspaces.iter().find_map(|ws| match ws {
465 ProjectWorkspace::Cargo { cargo, .. } => {
466 cargo.target_by_root(path).map(|it| (cargo, it))
467 }
468 ProjectWorkspace::Json { .. } => None,
469 ProjectWorkspace::DetachedFiles { .. } => None,
470 })
471 }
472
vfs_memory_usage(&self) -> usize473 pub(crate) fn vfs_memory_usage(&self) -> usize {
474 self.vfs.read().0.memory_usage()
475 }
476 }
477
file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url478 pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url {
479 let path = vfs.file_path(id);
480 let path = path.as_path().unwrap();
481 url_from_abs_path(path)
482 }
483
url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> Result<FileId>484 pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> Result<FileId> {
485 let path = from_proto::vfs_path(url)?;
486 let res = vfs.file_id(&path).ok_or_else(|| format!("file not found: {path}"))?;
487 Ok(res)
488 }
489