Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Result for failed text document retrieval in LSP requests #14579

Merged
merged 2 commits into from
Nov 25, 2024

Conversation

dhruvmanila
Copy link
Member

Summary

Ref: astral-sh/ruff-vscode#644 (comment)

Test Plan

Not sure how to test this as this is mainly to get more context on the panic that the server is raising.

@dhruvmanila dhruvmanila added the internal An internal refactor or improvement label Nov 25, 2024
Copy link
Contributor

github-actions bot commented Nov 25, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great.

We should consider propagating the error instead of unwrapping, to prevent the LSP from crashing

Comment on lines 67 to 68
.map_err(|err| anyhow::anyhow!("Failed to get text document for the format request: {err}"))
.unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never fully understood this in the LSP but could we propagate the error instead of unwrapping here?

Suggested change
.map_err(|err| anyhow::anyhow!("Failed to get text document for the format request: {err}"))
.unwrap();
.context("Failed to get text document for the format request"))
.unwrap();

I think that should be sufficient. anyhow preserves the entire error-chain

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought about doing this but then we would still have the problem that the message will only be logged if the user has turned on server messages via ruff.trace.server: "messages":

/// Sends back a response to the server using a [`Responder`].
fn respond<Req>(
id: server::RequestId,
result: crate::server::Result<
<<Req as traits::RequestHandler>::RequestType as lsp_types::request::Request>::Result,
>,
responder: &Responder,
) where
Req: traits::RequestHandler,
{
if let Err(err) = &result {
tracing::error!("An error occurred with result ID {id}: {err}");
show_err_msg!("Ruff encountered a problem. Check the logs for more details.");
}
if let Err(err) = responder.respond(id, result) {
tracing::error!("Failed to send response: {err}");
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll want to club the change that you're suggesting along with the logging infrastructure change that I'm planning for later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I would still prefer propagating because there's a workaround to get logs, but there's no workaround for a crashing ruff server.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main concern here is that the user will only see the notification but no log messages. Then, the user would need to turn on server messages which would restart the server and will loose the state in which the panic occurs. So, the user would need to wait until the next time the error occurs to provide us the logs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using anyhow's Context requires cloning the Url because I can't use the reference because of lifetime issues.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use with_context

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue:

error[E0521]: borrowed data escapes outside of function
  --> crates/ruff_server/src/server/api/requests/format.rs:65:25
   |
64 |   pub(super) fn format_document(snapshot: &DocumentSnapshot) -> Result<super::FormatResponse> {
   |                                 --------  - let's call the lifetime of this reference `'1`
   |                                 |
   |                                 `snapshot` is a reference that is only valid in the function body
65 |       let text_document = snapshot
   |  _________________________^
66 | |         .query()
   | |                ^
   | |                |
   | |________________`snapshot` escapes the function body here
   |                  argument requires that `'1` must outlive `'static`

error[E0521]: borrowed data escapes outside of function
  --> crates/ruff_server/src/server/api/requests/format_range.rs:33:25
   |
30 |       snapshot: &DocumentSnapshot,
   |       --------  - let's call the lifetime of this reference `'1`
   |       |
   |       `snapshot` is a reference that is only valid in the function body
...
33 |       let text_document = snapshot
   |  _________________________^
34 | |         .query()
   | |                ^
   | |                |
   | |________________`snapshot` escapes the function body here
   |                  argument requires that `'1` must outlive `'static`

error[E0521]: borrowed data escapes outside of function
  --> crates/ruff_server/src/server/api/requests/hover.rs:34:20
   |
30 |       snapshot: &DocumentSnapshot,
   |       --------  - let's call the lifetime of this reference `'1`
   |       |
   |       `snapshot` is a reference that is only valid in the function body
...
34 |       let document = snapshot
   |  ____________________^
35 | |         .query()
   | |                ^
   | |                |
   | |________________`snapshot` escapes the function body here
   |                  argument requires that `'1` must outlive `'static`

For more information about this error, try `rustc --explain E0521`.
error: could not compile `ruff_server` (lib) due to 3 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `ruff_server` (lib test) due to 3 previous errors

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see now. I think cloning is fine. It only happens in the error case and it's uncommon that errors have lifetime bounds (e.g. it prevents users from propagating errors, as you've seen here)

@MichaReiser MichaReiser added the server Related to the LSP server label Nov 25, 2024
@dhruvmanila dhruvmanila force-pushed the dhruv/single-document-error branch from d3a58c1 to 8c1c42a Compare November 25, 2024 08:41
@dhruvmanila dhruvmanila merged commit 0c9165f into main Nov 25, 2024
20 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/single-document-error branch November 25, 2024 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement server Related to the LSP server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants