mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-19 00:31:19 +00:00
Recognize proc macro server unexpectedly exiting and report the exit code
This commit is contained in:
parent
f5f7ddae23
commit
21a953fe37
3 changed files with 52 additions and 12 deletions
|
@ -1,8 +1,9 @@
|
|||
//! Handle process life-time and message passing for proc-macro client
|
||||
|
||||
use std::{
|
||||
io::{self, BufRead, BufReader, Write},
|
||||
io::{self, BufRead, BufReader, Read, Write},
|
||||
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use paths::{AbsPath, AbsPathBuf};
|
||||
|
@ -15,9 +16,11 @@ use crate::{
|
|||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ProcMacroProcessSrv {
|
||||
_process: Process,
|
||||
process: Process,
|
||||
stdin: ChildStdin,
|
||||
stdout: BufReader<ChildStdout>,
|
||||
/// Populated when the server exits.
|
||||
server_exited: Option<ServerError>,
|
||||
version: u32,
|
||||
mode: SpanMode,
|
||||
}
|
||||
|
@ -29,9 +32,10 @@ impl ProcMacroProcessSrv {
|
|||
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
|
||||
|
||||
io::Result::Ok(ProcMacroProcessSrv {
|
||||
_process: process,
|
||||
process,
|
||||
stdin,
|
||||
stdout,
|
||||
server_exited: None,
|
||||
version: 0,
|
||||
mode: SpanMode::Id,
|
||||
})
|
||||
|
@ -105,8 +109,35 @@ impl ProcMacroProcessSrv {
|
|||
}
|
||||
|
||||
pub(crate) fn send_task(&mut self, req: Request) -> Result<Response, ServerError> {
|
||||
if let Some(server_error) = &self.server_exited {
|
||||
return Err(server_error.clone());
|
||||
}
|
||||
|
||||
let mut buf = String::new();
|
||||
send_request(&mut self.stdin, &mut self.stdout, req, &mut buf)
|
||||
send_request(&mut self.stdin, &mut self.stdout, req, &mut buf).map_err(|e| {
|
||||
if e.io.as_ref().map(|it| it.kind()) == Some(io::ErrorKind::BrokenPipe) {
|
||||
match self.process.child.try_wait() {
|
||||
Ok(None) => e,
|
||||
Ok(Some(status)) => {
|
||||
let mut msg = String::new();
|
||||
if !status.success() {
|
||||
if let Some(stderr) = self.process.child.stderr.as_mut() {
|
||||
_ = stderr.read_to_string(&mut msg);
|
||||
}
|
||||
}
|
||||
let server_error = ServerError {
|
||||
message: format!("server exited with {status}: {msg}"),
|
||||
io: None,
|
||||
};
|
||||
self.server_exited = Some(server_error.clone());
|
||||
server_error
|
||||
}
|
||||
Err(_) => e,
|
||||
}
|
||||
} else {
|
||||
e
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -145,9 +176,13 @@ fn send_request(
|
|||
req: Request,
|
||||
buf: &mut String,
|
||||
) -> Result<Response, ServerError> {
|
||||
req.write(&mut writer)
|
||||
.map_err(|err| ServerError { message: "failed to write request".into(), io: Some(err) })?;
|
||||
let res = Response::read(&mut reader, buf)
|
||||
.map_err(|err| ServerError { message: "failed to read response".into(), io: Some(err) })?;
|
||||
req.write(&mut writer).map_err(|err| ServerError {
|
||||
message: "failed to write request".into(),
|
||||
io: Some(Arc::new(err)),
|
||||
})?;
|
||||
let res = Response::read(&mut reader, buf).map_err(|err| ServerError {
|
||||
message: "failed to read response".into(),
|
||||
io: Some(Arc::new(err)),
|
||||
})?;
|
||||
res.ok_or_else(|| ServerError { message: "server exited".into(), io: None })
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue