Skip to content

Commit e37c6dc

Browse files
authored
Merge pull request #18408 from Veykril/veykril/push-ulxyznwzokut
fix: Don't compute diagnostics for non local files
2 parents f2d12ff + bf77cf7 commit e37c6dc

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/dispatch.rs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
};
66

77
use ide::Cancelled;
8-
use lsp_server::ExtractError;
8+
use lsp_server::{ExtractError, Response, ResponseError};
99
use serde::{de::DeserializeOwned, Serialize};
1010
use stdx::thread::ThreadIntent;
1111

@@ -117,15 +117,20 @@ impl RequestDispatcher<'_> {
117117
}
118118
return self;
119119
}
120-
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(ThreadIntent::Worker, f)
120+
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(
121+
ThreadIntent::Worker,
122+
f,
123+
Self::content_modified_error,
124+
)
121125
}
122126

123127
/// Dispatches a non-latency-sensitive request onto the thread pool. When the VFS is marked not
124-
/// ready this will return a default constructed [`R::Result`].
125-
pub(crate) fn on_or<const ALLOW_RETRYING: bool, R>(
128+
/// ready this will return a `default` constructed [`R::Result`].
129+
pub(crate) fn on_with<R>(
126130
&mut self,
127131
f: fn(GlobalStateSnapshot, R::Params) -> anyhow::Result<R::Result>,
128132
default: impl FnOnce() -> R::Result,
133+
on_cancelled: fn() -> ResponseError,
129134
) -> &mut Self
130135
where
131136
R: lsp_types::request::Request<
@@ -141,7 +146,7 @@ impl RequestDispatcher<'_> {
141146
}
142147
return self;
143148
}
144-
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(ThreadIntent::Worker, f)
149+
self.on_with_thread_intent::<true, false, R>(ThreadIntent::Worker, f, on_cancelled)
145150
}
146151

147152
/// Dispatches a non-latency-sensitive request onto the thread pool. When the VFS is marked not
@@ -160,7 +165,11 @@ impl RequestDispatcher<'_> {
160165
}
161166
return self;
162167
}
163-
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(ThreadIntent::Worker, f)
168+
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(
169+
ThreadIntent::Worker,
170+
f,
171+
Self::content_modified_error,
172+
)
164173
}
165174

166175
/// Dispatches a latency-sensitive request onto the thread pool. When the VFS is marked not
@@ -183,7 +192,11 @@ impl RequestDispatcher<'_> {
183192
}
184193
return self;
185194
}
186-
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(ThreadIntent::LatencySensitive, f)
195+
self.on_with_thread_intent::<true, ALLOW_RETRYING, R>(
196+
ThreadIntent::LatencySensitive,
197+
f,
198+
Self::content_modified_error,
199+
)
187200
}
188201

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

204221
pub(crate) fn finish(&mut self) {
@@ -217,6 +234,7 @@ impl RequestDispatcher<'_> {
217234
&mut self,
218235
intent: ThreadIntent,
219236
f: fn(GlobalStateSnapshot, R::Params) -> anyhow::Result<R::Result>,
237+
on_cancelled: fn() -> ResponseError,
220238
) -> &mut Self
221239
where
222240
R: lsp_types::request::Request + 'static,
@@ -245,11 +263,10 @@ impl RequestDispatcher<'_> {
245263
match thread_result_to_response::<R>(req.id.clone(), result) {
246264
Ok(response) => Task::Response(response),
247265
Err(_cancelled) if ALLOW_RETRYING => Task::Retry(req),
248-
Err(_cancelled) => Task::Response(lsp_server::Response::new_err(
249-
req.id,
250-
lsp_server::ErrorCode::ContentModified as i32,
251-
"content modified".to_owned(),
252-
)),
266+
Err(_cancelled) => {
267+
let error = on_cancelled();
268+
Task::Response(Response { id: req.id, result: None, error: Some(error) })
269+
}
253270
}
254271
});
255272

@@ -280,6 +297,14 @@ impl RequestDispatcher<'_> {
280297
}
281298
}
282299
}
300+
301+
fn content_modified_error() -> ResponseError {
302+
ResponseError {
303+
code: lsp_server::ErrorCode::ContentModified as i32,
304+
message: "content modified".to_owned(),
305+
data: None,
306+
}
307+
}
283308
}
284309

285310
fn thread_result_to_response<R>(

src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,8 @@ pub(crate) fn handle_document_diagnostics(
479479
snap: GlobalStateSnapshot,
480480
params: lsp_types::DocumentDiagnosticParams,
481481
) -> anyhow::Result<lsp_types::DocumentDiagnosticReportResult> {
482-
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
483-
let source_root = snap.analysis.source_root_id(file_id)?;
484-
let line_index = snap.file_line_index(file_id)?;
485-
let config = snap.config.diagnostics(Some(source_root));
486-
if !config.enabled {
487-
return Ok(lsp_types::DocumentDiagnosticReportResult::Report(
482+
const EMPTY: lsp_types::DocumentDiagnosticReportResult =
483+
lsp_types::DocumentDiagnosticReportResult::Report(
488484
lsp_types::DocumentDiagnosticReport::Full(
489485
lsp_types::RelatedFullDocumentDiagnosticReport {
490486
related_documents: None,
@@ -494,8 +490,18 @@ pub(crate) fn handle_document_diagnostics(
494490
},
495491
},
496492
),
497-
));
493+
);
494+
495+
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
496+
let source_root = snap.analysis.source_root_id(file_id)?;
497+
if !snap.analysis.is_local_source_root(source_root)? {
498+
return Ok(EMPTY);
499+
}
500+
let config = snap.config.diagnostics(Some(source_root));
501+
if !config.enabled {
502+
return Ok(EMPTY);
498503
}
504+
let line_index = snap.file_line_index(file_id)?;
499505
let supports_related = snap.config.text_document_diagnostic_related_document_support();
500506

501507
let mut related_documents = FxHashMap::default();

src/tools/rust-analyzer/crates/rust-analyzer/src/main_loop.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ impl GlobalState {
10921092
.on_latency_sensitive::<NO_RETRY, lsp_request::SemanticTokensRangeRequest>(handlers::handle_semantic_tokens_range)
10931093
// FIXME: Some of these NO_RETRY could be retries if the file they are interested didn't change.
10941094
// All other request handlers
1095-
.on_or::<NO_RETRY, lsp_request::DocumentDiagnosticRequest>(handlers::handle_document_diagnostics, || lsp_types::DocumentDiagnosticReportResult::Report(
1095+
.on_with::<lsp_request::DocumentDiagnosticRequest>(handlers::handle_document_diagnostics, || lsp_types::DocumentDiagnosticReportResult::Report(
10961096
lsp_types::DocumentDiagnosticReport::Full(
10971097
lsp_types::RelatedFullDocumentDiagnosticReport {
10981098
related_documents: None,
@@ -1102,7 +1102,13 @@ impl GlobalState {
11021102
},
11031103
},
11041104
),
1105-
))
1105+
), || lsp_server::ResponseError {
1106+
code: lsp_server::ErrorCode::ServerCancelled as i32,
1107+
message: "server cancelled the request".to_owned(),
1108+
data: serde_json::to_value(lsp_types::DiagnosticServerCancellationData {
1109+
retrigger_request: true
1110+
}).ok(),
1111+
})
11061112
.on::<RETRY, lsp_request::DocumentSymbolRequest>(handlers::handle_document_symbol)
11071113
.on::<RETRY, lsp_request::FoldingRangeRequest>(handlers::handle_folding_range)
11081114
.on::<NO_RETRY, lsp_request::SignatureHelpRequest>(handlers::handle_signature_help)

0 commit comments

Comments
 (0)