mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Auto merge of #17526 - Veykril:proc-server-errors, r=Veykril
internal: Improve error message when the proc-macro server unexpectedly exits
This commit is contained in:
commit
1b283db47f
4 changed files with 81 additions and 39 deletions
|
@ -130,6 +130,10 @@ impl ProcMacroServer {
|
|||
Err(message) => Err(ServerError { message, io: None }),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exited(&self) -> Option<&ServerError> {
|
||||
self.process.exited()
|
||||
}
|
||||
}
|
||||
|
||||
impl ProcMacro {
|
||||
|
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
use std::{
|
||||
io::{self, BufRead, BufReader, Read, Write},
|
||||
panic::AssertUnwindSafe,
|
||||
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
|
||||
sync::{Arc, Mutex},
|
||||
sync::{Arc, Mutex, OnceLock},
|
||||
};
|
||||
|
||||
use paths::AbsPath;
|
||||
|
@ -22,6 +23,8 @@ pub(crate) struct ProcMacroProcessSrv {
|
|||
state: Mutex<ProcessSrvState>,
|
||||
version: u32,
|
||||
mode: SpanMode,
|
||||
/// Populated when the server exits.
|
||||
exited: OnceLock<AssertUnwindSafe<ServerError>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -29,8 +32,6 @@ struct ProcessSrvState {
|
|||
process: Process,
|
||||
stdin: ChildStdin,
|
||||
stdout: BufReader<ChildStdout>,
|
||||
/// Populated when the server exits.
|
||||
server_exited: Option<ServerError>,
|
||||
}
|
||||
|
||||
impl ProcMacroProcessSrv {
|
||||
|
@ -44,9 +45,10 @@ impl ProcMacroProcessSrv {
|
|||
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
|
||||
|
||||
io::Result::Ok(ProcMacroProcessSrv {
|
||||
state: Mutex::new(ProcessSrvState { process, stdin, stdout, server_exited: None }),
|
||||
state: Mutex::new(ProcessSrvState { process, stdin, stdout }),
|
||||
version: 0,
|
||||
mode: SpanMode::Id,
|
||||
exited: OnceLock::new(),
|
||||
})
|
||||
};
|
||||
let mut srv = create_srv(true)?;
|
||||
|
@ -77,6 +79,10 @@ impl ProcMacroProcessSrv {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn exited(&self) -> Option<&ServerError> {
|
||||
self.exited.get().map(|it| &it.0)
|
||||
}
|
||||
|
||||
pub(crate) fn version(&self) -> u32 {
|
||||
self.version
|
||||
}
|
||||
|
@ -118,16 +124,29 @@ impl ProcMacroProcessSrv {
|
|||
}
|
||||
|
||||
pub(crate) fn send_task(&self, req: Request) -> Result<Response, ServerError> {
|
||||
let state = &mut *self.state.lock().unwrap();
|
||||
if let Some(server_error) = &state.server_exited {
|
||||
return Err(server_error.clone());
|
||||
if let Some(server_error) = self.exited.get() {
|
||||
return Err(server_error.0.clone());
|
||||
}
|
||||
|
||||
let state = &mut *self.state.lock().unwrap();
|
||||
let mut buf = String::new();
|
||||
send_request(&mut state.stdin, &mut state.stdout, req, &mut buf).map_err(|e| {
|
||||
send_request(&mut state.stdin, &mut state.stdout, req, &mut buf)
|
||||
.and_then(|res| {
|
||||
res.ok_or_else(|| {
|
||||
let message = "proc-macro server did not respond with data".to_owned();
|
||||
ServerError {
|
||||
io: Some(Arc::new(io::Error::new(
|
||||
io::ErrorKind::BrokenPipe,
|
||||
message.clone(),
|
||||
))),
|
||||
message,
|
||||
}
|
||||
})
|
||||
})
|
||||
.map_err(|e| {
|
||||
if e.io.as_ref().map(|it| it.kind()) == Some(io::ErrorKind::BrokenPipe) {
|
||||
match state.process.child.try_wait() {
|
||||
Ok(None) => e,
|
||||
Ok(None) | Err(_) => e,
|
||||
Ok(Some(status)) => {
|
||||
let mut msg = String::new();
|
||||
if !status.success() {
|
||||
|
@ -136,13 +155,16 @@ impl ProcMacroProcessSrv {
|
|||
}
|
||||
}
|
||||
let server_error = ServerError {
|
||||
message: format!("server exited with {status}: {msg}"),
|
||||
message: format!(
|
||||
"proc-macro server exited with {status}{}{msg}",
|
||||
if msg.is_empty() { "" } else { ": " }
|
||||
),
|
||||
io: None,
|
||||
};
|
||||
state.server_exited = Some(server_error.clone());
|
||||
server_error
|
||||
// `AssertUnwindSafe` is fine here, we already correct initialized
|
||||
// server_error at this point.
|
||||
self.exited.get_or_init(|| AssertUnwindSafe(server_error)).0.clone()
|
||||
}
|
||||
Err(_) => e,
|
||||
}
|
||||
} else {
|
||||
e
|
||||
|
@ -201,7 +223,7 @@ fn send_request(
|
|||
mut reader: &mut impl BufRead,
|
||||
req: Request,
|
||||
buf: &mut String,
|
||||
) -> Result<Response, ServerError> {
|
||||
) -> Result<Option<Response>, ServerError> {
|
||||
req.write(write_json, &mut writer).map_err(|err| ServerError {
|
||||
message: "failed to write request".into(),
|
||||
io: Some(Arc::new(err)),
|
||||
|
@ -210,5 +232,5 @@ fn send_request(
|
|||
message: "failed to read response".into(),
|
||||
io: Some(Arc::new(err)),
|
||||
})?;
|
||||
res.ok_or_else(|| ServerError { message: "server exited".into(), io: None })
|
||||
Ok(res)
|
||||
}
|
||||
|
|
|
@ -204,6 +204,8 @@ fn expand_ra_span(
|
|||
let macro_body = macro_body.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table);
|
||||
let attributes =
|
||||
attributes.map(|it| it.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table));
|
||||
// Note, we spawn a new thread here so that thread locals allocation don't accumulate (this
|
||||
// includes the proc-macro symbol interner)
|
||||
let result = thread::scope(|s| {
|
||||
let thread = thread::Builder::new()
|
||||
.stack_size(EXPANDER_STACK_SIZE)
|
||||
|
|
|
@ -184,7 +184,8 @@ impl GlobalState {
|
|||
message.push_str(err);
|
||||
message.push_str("\n\n");
|
||||
};
|
||||
if let Some(Err(err)) = proc_macro_client {
|
||||
match proc_macro_client {
|
||||
Some(Err(err)) => {
|
||||
status.health |= lsp_ext::Health::Warning;
|
||||
format_to!(
|
||||
message,
|
||||
|
@ -193,6 +194,19 @@ impl GlobalState {
|
|||
);
|
||||
message.push_str("\n\n");
|
||||
}
|
||||
Some(Ok(client)) => {
|
||||
if let Some(err) = client.exited() {
|
||||
status.health |= lsp_ext::Health::Warning;
|
||||
format_to!(
|
||||
message,
|
||||
"proc-macro server for workspace `{}` exited: {err}",
|
||||
ws.manifest_or_root()
|
||||
);
|
||||
message.push_str("\n\n");
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue