Skip to content

Commit

Permalink
Merge pull request #18408 from Veykril/veykril/push-ulxyznwzokut
Browse files Browse the repository at this point in the history
fix: Don't compute diagnostics for non local files
  • Loading branch information
Veykril authored Oct 25, 2024
2 parents 169d446 + 8eef1c5 commit 52a03ec
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 22 deletions.
51 changes: 38 additions & 13 deletions crates/rust-analyzer/src/handlers/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use ide::Cancelled;
use lsp_server::ExtractError;
use lsp_server::{ExtractError, Response, ResponseError};
use serde::{de::DeserializeOwned, Serialize};
use stdx::thread::ThreadIntent;

Expand Down Expand Up @@ -117,15 +117,20 @@ impl RequestDispatcher<'_> {
}
return self;
}
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(ThreadIntent::Worker, f)
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(
ThreadIntent::Worker,
f,
Self::content_modified_error,
)
}

/// Dispatches a non-latency-sensitive request onto the thread pool. When the VFS is marked not
/// ready this will return a default constructed [`R::Result`].
pub(crate) fn on_or<const ALLOW_RETRYING: bool, R>(
/// ready this will return a `default` constructed [`R::Result`].
pub(crate) fn on_with<R>(
&mut self,
f: fn(GlobalStateSnapshot, R::Params) -> anyhow::Result<R::Result>,
default: impl FnOnce() -> R::Result,
on_cancelled: fn() -> ResponseError,
) -> &mut Self
where
R: lsp_types::request::Request<
Expand All @@ -141,7 +146,7 @@ impl RequestDispatcher<'_> {
}
return self;
}
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(ThreadIntent::Worker, f)
self.on_with_thread_intent::<true, false, R>(ThreadIntent::Worker, f, on_cancelled)
}

/// Dispatches a non-latency-sensitive request onto the thread pool. When the VFS is marked not
Expand All @@ -160,7 +165,11 @@ impl RequestDispatcher<'_> {
}
return self;
}
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(ThreadIntent::Worker, f)
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(
ThreadIntent::Worker,
f,
Self::content_modified_error,
)
}

/// Dispatches a latency-sensitive request onto the thread pool. When the VFS is marked not
Expand All @@ -183,7 +192,11 @@ impl RequestDispatcher<'_> {
}
return self;
}
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(ThreadIntent::LatencySensitive, f)
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(
ThreadIntent::LatencySensitive,
f,
Self::content_modified_error,
)
}

/// Formatting requests should never block on waiting a for task thread to open up, editors will wait
Expand All @@ -198,7 +211,11 @@ impl RequestDispatcher<'_> {
R::Params: DeserializeOwned + panic::UnwindSafe + Send + fmt::Debug,
R::Result: Serialize,
{
self.on_with_thread_intent::<false, false, R>(ThreadIntent::LatencySensitive, f)
self.on_with_thread_intent::<false, false, R>(
ThreadIntent::LatencySensitive,
f,
Self::content_modified_error,
)
}

pub(crate) fn finish(&mut self) {
Expand All @@ -217,6 +234,7 @@ impl RequestDispatcher<'_> {
&mut self,
intent: ThreadIntent,
f: fn(GlobalStateSnapshot, R::Params) -> anyhow::Result<R::Result>,
on_cancelled: fn() -> ResponseError,
) -> &mut Self
where
R: lsp_types::request::Request + 'static,
Expand Down Expand Up @@ -245,11 +263,10 @@ impl RequestDispatcher<'_> {
match thread_result_to_response::<R>(req.id.clone(), result) {
Ok(response) => Task::Response(response),
Err(_cancelled) if ALLOW_RETRYING => Task::Retry(req),
Err(_cancelled) => Task::Response(lsp_server::Response::new_err(
req.id,
lsp_server::ErrorCode::ContentModified as i32,
"content modified".to_owned(),
)),
Err(_cancelled) => {
let error = on_cancelled();
Task::Response(Response { id: req.id, result: None, error: Some(error) })
}
}
});

Expand Down Expand Up @@ -280,6 +297,14 @@ impl RequestDispatcher<'_> {
}
}
}

fn content_modified_error() -> ResponseError {
ResponseError {
code: lsp_server::ErrorCode::ContentModified as i32,
message: "content modified".to_owned(),
data: None,
}
}
}

fn thread_result_to_response<R>(
Expand Down
20 changes: 13 additions & 7 deletions crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,8 @@ pub(crate) fn handle_document_diagnostics(
snap: GlobalStateSnapshot,
params: lsp_types::DocumentDiagnosticParams,
) -> anyhow::Result<lsp_types::DocumentDiagnosticReportResult> {
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let source_root = snap.analysis.source_root_id(file_id)?;
let line_index = snap.file_line_index(file_id)?;
let config = snap.config.diagnostics(Some(source_root));
if !config.enabled {
return Ok(lsp_types::DocumentDiagnosticReportResult::Report(
const EMPTY: lsp_types::DocumentDiagnosticReportResult =
lsp_types::DocumentDiagnosticReportResult::Report(
lsp_types::DocumentDiagnosticReport::Full(
lsp_types::RelatedFullDocumentDiagnosticReport {
related_documents: None,
Expand All @@ -494,8 +490,18 @@ pub(crate) fn handle_document_diagnostics(
},
},
),
));
);

let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let source_root = snap.analysis.source_root_id(file_id)?;
if !snap.analysis.is_local_source_root(source_root)? {
return Ok(EMPTY);
}
let config = snap.config.diagnostics(Some(source_root));
if !config.enabled {
return Ok(EMPTY);
}
let line_index = snap.file_line_index(file_id)?;
let supports_related = snap.config.text_document_diagnostic_related_document_support();

let mut related_documents = FxHashMap::default();
Expand Down
10 changes: 8 additions & 2 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ impl GlobalState {
.on_latency_sensitive::<NO_RETRY, lsp_request::SemanticTokensRangeRequest>(handlers::handle_semantic_tokens_range)
// FIXME: Some of these NO_RETRY could be retries if the file they are interested didn't change.
// All other request handlers
.on_or::<NO_RETRY, lsp_request::DocumentDiagnosticRequest>(handlers::handle_document_diagnostics, || lsp_types::DocumentDiagnosticReportResult::Report(
.on_with::<lsp_request::DocumentDiagnosticRequest>(handlers::handle_document_diagnostics, || lsp_types::DocumentDiagnosticReportResult::Report(
lsp_types::DocumentDiagnosticReport::Full(
lsp_types::RelatedFullDocumentDiagnosticReport {
related_documents: None,
Expand All @@ -1102,7 +1102,13 @@ impl GlobalState {
},
},
),
))
), || lsp_server::ResponseError {
code: lsp_server::ErrorCode::ServerCancelled as i32,
message: "server cancelled the request".to_owned(),
data: serde_json::to_value(lsp_types::DiagnosticServerCancellationData {
retrigger_request: true
}).ok(),
})
.on::<RETRY, lsp_request::DocumentSymbolRequest>(handlers::handle_document_symbol)
.on::<RETRY, lsp_request::FoldingRangeRequest>(handlers::handle_folding_range)
.on::<NO_RETRY, lsp_request::SignatureHelpRequest>(handlers::handle_signature_help)
Expand Down

0 comments on commit 52a03ec

Please sign in to comment.