mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 21:43:37 +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 }),
|
Err(message) => Err(ServerError { message, io: None }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn exited(&self) -> Option<&ServerError> {
|
||||||
|
self.process.exited()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProcMacro {
|
impl ProcMacro {
|
||||||
|
|
|
@ -2,8 +2,9 @@
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
io::{self, BufRead, BufReader, Read, Write},
|
io::{self, BufRead, BufReader, Read, Write},
|
||||||
|
panic::AssertUnwindSafe,
|
||||||
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
|
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex, OnceLock},
|
||||||
};
|
};
|
||||||
|
|
||||||
use paths::AbsPath;
|
use paths::AbsPath;
|
||||||
|
@ -22,6 +23,8 @@ pub(crate) struct ProcMacroProcessSrv {
|
||||||
state: Mutex<ProcessSrvState>,
|
state: Mutex<ProcessSrvState>,
|
||||||
version: u32,
|
version: u32,
|
||||||
mode: SpanMode,
|
mode: SpanMode,
|
||||||
|
/// Populated when the server exits.
|
||||||
|
exited: OnceLock<AssertUnwindSafe<ServerError>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -29,8 +32,6 @@ struct ProcessSrvState {
|
||||||
process: Process,
|
process: Process,
|
||||||
stdin: ChildStdin,
|
stdin: ChildStdin,
|
||||||
stdout: BufReader<ChildStdout>,
|
stdout: BufReader<ChildStdout>,
|
||||||
/// Populated when the server exits.
|
|
||||||
server_exited: Option<ServerError>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProcMacroProcessSrv {
|
impl ProcMacroProcessSrv {
|
||||||
|
@ -44,9 +45,10 @@ impl ProcMacroProcessSrv {
|
||||||
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
|
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
|
||||||
|
|
||||||
io::Result::Ok(ProcMacroProcessSrv {
|
io::Result::Ok(ProcMacroProcessSrv {
|
||||||
state: Mutex::new(ProcessSrvState { process, stdin, stdout, server_exited: None }),
|
state: Mutex::new(ProcessSrvState { process, stdin, stdout }),
|
||||||
version: 0,
|
version: 0,
|
||||||
mode: SpanMode::Id,
|
mode: SpanMode::Id,
|
||||||
|
exited: OnceLock::new(),
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
let mut srv = create_srv(true)?;
|
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 {
|
pub(crate) fn version(&self) -> u32 {
|
||||||
self.version
|
self.version
|
||||||
}
|
}
|
||||||
|
@ -118,16 +124,29 @@ impl ProcMacroProcessSrv {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn send_task(&self, req: Request) -> Result<Response, ServerError> {
|
pub(crate) fn send_task(&self, req: Request) -> Result<Response, ServerError> {
|
||||||
let state = &mut *self.state.lock().unwrap();
|
if let Some(server_error) = self.exited.get() {
|
||||||
if let Some(server_error) = &state.server_exited {
|
return Err(server_error.0.clone());
|
||||||
return Err(server_error.clone());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let state = &mut *self.state.lock().unwrap();
|
||||||
let mut buf = String::new();
|
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) {
|
if e.io.as_ref().map(|it| it.kind()) == Some(io::ErrorKind::BrokenPipe) {
|
||||||
match state.process.child.try_wait() {
|
match state.process.child.try_wait() {
|
||||||
Ok(None) => e,
|
Ok(None) | Err(_) => e,
|
||||||
Ok(Some(status)) => {
|
Ok(Some(status)) => {
|
||||||
let mut msg = String::new();
|
let mut msg = String::new();
|
||||||
if !status.success() {
|
if !status.success() {
|
||||||
|
@ -136,13 +155,16 @@ impl ProcMacroProcessSrv {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let server_error = ServerError {
|
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,
|
io: None,
|
||||||
};
|
};
|
||||||
state.server_exited = Some(server_error.clone());
|
// `AssertUnwindSafe` is fine here, we already correct initialized
|
||||||
server_error
|
// server_error at this point.
|
||||||
|
self.exited.get_or_init(|| AssertUnwindSafe(server_error)).0.clone()
|
||||||
}
|
}
|
||||||
Err(_) => e,
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
e
|
e
|
||||||
|
@ -201,7 +223,7 @@ fn send_request(
|
||||||
mut reader: &mut impl BufRead,
|
mut reader: &mut impl BufRead,
|
||||||
req: Request,
|
req: Request,
|
||||||
buf: &mut String,
|
buf: &mut String,
|
||||||
) -> Result<Response, ServerError> {
|
) -> Result<Option<Response>, ServerError> {
|
||||||
req.write(write_json, &mut writer).map_err(|err| ServerError {
|
req.write(write_json, &mut writer).map_err(|err| ServerError {
|
||||||
message: "failed to write request".into(),
|
message: "failed to write request".into(),
|
||||||
io: Some(Arc::new(err)),
|
io: Some(Arc::new(err)),
|
||||||
|
@ -210,5 +232,5 @@ fn send_request(
|
||||||
message: "failed to read response".into(),
|
message: "failed to read response".into(),
|
||||||
io: Some(Arc::new(err)),
|
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 macro_body = macro_body.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table);
|
||||||
let attributes =
|
let attributes =
|
||||||
attributes.map(|it| it.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table));
|
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 result = thread::scope(|s| {
|
||||||
let thread = thread::Builder::new()
|
let thread = thread::Builder::new()
|
||||||
.stack_size(EXPANDER_STACK_SIZE)
|
.stack_size(EXPANDER_STACK_SIZE)
|
||||||
|
|
|
@ -184,7 +184,8 @@ impl GlobalState {
|
||||||
message.push_str(err);
|
message.push_str(err);
|
||||||
message.push_str("\n\n");
|
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;
|
status.health |= lsp_ext::Health::Warning;
|
||||||
format_to!(
|
format_to!(
|
||||||
message,
|
message,
|
||||||
|
@ -193,6 +194,19 @@ impl GlobalState {
|
||||||
);
|
);
|
||||||
message.push_str("\n\n");
|
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