diff --git a/src/sharplsp/src/project_edit.rs b/src/sharplsp/src/project_edit.rs index 53a1158d..9702a1c3 100644 --- a/src/sharplsp/src/project_edit.rs +++ b/src/sharplsp/src/project_edit.rs @@ -95,6 +95,15 @@ pub fn handle( Ok(serde_json::to_value(response)?) } +/// Reload the compilers that own `project` now, not when the platform watcher +/// notices the write: on Windows it can miss the host's own edit, and the new +/// file stays unowned. [SE-ADD-FILE] [DIAG-LSP-REFRESH] +fn announce_written(project: &str) { + if let Err(error) = crate::pull_diagnostics::monitor::written(&[project]) { + warn!(project, error = %error, "edited project could not be announced for reload"); + } +} + /// Time `run`, log its outcome as `operation`, and shape the response. fn respond( operation: &str, @@ -113,6 +122,9 @@ fn respond( ms = elapsed_ms(&started), "compile item edit complete" ); + if outcome.modified { + announce_written(project); + } CompileItemResponse { success: true, message: outcome.message, diff --git a/src/sharplsp/src/pull_diagnostics/monitor.rs b/src/sharplsp/src/pull_diagnostics/monitor.rs index 96ab54fe..a6f53ae8 100644 --- a/src/sharplsp/src/pull_diagnostics/monitor.rs +++ b/src/sharplsp/src/pull_diagnostics/monitor.rs @@ -144,6 +144,28 @@ async fn observe( } } +/// Announce files the host itself just wrote through the notification the native +/// watcher sends, so the compilers that own them reload even when the platform +/// watcher misses the host's own write. Before the dispatch queue exists there +/// is nobody to tell. [DIAG-LSP-REFRESH] +pub fn written(paths: &[&str]) -> anyhow::Result<()> { + let changes: Vec = paths + .iter() + .filter_map(|path| crate::paths::path_to_uri(path).ok()) + .map(|uri| json!({ "uri": uri, "type": 2 })) + .collect(); + let inbound = super::state::inbound()?; + if let Some(inbound) = inbound.filter(|_| !changes.is_empty()) { + if !notify_changes(&inbound, &changes, false) { + tracing::warn!( + count = changes.len(), + "written files not announced: dispatch queue closed" + ); + } + } + Ok(()) +} + /// Route native observations through the existing foreground notification queue. fn notify_changes(inbound: &Sender, changes: &[Value], rescan: bool) -> bool { let notification = Notification::new( diff --git a/src/sharplsp/src/pull_diagnostics/state.rs b/src/sharplsp/src/pull_diagnostics/state.rs index ba7153c8..4ac6037f 100644 --- a/src/sharplsp/src/pull_diagnostics/state.rs +++ b/src/sharplsp/src/pull_diagnostics/state.rs @@ -132,6 +132,11 @@ pub fn connect_inputs( }) } +/// The foreground dispatch queue, once the connection has created it. +pub(super) fn inbound() -> Result>> { + with_session(|session| Ok(session.inbound.clone())) +} + /// The actual selected solution can live outside the editor's workspace folder. pub fn workspace() -> Result> { with_session(|session| Ok(session.workspace.clone())) @@ -380,6 +385,10 @@ fn analyzer( #[path = "release_tests.rs"] mod release_tests; +#[cfg(test)] +#[path = "written_tests.rs"] +mod written_tests; + #[cfg(test)] mod load_completion_tests { use super::*; diff --git a/src/sharplsp/src/pull_diagnostics/written_tests.rs b/src/sharplsp/src/pull_diagnostics/written_tests.rs new file mode 100644 index 00000000..a869a752 --- /dev/null +++ b/src/sharplsp/src/pull_diagnostics/written_tests.rs @@ -0,0 +1,79 @@ +//! A file the host itself writes — a compile item added to an `.fsproj` — +//! reloads its compilers through the same watched-file notification the native +//! watcher sends, so the reload never depends on the platform watcher noticing +//! the host's own write (it misses some on Windows). [DIAG-LSP-REFRESH] +use super::*; + +#[expect( + clippy::panic_in_result_fn, + reason = "test assertions intentionally fail while Result carries fixture I/O errors" +)] +mod contract { + use serde_json::json; + + use super::*; + use crate::config::DiagnosticsConfig; + use crate::sidecar::fake_compiler::{answering, empty, Fake}; + + /// The `didChangeWatchedFiles` notifications waiting on `receiver`. + fn announced(receiver: &crossbeam_channel::Receiver) -> Vec { + receiver + .try_iter() + .filter_map(|message| match message { + Message::Notification(note) if note.method == "workspace/didChangeWatchedFiles" => { + Some(note.params) + } + _ => None, + }) + .collect() + } + + #[test] + fn a_project_file_the_host_writes_is_announced_as_a_watched_change() -> Result<()> { + let runtime = tokio::runtime::Runtime::new()?; + let compiler = Fake::connect(&runtime, answering(empty())); + shutdown()?; + let capabilities: lsp_types::ClientCapabilities = + serde_json::from_value(json!({"textDocument":{"diagnostic":{}}}))?; + let (sender, _client) = crossbeam_channel::unbounded(); + initialize( + &capabilities, + &DiagnosticsConfig::default(), + &runtime, + [Some(&compiler.manager), None], + sender, + )?; + let project = std::env::temp_dir() + .join("sharplsp-written") + .join("App.fsproj"); + let uri = crate::paths::native_path_to_uri(&project)?; + let project = project.to_string_lossy().into_owned(); + + // Interaction 1 — before the dispatch queue exists, there is nobody to tell. + crate::pull_diagnostics::monitor::written(&[project.as_str()])?; + + // Interaction 2 — once it exists, the write is one Changed event for the project. + let (inbound, queue) = crossbeam_channel::unbounded(); + with_session(|session| { + session.inbound = Some(inbound); + Ok(()) + })?; + crate::pull_diagnostics::monitor::written(&[project.as_str()])?; + let notes = announced(&queue); + assert_eq!(notes.len(), 1, "one notification per write"); + assert_eq!( + notes.first().map(|note| note["changes"].clone()), + Some(json!([{ "uri": uri, "type": 2 }])), + "the project is announced Changed, exactly as the native watcher would" + ); + assert_eq!( + notes.first().map(|note| note["sharplspRescan"].clone()), + Some(json!(false)) + ); + + // Interaction 3 — writing nothing announces nothing. + crate::pull_diagnostics::monitor::written(&[])?; + assert!(announced(&queue).is_empty(), "no paths, no notification"); + shutdown() + } +}