Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/sharplsp/src/project_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions src/sharplsp/src/pull_diagnostics/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value> = 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<Message>, changes: &[Value], rescan: bool) -> bool {
let notification = Notification::new(
Expand Down
9 changes: 9 additions & 0 deletions src/sharplsp/src/pull_diagnostics/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ pub fn connect_inputs(
})
}

/// The foreground dispatch queue, once the connection has created it.
pub(super) fn inbound() -> Result<Option<crossbeam_channel::Sender<Message>>> {
with_session(|session| Ok(session.inbound.clone()))
}

/// The actual selected solution can live outside the editor's workspace folder.
pub fn workspace() -> Result<Option<String>> {
with_session(|session| Ok(session.workspace.clone()))
Expand Down Expand Up @@ -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::*;
Expand Down
79 changes: 79 additions & 0 deletions src/sharplsp/src/pull_diagnostics/written_tests.rs
Original file line number Diff line number Diff line change
@@ -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<Message>) -> Vec<serde_json::Value> {
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()
}
}
Loading