1use std::collections::{HashMap, HashSet};
2use std::path::{Path, PathBuf};
3
4use crossbeam_channel::{unbounded, Receiver, RecvTimeoutError, Sender};
5use lsp_types::notification::{
6 DidChangeTextDocument, DidChangeWatchedFiles, DidCloseTextDocument, DidOpenTextDocument,
7};
8use lsp_types::{
9 DidChangeTextDocumentParams, DidChangeWatchedFilesParams, DidCloseTextDocumentParams,
10 DidOpenTextDocumentParams, FileChangeType, FileEvent, TextDocumentContentChangeEvent,
11 TextDocumentIdentifier, TextDocumentItem, VersionedTextDocumentIdentifier,
12};
13
14use crate::config::Config;
15use crate::lsp::child_registry::LspChildRegistry;
16use crate::lsp::client::{LspClient, LspEvent, ServerState};
17use crate::lsp::diagnostics::{
18 from_lsp_diagnostics, DiagnosticEntry, DiagnosticsStore, StoredDiagnostic,
19};
20use crate::lsp::document::DocumentStore;
21use crate::lsp::position::{uri_for_path, uri_to_path};
22use crate::lsp::pull_params::{
23 AftDocumentDiagnosticParams, AftDocumentDiagnosticRequest, AftWorkspaceDiagnosticParams,
24 AftWorkspaceDiagnosticRequest,
25};
26use crate::lsp::registry::{resolve_lsp_binary, servers_for_file, ServerDef, ServerKind};
27use crate::lsp::roots::{find_workspace_root, ServerKey};
28use crate::lsp::LspError;
29use crate::slog_error;
30
31const STDERR_REASON_BYTES: usize = 2 * 1024;
32
33#[derive(Debug, Clone)]
38pub enum ServerAttemptResult {
39 Ok { server_key: ServerKey },
41 NoRootMarker { looked_for: Vec<String> },
44 BinaryNotInstalled { binary: String },
47 SpawnFailed { binary: String, reason: String },
49}
50
51#[derive(Debug, Clone)]
53pub struct ServerAttempt {
54 pub server_id: String,
56 pub server_name: String,
58 pub result: ServerAttemptResult,
59}
60
61#[derive(Debug, Clone, Default)]
67pub struct EnsureServerOutcomes {
68 pub successful: Vec<ServerKey>,
70 pub attempts: Vec<ServerAttempt>,
73}
74
75impl EnsureServerOutcomes {
76 pub fn no_server_registered(&self) -> bool {
78 self.attempts.is_empty()
79 }
80
81 pub fn only_inapplicable_root_markers(&self) -> bool {
89 self.successful.is_empty()
90 && !self.attempts.is_empty()
91 && self
92 .attempts
93 .iter()
94 .all(|attempt| matches!(attempt.result, ServerAttemptResult::NoRootMarker { .. }))
95 }
96}
97
98#[derive(Debug, Clone, Default)]
108pub struct PostEditWaitOutcome {
109 pub diagnostics: Vec<StoredDiagnostic>,
113 pub pending_servers: Vec<ServerKey>,
117 pub exited_servers: Vec<ServerKey>,
121}
122
123#[derive(Debug, Clone, Copy, Default)]
125pub struct PreEditSnapshot {
126 pub epoch: u64,
127 pub document_version_at_capture: Option<i32>,
128}
129
130pub fn post_edit_entry_is_fresh(
131 entry: &DiagnosticEntry,
132 target_version: i32,
133 pre: PreEditSnapshot,
134) -> bool {
135 if entry.epoch <= pre.epoch {
136 return false;
137 }
138
139 match entry.version {
140 Some(version) => version >= target_version,
141 None => false,
146 }
147}
148
149impl PostEditWaitOutcome {
150 pub fn complete(&self) -> bool {
153 self.pending_servers.is_empty() && self.exited_servers.is_empty()
154 }
155}
156
157#[derive(Debug, Clone)]
159pub enum PullFileOutcome {
160 Full { diagnostic_count: usize },
162 Unchanged,
164 PartialNotSupported,
167 PullNotSupported,
170 RequestFailed { reason: String },
172}
173
174#[derive(Debug, Clone)]
176pub struct PullFileResult {
177 pub server_key: ServerKey,
178 pub outcome: PullFileOutcome,
179}
180
181#[derive(Debug, Clone)]
183pub struct PullWorkspaceResult {
184 pub server_key: ServerKey,
185 pub files_reported: Vec<PathBuf>,
189 pub complete: bool,
191 pub cancelled: bool,
193 pub supports_workspace: bool,
197}
198
199pub struct LspManager {
200 clients: HashMap<ServerKey, LspClient>,
202 server_binaries: HashMap<ServerKey, String>,
206 documents: HashMap<ServerKey, DocumentStore>,
208 diagnostics: DiagnosticsStore,
210 event_tx: Sender<LspEvent>,
212 event_rx: Receiver<LspEvent>,
213 binary_overrides: HashMap<ServerKind, PathBuf>,
215 extra_env: HashMap<String, String>,
219 failed_spawns: HashMap<ServerKey, ServerAttemptResult>,
234 watched_file_skip_logged: HashSet<ServerKey>,
237 child_registry: LspChildRegistry,
241}
242
243impl LspManager {
244 pub fn new() -> Self {
245 let (event_tx, event_rx) = unbounded();
246 Self {
247 clients: HashMap::new(),
248 server_binaries: HashMap::new(),
249 documents: HashMap::new(),
250 diagnostics: DiagnosticsStore::new(),
251 event_tx,
252 event_rx,
253 binary_overrides: HashMap::new(),
254 extra_env: HashMap::new(),
255 failed_spawns: HashMap::new(),
256 watched_file_skip_logged: HashSet::new(),
257 child_registry: LspChildRegistry::new(),
258 }
259 }
260
261 pub fn set_child_registry(&mut self, registry: LspChildRegistry) {
263 self.child_registry = registry;
264 }
265
266 pub fn set_extra_env(&mut self, key: &str, value: &str) {
270 self.extra_env.insert(key.to_string(), value.to_string());
271 }
272
273 pub fn server_count(&self) -> usize {
275 self.clients.len()
276 }
277
278 pub fn override_binary(&mut self, kind: ServerKind, binary_path: PathBuf) {
280 self.binary_overrides.insert(kind, binary_path);
281 }
282
283 pub fn ensure_server_for_file(&mut self, file_path: &Path, config: &Config) -> Vec<ServerKey> {
290 self.ensure_server_for_file_detailed(file_path, config)
291 .successful
292 }
293
294 pub fn ensure_server_for_file_detailed(
302 &mut self,
303 file_path: &Path,
304 config: &Config,
305 ) -> EnsureServerOutcomes {
306 let defs = servers_for_file(file_path, config);
307 let mut outcomes = EnsureServerOutcomes::default();
308
309 for def in defs {
310 let server_id = def.kind.id_str().to_string();
311 let server_name = def.name.to_string();
312
313 let Some(root) = find_workspace_root(file_path, &def.root_markers) else {
314 outcomes.attempts.push(ServerAttempt {
315 server_id,
316 server_name,
317 result: ServerAttemptResult::NoRootMarker {
318 looked_for: def.root_markers.iter().map(|s| s.to_string()).collect(),
319 },
320 });
321 continue;
322 };
323
324 let key = ServerKey {
325 kind: def.kind.clone(),
326 root,
327 };
328
329 if !self.clients.contains_key(&key) {
330 if let Some(cached) = self.failed_spawns.get(&key) {
337 outcomes.attempts.push(ServerAttempt {
338 server_id,
339 server_name,
340 result: cached.clone(),
341 });
342 continue;
343 }
344
345 match self.spawn_server(&def, &key.root, config) {
346 Ok(client) => {
347 self.clients.insert(key.clone(), client);
348 self.server_binaries.insert(key.clone(), def.binary.clone());
349 self.documents.entry(key.clone()).or_default();
350 }
351 Err(err) => {
352 slog_error!("failed to spawn {}: {}", def.name, err);
353 let result = classify_spawn_error(&def.binary, &err);
354 self.failed_spawns.insert(key.clone(), result.clone());
358 outcomes.attempts.push(ServerAttempt {
359 server_id,
360 server_name,
361 result,
362 });
363 continue;
364 }
365 }
366 }
367
368 outcomes.attempts.push(ServerAttempt {
369 server_id,
370 server_name,
371 result: ServerAttemptResult::Ok {
372 server_key: key.clone(),
373 },
374 });
375 outcomes.successful.push(key);
376 }
377
378 outcomes
379 }
380
381 pub fn ensure_server_for_file_default(&mut self, file_path: &Path) -> Vec<ServerKey> {
384 self.ensure_server_for_file(file_path, &Config::default())
385 }
386 pub fn ensure_file_open(
390 &mut self,
391 file_path: &Path,
392 config: &Config,
393 ) -> Result<Vec<ServerKey>, LspError> {
394 let canonical_path = canonicalize_for_lsp(file_path)?;
395 let server_keys = self.ensure_server_for_file(&canonical_path, config);
396 if server_keys.is_empty() {
397 return Ok(server_keys);
398 }
399
400 let uri = uri_for_path(&canonical_path)?;
401 let language_id = language_id_for_extension(
402 canonical_path
403 .extension()
404 .and_then(|ext| ext.to_str())
405 .unwrap_or_default(),
406 )
407 .to_string();
408
409 for key in &server_keys {
410 let already_open = self
411 .documents
412 .get(key)
413 .is_some_and(|store| store.is_open(&canonical_path));
414
415 if !already_open {
416 let content = std::fs::read_to_string(&canonical_path).map_err(LspError::Io)?;
417 if let Some(client) = self.clients.get_mut(key) {
418 client.send_notification::<DidOpenTextDocument>(DidOpenTextDocumentParams {
419 text_document: TextDocumentItem::new(
420 uri.clone(),
421 language_id.clone(),
422 0,
423 content,
424 ),
425 })?;
426 }
427 self.documents
428 .entry(key.clone())
429 .or_default()
430 .open(canonical_path.clone());
431 continue;
432 }
433
434 let drifted = self
444 .documents
445 .get(key)
446 .is_some_and(|store| store.is_stale_on_disk(&canonical_path));
447 if drifted {
448 let content = std::fs::read_to_string(&canonical_path).map_err(LspError::Io)?;
449 let next_version = self
450 .documents
451 .get(key)
452 .and_then(|store| store.version(&canonical_path))
453 .map(|v| v + 1)
454 .unwrap_or(1);
455 if let Some(client) = self.clients.get_mut(key) {
456 client.send_notification::<DidChangeTextDocument>(
457 DidChangeTextDocumentParams {
458 text_document: VersionedTextDocumentIdentifier::new(
459 uri.clone(),
460 next_version,
461 ),
462 content_changes: vec![TextDocumentContentChangeEvent {
463 range: None,
464 range_length: None,
465 text: content,
466 }],
467 },
468 )?;
469 }
470 if let Some(store) = self.documents.get_mut(key) {
471 store.bump_version(&canonical_path);
472 }
473 }
474 }
475
476 Ok(server_keys)
477 }
478
479 pub fn ensure_file_open_default(
480 &mut self,
481 file_path: &Path,
482 ) -> Result<Vec<ServerKey>, LspError> {
483 self.ensure_file_open(file_path, &Config::default())
484 }
485
486 pub fn notify_file_changed(
492 &mut self,
493 file_path: &Path,
494 content: &str,
495 config: &Config,
496 ) -> Result<(), LspError> {
497 self.notify_file_changed_versioned(file_path, content, config)
498 .map(|_| ())
499 }
500
501 pub fn notify_file_changed_versioned(
512 &mut self,
513 file_path: &Path,
514 content: &str,
515 config: &Config,
516 ) -> Result<Vec<(ServerKey, i32)>, LspError> {
517 let canonical_path = canonicalize_for_lsp(file_path)?;
518 let server_keys = self.ensure_server_for_file(&canonical_path, config);
519 if server_keys.is_empty() {
520 return Ok(Vec::new());
521 }
522
523 let uri = uri_for_path(&canonical_path)?;
524 let language_id = language_id_for_extension(
525 canonical_path
526 .extension()
527 .and_then(|ext| ext.to_str())
528 .unwrap_or_default(),
529 )
530 .to_string();
531
532 let mut versions: Vec<(ServerKey, i32)> = Vec::with_capacity(server_keys.len());
533
534 for key in server_keys {
535 let current_version = self
536 .documents
537 .get(&key)
538 .and_then(|store| store.version(&canonical_path));
539
540 if let Some(version) = current_version {
541 let next_version = version + 1;
542 if let Some(client) = self.clients.get_mut(&key) {
543 client.send_notification::<DidChangeTextDocument>(
544 DidChangeTextDocumentParams {
545 text_document: VersionedTextDocumentIdentifier::new(
546 uri.clone(),
547 next_version,
548 ),
549 content_changes: vec![TextDocumentContentChangeEvent {
550 range: None,
551 range_length: None,
552 text: content.to_string(),
553 }],
554 },
555 )?;
556 }
557 if let Some(store) = self.documents.get_mut(&key) {
558 store.bump_version(&canonical_path);
559 }
560 versions.push((key, next_version));
561 continue;
562 }
563
564 if let Some(client) = self.clients.get_mut(&key) {
565 client.send_notification::<DidOpenTextDocument>(DidOpenTextDocumentParams {
566 text_document: TextDocumentItem::new(
567 uri.clone(),
568 language_id.clone(),
569 0,
570 content.to_string(),
571 ),
572 })?;
573 }
574 self.documents
575 .entry(key.clone())
576 .or_default()
577 .open(canonical_path.clone());
578 versions.push((key, 0));
581 }
582
583 Ok(versions)
584 }
585
586 pub fn notify_file_changed_default(
587 &mut self,
588 file_path: &Path,
589 content: &str,
590 ) -> Result<(), LspError> {
591 self.notify_file_changed(file_path, content, &Config::default())
592 }
593
594 pub fn notify_files_watched_changed(
600 &mut self,
601 paths: &[(PathBuf, FileChangeType)],
602 _config: &Config,
603 ) -> Result<(), LspError> {
604 if paths.is_empty() {
605 return Ok(());
606 }
607
608 let mut canonical_events = Vec::with_capacity(paths.len());
609 for (path, typ) in paths {
610 let canonical_path = resolve_for_lsp_uri(path);
611 canonical_events.push((canonical_path, *typ));
612 }
613
614 let keys: Vec<ServerKey> = self.clients.keys().cloned().collect();
615 for key in keys {
616 let mut changes = Vec::new();
617 for (path, typ) in &canonical_events {
618 if !path.starts_with(&key.root) {
619 continue;
620 }
621 changes.push(FileEvent::new(uri_for_path(path)?, *typ));
622 }
623
624 if changes.is_empty() {
625 continue;
626 }
627
628 if let Some(client) = self.clients.get_mut(&key) {
629 let supports_static_watched_files = client.supports_watched_files();
635 let has_dynamic_registration = client.has_watched_file_registration();
636 if !(supports_static_watched_files || has_dynamic_registration) {
637 if self.watched_file_skip_logged.insert(key.clone()) {
638 log::debug!(
639 "skipping didChangeWatchedFiles for {:?} (not supported or registered)",
640 key
641 );
642 }
643 continue;
644 }
645 client.send_notification::<DidChangeWatchedFiles>(DidChangeWatchedFilesParams {
646 changes,
647 })?;
648 }
649 }
650
651 Ok(())
652 }
653
654 pub fn notify_file_closed(&mut self, file_path: &Path) -> Result<(), LspError> {
656 let canonical_path = canonicalize_for_lsp(file_path)?;
657 let uri = uri_for_path(&canonical_path)?;
658 let keys: Vec<ServerKey> = self.documents.keys().cloned().collect();
659
660 for key in keys {
661 let was_open = self
662 .documents
663 .get(&key)
664 .map(|store| store.is_open(&canonical_path))
665 .unwrap_or(false);
666 if !was_open {
667 continue;
668 }
669
670 if let Some(client) = self.clients.get_mut(&key) {
671 client.send_notification::<DidCloseTextDocument>(DidCloseTextDocumentParams {
672 text_document: TextDocumentIdentifier::new(uri.clone()),
673 })?;
674 }
675
676 if let Some(store) = self.documents.get_mut(&key) {
677 store.close(&canonical_path);
678 }
679 self.diagnostics
680 .clear_for_server_file(&key, &canonical_path);
681 }
682
683 Ok(())
684 }
685
686 pub fn client_for_file(&self, file_path: &Path, config: &Config) -> Option<&LspClient> {
688 let key = self.server_key_for_file(file_path, config)?;
689 self.clients.get(&key)
690 }
691
692 pub fn client_for_file_default(&self, file_path: &Path) -> Option<&LspClient> {
693 self.client_for_file(file_path, &Config::default())
694 }
695
696 pub fn client_for_file_mut(
698 &mut self,
699 file_path: &Path,
700 config: &Config,
701 ) -> Option<&mut LspClient> {
702 let key = self.server_key_for_file(file_path, config)?;
703 self.clients.get_mut(&key)
704 }
705
706 pub fn client_for_file_mut_default(&mut self, file_path: &Path) -> Option<&mut LspClient> {
707 self.client_for_file_mut(file_path, &Config::default())
708 }
709
710 pub fn active_client_count(&self) -> usize {
712 self.clients.len()
713 }
714
715 pub fn drain_events(&mut self) -> Vec<LspEvent> {
717 let mut events = Vec::new();
718 while let Ok(event) = self.event_rx.try_recv() {
719 self.handle_event(&event);
720 events.push(event);
721 }
722 events
723 }
724
725 pub fn wait_for_diagnostics(
727 &mut self,
728 file_path: &Path,
729 config: &Config,
730 timeout: std::time::Duration,
731 ) -> Vec<StoredDiagnostic> {
732 let deadline = std::time::Instant::now() + timeout;
733 self.wait_for_file_diagnostics(file_path, config, deadline)
734 }
735
736 pub fn wait_for_diagnostics_default(
737 &mut self,
738 file_path: &Path,
739 timeout: std::time::Duration,
740 ) -> Vec<StoredDiagnostic> {
741 self.wait_for_diagnostics(file_path, &Config::default(), timeout)
742 }
743
744 #[doc(hidden)]
749 pub fn diagnostics_store_for_test(&self) -> &DiagnosticsStore {
750 &self.diagnostics
751 }
752
753 pub fn snapshot_diagnostic_epochs(&self, file_path: &Path) -> HashMap<ServerKey, u64> {
758 let lookup_path = normalize_lookup_path(file_path);
759 self.diagnostics
760 .entries_for_file(&lookup_path)
761 .into_iter()
762 .map(|(key, entry)| (key.clone(), entry.epoch))
763 .collect()
764 }
765
766 pub fn snapshot_pre_edit_state(&self, file_path: &Path) -> HashMap<ServerKey, PreEditSnapshot> {
769 let lookup_path = normalize_lookup_path(file_path);
770 let mut snapshots: HashMap<ServerKey, PreEditSnapshot> = self
771 .diagnostics
772 .entries_for_file(&lookup_path)
773 .into_iter()
774 .map(|(key, entry)| {
775 (
776 key.clone(),
777 PreEditSnapshot {
778 epoch: entry.epoch,
779 document_version_at_capture: None,
780 },
781 )
782 })
783 .collect();
784
785 for (key, store) in &self.documents {
786 if let Some(version) = store.version(&lookup_path) {
787 snapshots
788 .entry(key.clone())
789 .or_default()
790 .document_version_at_capture = Some(version);
791 }
792 }
793
794 snapshots
795 }
796
797 pub fn diagnostic_entry_is_fresh_for_document(
805 &self,
806 file_path: &Path,
807 server_key: &ServerKey,
808 pre: PreEditSnapshot,
809 ) -> bool {
810 let lookup_path = normalize_lookup_path(file_path);
811 let Some(entry) = self
812 .diagnostics
813 .entries_for_file(&lookup_path)
814 .into_iter()
815 .find_map(|(key, entry)| if key == server_key { Some(entry) } else { None })
816 else {
817 return false;
818 };
819
820 let target_version = self
821 .documents
822 .get(server_key)
823 .and_then(|store| store.version(&lookup_path))
824 .or(pre.document_version_at_capture)
825 .unwrap_or(0);
826
827 matches!(entry.version, Some(version) if version >= target_version)
828 }
829
830 pub fn wait_for_post_edit_diagnostics(
853 &mut self,
854 file_path: &Path,
855 _config: &Config,
859 expected_versions: &[(ServerKey, i32)],
860 pre_snapshot: &HashMap<ServerKey, PreEditSnapshot>,
861 timeout: std::time::Duration,
862 ) -> PostEditWaitOutcome {
863 let lookup_path = normalize_lookup_path(file_path);
864 let deadline = std::time::Instant::now() + timeout;
865
866 let _ = self.drain_events_for_file(&lookup_path);
871
872 let mut fresh: HashMap<ServerKey, Vec<StoredDiagnostic>> = HashMap::new();
873 let mut exited: Vec<ServerKey> = Vec::new();
874
875 loop {
876 for (key, target_version) in expected_versions {
884 if fresh.contains_key(key) || exited.contains(key) {
885 continue;
886 }
887 if !self.clients.contains_key(key) {
888 exited.push(key.clone());
889 continue;
890 }
891 if let Some(entry) = self
892 .diagnostics
893 .entries_for_file(&lookup_path)
894 .into_iter()
895 .find_map(|(k, e)| if k == key { Some(e) } else { None })
896 {
897 let pre = pre_snapshot.get(key).copied().unwrap_or_default();
898 let is_fresh = post_edit_entry_is_fresh(entry, *target_version, pre);
899 if is_fresh {
900 fresh.insert(key.clone(), entry.diagnostics.clone());
901 }
902 }
903 }
904
905 if fresh.len() + exited.len() == expected_versions.len() {
907 break;
908 }
909
910 let now = std::time::Instant::now();
911 if now >= deadline {
912 break;
913 }
914
915 let timeout = deadline.saturating_duration_since(now);
916 match self.event_rx.recv_timeout(timeout) {
917 Ok(event) => {
918 self.handle_event(&event);
919 }
920 Err(RecvTimeoutError::Timeout) | Err(RecvTimeoutError::Disconnected) => break,
921 }
922 }
923
924 let pending: Vec<ServerKey> = expected_versions
926 .iter()
927 .filter(|(k, _)| !fresh.contains_key(k) && !exited.contains(k))
928 .map(|(k, _)| k.clone())
929 .collect();
930
931 let mut diagnostics: Vec<StoredDiagnostic> = fresh
934 .into_iter()
935 .flat_map(|(_, diags)| diags.into_iter())
936 .collect();
937 diagnostics.sort_by(|a, b| {
938 a.file
939 .cmp(&b.file)
940 .then(a.line.cmp(&b.line))
941 .then(a.column.cmp(&b.column))
942 .then(a.message.cmp(&b.message))
943 });
944
945 PostEditWaitOutcome {
946 diagnostics,
947 pending_servers: pending,
948 exited_servers: exited,
949 }
950 }
951
952 pub fn wait_for_file_diagnostics(
958 &mut self,
959 file_path: &Path,
960 config: &Config,
961 deadline: std::time::Instant,
962 ) -> Vec<StoredDiagnostic> {
963 let lookup_path = normalize_lookup_path(file_path);
964
965 if self.server_key_for_file(&lookup_path, config).is_none() {
966 return Vec::new();
967 }
968
969 loop {
970 if self.drain_events_for_file(&lookup_path) {
971 break;
972 }
973
974 let now = std::time::Instant::now();
975 if now >= deadline {
976 break;
977 }
978
979 let timeout = deadline.saturating_duration_since(now);
980 match self.event_rx.recv_timeout(timeout) {
981 Ok(event) => {
982 if matches!(
983 self.handle_event(&event),
984 Some(ref published_file) if published_file.as_path() == lookup_path.as_path()
985 ) {
986 break;
987 }
988 }
989 Err(RecvTimeoutError::Timeout) | Err(RecvTimeoutError::Disconnected) => break,
990 }
991 }
992
993 self.get_diagnostics_for_file(&lookup_path)
994 .into_iter()
995 .cloned()
996 .collect()
997 }
998
999 pub const PULL_FILE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
1005
1006 pub fn pull_file_timeout() -> std::time::Duration {
1008 Self::PULL_FILE_TIMEOUT
1009 }
1010
1011 const PULL_WORKSPACE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
1015
1016 pub fn pull_file_diagnostics(
1027 &mut self,
1028 file_path: &Path,
1029 config: &Config,
1030 ) -> Result<Vec<PullFileResult>, LspError> {
1031 let canonical_path = canonicalize_for_lsp(file_path)?;
1032 self.ensure_file_open(&canonical_path, config)?;
1035
1036 let server_keys = self.ensure_server_for_file(&canonical_path, config);
1037 if server_keys.is_empty() {
1038 return Ok(Vec::new());
1039 }
1040
1041 let uri = uri_for_path(&canonical_path)?;
1042 let mut results = Vec::with_capacity(server_keys.len());
1043
1044 for key in server_keys {
1045 let supports_pull = self
1046 .clients
1047 .get(&key)
1048 .and_then(|c| c.diagnostic_capabilities())
1049 .is_some_and(|caps| caps.pull_diagnostics);
1050
1051 if !supports_pull {
1052 results.push(PullFileResult {
1053 server_key: key.clone(),
1054 outcome: PullFileOutcome::PullNotSupported,
1055 });
1056 continue;
1057 }
1058
1059 let previous_result_id = self
1061 .diagnostics
1062 .entries_for_file(&canonical_path)
1063 .into_iter()
1064 .find(|(k, _)| **k == key)
1065 .and_then(|(_, entry)| entry.result_id.clone());
1066
1067 let identifier = self
1068 .clients
1069 .get(&key)
1070 .and_then(|c| c.diagnostic_capabilities())
1071 .and_then(|caps| caps.identifier.clone());
1072
1073 let params = AftDocumentDiagnosticParams {
1074 text_document: lsp_types::TextDocumentIdentifier { uri: uri.clone() },
1075 identifier,
1076 previous_result_id,
1077 work_done_progress_params: Default::default(),
1078 partial_result_params: Default::default(),
1079 };
1080
1081 let outcome = match self.send_pull_request(&key, params) {
1082 Ok(report) => self.ingest_document_report(&key, &canonical_path, report),
1083 Err(err) => {
1084 if let Some(result) = self.cache_post_initialize_exit(&key, &err) {
1085 PullFileOutcome::RequestFailed {
1086 reason: server_attempt_result_reason(&result),
1087 }
1088 } else if recoverable_pull_rejection(&err)
1089 && self.clients.get(&key).is_some_and(|client| {
1090 matches!(
1091 client.state(),
1092 ServerState::Ready | ServerState::Initializing
1093 )
1094 })
1095 {
1096 PullFileOutcome::RequestFailed {
1097 reason: format!("pull_rejected_push_fallback: {err}"),
1098 }
1099 } else {
1100 PullFileOutcome::RequestFailed {
1101 reason: err.to_string(),
1102 }
1103 }
1104 }
1105 };
1106
1107 results.push(PullFileResult {
1108 server_key: key,
1109 outcome,
1110 });
1111 }
1112
1113 Ok(results)
1114 }
1115
1116 pub fn pull_workspace_diagnostics(
1121 &mut self,
1122 server_key: &ServerKey,
1123 timeout: Option<std::time::Duration>,
1124 ) -> Result<PullWorkspaceResult, LspError> {
1125 let timeout = timeout.unwrap_or(Self::PULL_WORKSPACE_TIMEOUT);
1126
1127 let supports_workspace = self
1128 .clients
1129 .get(server_key)
1130 .and_then(|c| c.diagnostic_capabilities())
1131 .is_some_and(|caps| caps.workspace_diagnostics);
1132
1133 if !supports_workspace {
1134 return Ok(PullWorkspaceResult {
1135 server_key: server_key.clone(),
1136 files_reported: Vec::new(),
1137 complete: false,
1138 cancelled: false,
1139 supports_workspace: false,
1140 });
1141 }
1142
1143 let identifier = self
1144 .clients
1145 .get(server_key)
1146 .and_then(|c| c.diagnostic_capabilities())
1147 .and_then(|caps| caps.identifier.clone());
1148
1149 let params = AftWorkspaceDiagnosticParams {
1150 identifier,
1151 previous_result_ids: Vec::new(),
1152 work_done_progress_params: Default::default(),
1153 partial_result_params: Default::default(),
1154 };
1155
1156 let result = match self
1157 .clients
1158 .get_mut(server_key)
1159 .ok_or_else(|| LspError::ServerNotReady("server not found".into()))?
1160 .send_request_with_timeout::<AftWorkspaceDiagnosticRequest>(params, timeout)
1161 {
1162 Ok(result) => result,
1163 Err(LspError::Timeout(_)) => {
1164 return Ok(PullWorkspaceResult {
1165 server_key: server_key.clone(),
1166 files_reported: Vec::new(),
1167 complete: false,
1168 cancelled: true,
1169 supports_workspace: true,
1170 });
1171 }
1172 Err(err) => {
1173 if let Some(result) = self.cache_post_initialize_exit(server_key, &err) {
1174 return Err(LspError::ServerNotReady(server_attempt_result_reason(
1175 &result,
1176 )));
1177 }
1178 return Err(err);
1179 }
1180 };
1181
1182 let (items, complete) = match result {
1186 lsp_types::WorkspaceDiagnosticReportResult::Report(report) => (report.items, true),
1187 lsp_types::WorkspaceDiagnosticReportResult::Partial(partial) => (partial.items, false),
1188 };
1189
1190 let mut files_reported = Vec::with_capacity(items.len());
1192 for item in items {
1193 match item {
1194 lsp_types::WorkspaceDocumentDiagnosticReport::Full(full) => {
1195 if let Some(file) = uri_to_path(&full.uri) {
1196 let stored = from_lsp_diagnostics(
1197 file.clone(),
1198 full.full_document_diagnostic_report.items.clone(),
1199 );
1200 self.diagnostics.publish_with_result_id(
1201 server_key.clone(),
1202 file.clone(),
1203 stored,
1204 full.full_document_diagnostic_report.result_id.clone(),
1205 );
1206 files_reported.push(file);
1207 }
1208 }
1209 lsp_types::WorkspaceDocumentDiagnosticReport::Unchanged(_unchanged) => {
1210 }
1213 }
1214 }
1215
1216 Ok(PullWorkspaceResult {
1217 server_key: server_key.clone(),
1218 files_reported,
1219 complete,
1220 cancelled: false,
1221 supports_workspace: true,
1222 })
1223 }
1224
1225 fn cache_post_initialize_exit(
1226 &mut self,
1227 key: &ServerKey,
1228 err: &LspError,
1229 ) -> Option<ServerAttemptResult> {
1230 let binary = self
1231 .server_binaries
1232 .get(key)
1233 .cloned()
1234 .unwrap_or_else(|| key.kind.id_str().to_string());
1235 let (status, stderr_tail) = {
1236 let client = self.clients.get_mut(key)?;
1237 let mut status = client.child_exit_status();
1238 for _ in 0..10 {
1239 if status.is_some() {
1240 break;
1241 }
1242 std::thread::sleep(std::time::Duration::from_millis(10));
1243 status = client.child_exit_status();
1244 }
1245 let status = status?;
1246 wait_for_stderr_tail(client);
1247 (status, client.stderr_tail())
1248 };
1249 let reason = format_post_initialize_exit_reason(&binary, status, &stderr_tail, err);
1250 let result = ServerAttemptResult::SpawnFailed { binary, reason };
1251 self.clients.remove(key);
1252 self.server_binaries.remove(key);
1253 self.documents.remove(key);
1254 self.diagnostics.clear_for_server(key);
1255 self.failed_spawns.insert(key.clone(), result.clone());
1256 Some(result)
1257 }
1258
1259 fn send_pull_request(
1261 &mut self,
1262 key: &ServerKey,
1263 params: AftDocumentDiagnosticParams,
1264 ) -> Result<lsp_types::DocumentDiagnosticReportResult, LspError> {
1265 let client = self
1266 .clients
1267 .get_mut(key)
1268 .ok_or_else(|| LspError::ServerNotReady("server not found".into()))?;
1269 client.send_request::<AftDocumentDiagnosticRequest>(params)
1270 }
1271
1272 fn ingest_document_report(
1275 &mut self,
1276 key: &ServerKey,
1277 canonical_path: &Path,
1278 result: lsp_types::DocumentDiagnosticReportResult,
1279 ) -> PullFileOutcome {
1280 let report = match result {
1281 lsp_types::DocumentDiagnosticReportResult::Report(report) => report,
1282 lsp_types::DocumentDiagnosticReportResult::Partial(_) => {
1283 return PullFileOutcome::PartialNotSupported;
1287 }
1288 };
1289
1290 match report {
1291 lsp_types::DocumentDiagnosticReport::Full(full) => {
1292 let result_id = full.full_document_diagnostic_report.result_id.clone();
1293 let stored = from_lsp_diagnostics(
1294 canonical_path.to_path_buf(),
1295 full.full_document_diagnostic_report.items.clone(),
1296 );
1297 let count = stored.len();
1298 self.diagnostics.publish_with_result_id(
1299 key.clone(),
1300 canonical_path.to_path_buf(),
1301 stored,
1302 result_id,
1303 );
1304 PullFileOutcome::Full {
1305 diagnostic_count: count,
1306 }
1307 }
1308 lsp_types::DocumentDiagnosticReport::Unchanged(_unchanged) => {
1309 if self
1313 .diagnostics
1314 .has_report_for_server_file(key, canonical_path)
1315 {
1316 PullFileOutcome::Unchanged
1317 } else {
1318 PullFileOutcome::RequestFailed {
1319 reason: "no_cache_for_unchanged".to_string(),
1320 }
1321 }
1322 }
1323 }
1324 }
1325
1326 pub fn shutdown_all(&mut self) {
1328 for (key, mut client) in self.clients.drain() {
1329 if let Err(err) = client.shutdown() {
1330 slog_error!("error shutting down {:?}: {}", key, err);
1331 }
1332 }
1333 self.server_binaries.clear();
1334 self.documents.clear();
1335 self.diagnostics = DiagnosticsStore::new();
1336 }
1337
1338 pub fn has_active_servers(&self) -> bool {
1340 self.clients
1341 .values()
1342 .any(|client| client.state() == ServerState::Ready)
1343 }
1344
1345 pub fn active_server_keys(&self) -> Vec<ServerKey> {
1348 self.clients.keys().cloned().collect()
1349 }
1350
1351 pub fn get_diagnostics_for_file(&self, file: &Path) -> Vec<&StoredDiagnostic> {
1352 let normalized = normalize_lookup_path(file);
1353 self.diagnostics.for_file(&normalized)
1354 }
1355
1356 pub fn get_diagnostics_for_directory(&self, dir: &Path) -> Vec<&StoredDiagnostic> {
1357 let normalized = normalize_lookup_path(dir);
1358 self.diagnostics.for_directory(&normalized)
1359 }
1360
1361 pub fn get_all_diagnostics(&self) -> Vec<&StoredDiagnostic> {
1362 self.diagnostics.all()
1363 }
1364
1365 pub fn has_any_diagnostic_reports(&self) -> bool {
1370 !self.diagnostics.is_empty()
1371 }
1372
1373 pub fn has_diagnostic_report_for_file(&self, file: &Path) -> bool {
1376 let normalized = normalize_lookup_path(file);
1377 self.diagnostics.has_any_report_for_file(&normalized)
1378 }
1379
1380 pub fn has_diagnostic_report_for_server_file(&self, server: &ServerKey, file: &Path) -> bool {
1383 let normalized = normalize_lookup_path(file);
1384 self.diagnostics
1385 .has_report_for_server_file(server, &normalized)
1386 }
1387
1388 fn drain_events_for_file(&mut self, file_path: &Path) -> bool {
1389 let mut saw_file_diagnostics = false;
1390 while let Ok(event) = self.event_rx.try_recv() {
1391 if matches!(
1392 self.handle_event(&event),
1393 Some(ref published_file) if published_file.as_path() == file_path
1394 ) {
1395 saw_file_diagnostics = true;
1396 }
1397 }
1398 saw_file_diagnostics
1399 }
1400
1401 fn handle_event(&mut self, event: &LspEvent) -> Option<PathBuf> {
1402 match event {
1403 LspEvent::Notification {
1404 server_kind,
1405 root,
1406 method,
1407 params: Some(params),
1408 } if method == "textDocument/publishDiagnostics" => {
1409 self.handle_publish_diagnostics(server_kind.clone(), root.clone(), params)
1410 }
1411 LspEvent::ServerExited { server_kind, root } => {
1412 let key = ServerKey {
1413 kind: server_kind.clone(),
1414 root: root.clone(),
1415 };
1416 self.clients.remove(&key);
1417 self.server_binaries.remove(&key);
1418 self.documents.remove(&key);
1419 self.diagnostics.clear_for_server(&key);
1420 None
1421 }
1422 _ => None,
1423 }
1424 }
1425
1426 fn handle_publish_diagnostics(
1427 &mut self,
1428 server: ServerKind,
1429 root: PathBuf,
1430 params: &serde_json::Value,
1431 ) -> Option<PathBuf> {
1432 if let Ok(publish_params) =
1433 serde_json::from_value::<lsp_types::PublishDiagnosticsParams>(params.clone())
1434 {
1435 let file = uri_to_path(&publish_params.uri)?;
1436 let stored = from_lsp_diagnostics(file.clone(), publish_params.diagnostics);
1437 let key = ServerKey { kind: server, root };
1443 self.diagnostics
1444 .publish_full(key, file.clone(), stored, None, publish_params.version);
1445 return Some(file);
1446 }
1447 None
1448 }
1449
1450 fn spawn_server(
1451 &self,
1452 def: &ServerDef,
1453 root: &Path,
1454 config: &Config,
1455 ) -> Result<LspClient, LspError> {
1456 let binary = self.resolve_binary(def, config)?;
1457
1458 let mut merged_env = def.env.clone();
1462 for (key, value) in &self.extra_env {
1463 merged_env.insert(key.clone(), value.clone());
1464 }
1465
1466 let mut client = LspClient::spawn(
1467 def.kind.clone(),
1468 root.to_path_buf(),
1469 &binary,
1470 &def.args,
1471 &merged_env,
1472 self.event_tx.clone(),
1473 self.child_registry.clone(),
1474 )?;
1475 if let Err(err) = client.initialize(root, def.initialization_options.clone()) {
1476 wait_for_stderr_tail(&mut client);
1477 let stderr_tail = client.stderr_tail();
1478 let reason = if client.child_exited() || !stderr_tail.is_empty() {
1479 format_initialize_failure_reason(&def.binary, &stderr_tail, &err)
1480 } else {
1481 format!("server failed during initialize: {err}")
1482 };
1483 return Err(LspError::ServerNotReady(reason));
1484 }
1485 Ok(client)
1486 }
1487
1488 fn resolve_binary(&self, def: &ServerDef, config: &Config) -> Result<PathBuf, LspError> {
1489 if let Some(path) = self.binary_overrides.get(&def.kind) {
1490 if path.exists() {
1491 return Ok(path.clone());
1492 }
1493 return Err(LspError::NotFound(format!(
1494 "override binary for {:?} not found: {}",
1495 def.kind,
1496 path.display()
1497 )));
1498 }
1499
1500 if let Some(path) = env_binary_override(&def.kind) {
1501 if path.exists() {
1502 return Ok(path);
1503 }
1504 return Err(LspError::NotFound(format!(
1505 "environment override binary for {:?} not found: {}",
1506 def.kind,
1507 path.display()
1508 )));
1509 }
1510
1511 resolve_lsp_binary(
1516 &def.binary,
1517 config.project_root.as_deref(),
1518 &config.lsp_paths_extra,
1519 )
1520 .ok_or_else(|| {
1521 LspError::NotFound(format!(
1522 "language server binary '{}' not found in node_modules/.bin, lsp_paths_extra, or PATH",
1523 def.binary
1524 ))
1525 })
1526 }
1527
1528 fn server_key_for_file(&self, file_path: &Path, config: &Config) -> Option<ServerKey> {
1529 for def in servers_for_file(file_path, config) {
1530 let root = find_workspace_root(file_path, &def.root_markers)?;
1531 let key = ServerKey {
1532 kind: def.kind.clone(),
1533 root,
1534 };
1535 if self.clients.contains_key(&key) {
1536 return Some(key);
1537 }
1538 }
1539 None
1540 }
1541}
1542
1543impl Default for LspManager {
1544 fn default() -> Self {
1545 Self::new()
1546 }
1547}
1548
1549fn wait_for_stderr_tail(client: &mut LspClient) {
1550 for _ in 0..10 {
1551 if !client.stderr_tail().is_empty() {
1552 break;
1553 }
1554 std::thread::sleep(std::time::Duration::from_millis(10));
1555 }
1556}
1557
1558fn recoverable_pull_rejection(err: &LspError) -> bool {
1559 matches!(
1560 err,
1561 LspError::ServerError {
1562 code: -32601 | -32602,
1563 ..
1564 }
1565 )
1566}
1567
1568fn server_attempt_result_reason(result: &ServerAttemptResult) -> String {
1569 match result {
1570 ServerAttemptResult::SpawnFailed { binary, reason } => {
1571 format!("spawn_failed: {binary} ({reason})")
1572 }
1573 ServerAttemptResult::BinaryNotInstalled { binary } => {
1574 format!("binary_not_installed: {binary}")
1575 }
1576 ServerAttemptResult::NoRootMarker { looked_for } => {
1577 format!("no_root_marker (looked for: {})", looked_for.join(", "))
1578 }
1579 ServerAttemptResult::Ok { .. } => "ok".to_string(),
1580 }
1581}
1582
1583fn format_stderr_tail_for_reason(stderr_tail: &str) -> String {
1584 truncate_stderr_tail_for_reason(stderr_tail)
1585 .lines()
1586 .map(|line| format!(" {line}"))
1587 .collect::<Vec<_>>()
1588 .join("\n")
1589}
1590
1591fn truncate_stderr_tail_for_reason(stderr_tail: &str) -> String {
1592 if stderr_tail.len() <= STDERR_REASON_BYTES {
1593 return stderr_tail.to_string();
1594 }
1595
1596 let ellipsis = "...";
1597 let target_len = STDERR_REASON_BYTES.saturating_sub(ellipsis.len());
1598 let mut start = stderr_tail.len() - target_len;
1599 while start < stderr_tail.len() && !stderr_tail.is_char_boundary(start) {
1600 start += 1;
1601 }
1602 format!("{ellipsis}{}", &stderr_tail[start..])
1603}
1604
1605fn format_initialize_failure_reason(binary: &str, stderr_tail: &str, err: &LspError) -> String {
1606 let mut reason = format!("server crashed during initialize: {err}");
1607 if !stderr_tail.is_empty() {
1608 reason.push_str("; stderr (last 64 lines):\n");
1609 reason.push_str(&format_stderr_tail_for_reason(stderr_tail));
1610 reason.push_str("\n\n");
1611 reason.push_str(&failure_hint(binary, stderr_tail));
1612 }
1613 reason
1614}
1615
1616fn format_post_initialize_exit_reason(
1617 binary: &str,
1618 status: std::process::ExitStatus,
1619 stderr_tail: &str,
1620 err: &LspError,
1621) -> String {
1622 let code = status
1623 .code()
1624 .map(|c| c.to_string())
1625 .unwrap_or_else(|| "signal/unknown".to_string());
1626 let mut reason = format!("server exited after initialize (code {code}): {err}");
1627 if !stderr_tail.is_empty() {
1628 reason.push_str("; stderr (last 64 lines):\n");
1629 reason.push_str(&format_stderr_tail_for_reason(stderr_tail));
1630 reason.push_str("\n\n");
1631 reason.push_str(&failure_hint(binary, stderr_tail));
1632 }
1633 reason
1634}
1635
1636fn failure_hint(binary: &str, stderr_tail: &str) -> String {
1637 if stderr_tail.contains("MODULE_NOT_FOUND") || stderr_tail.contains("Cannot find module") {
1638 let package_manager = infer_package_manager(stderr_tail);
1639 format!(
1640 "Your package-manager shim resolves to a missing file. Try reinstalling: {package_manager} install -g {binary} --force. Common cause: hard-link breakage from fs migration or store prune."
1641 )
1642 } else {
1643 format!("Hint: see stderr above for '{binary}' failure details.")
1644 }
1645}
1646
1647fn infer_package_manager(stderr_tail: &str) -> &'static str {
1648 let lower = stderr_tail.to_ascii_lowercase();
1649 if lower.contains(".pnpm/") || lower.contains(".pnpm\\") || lower.contains("/pnpm/") {
1650 "pnpm"
1651 } else if lower.contains(".yarn/")
1652 || lower.contains(".yarn\\")
1653 || lower.contains("/yarn/")
1654 || lower.contains("yarn")
1655 {
1656 "yarn"
1657 } else {
1658 "npm"
1659 }
1660}
1661
1662fn canonicalize_for_lsp(file_path: &Path) -> Result<PathBuf, LspError> {
1663 std::fs::canonicalize(file_path).map_err(LspError::from)
1664}
1665
1666fn resolve_for_lsp_uri(file_path: &Path) -> PathBuf {
1667 if let Ok(path) = std::fs::canonicalize(file_path) {
1668 return path;
1669 }
1670
1671 let mut existing = file_path.to_path_buf();
1672 let mut missing = Vec::new();
1673 while !existing.exists() {
1674 let Some(name) = existing.file_name() else {
1675 break;
1676 };
1677 missing.push(name.to_owned());
1678 let Some(parent) = existing.parent() else {
1679 break;
1680 };
1681 existing = parent.to_path_buf();
1682 }
1683
1684 let mut resolved = std::fs::canonicalize(&existing).unwrap_or(existing);
1685 for segment in missing.into_iter().rev() {
1686 resolved.push(segment);
1687 }
1688 resolved
1689}
1690
1691fn language_id_for_extension(ext: &str) -> &'static str {
1692 match ext {
1693 "ts" => "typescript",
1694 "tsx" => "typescriptreact",
1695 "js" | "mjs" | "cjs" => "javascript",
1696 "jsx" => "javascriptreact",
1697 "py" | "pyi" => "python",
1698 "rs" => "rust",
1699 "go" => "go",
1700 "html" | "htm" => "html",
1701 _ => "plaintext",
1702 }
1703}
1704
1705fn normalize_lookup_path(path: &Path) -> PathBuf {
1706 std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf())
1707}
1708
1709fn classify_spawn_error(binary: &str, err: &LspError) -> ServerAttemptResult {
1716 match err {
1717 LspError::NotFound(_) => ServerAttemptResult::BinaryNotInstalled {
1722 binary: binary.to_string(),
1723 },
1724 other => ServerAttemptResult::SpawnFailed {
1725 binary: binary.to_string(),
1726 reason: other.to_string(),
1727 },
1728 }
1729}
1730
1731fn env_binary_override(kind: &ServerKind) -> Option<PathBuf> {
1732 let id = kind.id_str();
1733 let suffix: String = id
1734 .chars()
1735 .map(|ch| {
1736 if ch.is_ascii_alphanumeric() {
1737 ch.to_ascii_uppercase()
1738 } else {
1739 '_'
1740 }
1741 })
1742 .collect();
1743 let key = format!("AFT_LSP_{suffix}_BINARY");
1744 std::env::var_os(key).map(PathBuf::from)
1745}