mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
Merge commit '9d8889cdfc
' into sync-from-ra
This commit is contained in:
parent
3afeb24198
commit
6bbd106c70
104 changed files with 1652 additions and 1026 deletions
|
@ -14,8 +14,10 @@ mod version;
|
|||
use indexmap::IndexSet;
|
||||
use paths::AbsPathBuf;
|
||||
use span::Span;
|
||||
use std::{fmt, io, sync::Mutex};
|
||||
use triomphe::Arc;
|
||||
use std::{
|
||||
fmt, io,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
@ -81,9 +83,11 @@ impl PartialEq for ProcMacro {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ServerError {
|
||||
pub message: String,
|
||||
pub io: Option<io::Error>,
|
||||
// io::Error isn't Clone for some reason
|
||||
pub io: Option<Arc<io::Error>>,
|
||||
}
|
||||
|
||||
impl fmt::Display for ServerError {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -131,12 +162,19 @@ impl Process {
|
|||
}
|
||||
|
||||
fn mk_child(path: &AbsPath, null_stderr: bool) -> io::Result<Child> {
|
||||
Command::new(path.as_os_str())
|
||||
.env("RUST_ANALYZER_INTERNALS_DO_NOT_USE", "this is unstable")
|
||||
let mut cmd = Command::new(path.as_os_str());
|
||||
cmd.env("RUST_ANALYZER_INTERNALS_DO_NOT_USE", "this is unstable")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(if null_stderr { Stdio::null() } else { Stdio::inherit() })
|
||||
.spawn()
|
||||
.stderr(if null_stderr { Stdio::null() } else { Stdio::inherit() });
|
||||
if cfg!(windows) {
|
||||
let mut path_var = std::ffi::OsString::new();
|
||||
path_var.push(path.parent().unwrap().parent().unwrap().as_os_str());
|
||||
path_var.push("\\bin;");
|
||||
path_var.push(std::env::var_os("PATH").unwrap_or_default());
|
||||
cmd.env("PATH", path_var);
|
||||
}
|
||||
cmd.spawn()
|
||||
}
|
||||
|
||||
fn send_request(
|
||||
|
@ -145,9 +183,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