Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/proc-macro-api/src/process.rs')
-rw-r--r--crates/proc-macro-api/src/process.rs52
1 files changed, 29 insertions, 23 deletions
diff --git a/crates/proc-macro-api/src/process.rs b/crates/proc-macro-api/src/process.rs
index 718a96dc80..2b1a791827 100644
--- a/crates/proc-macro-api/src/process.rs
+++ b/crates/proc-macro-api/src/process.rs
@@ -3,43 +3,48 @@
use std::{
io::{self, BufRead, BufReader, Read, Write},
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
- sync::Arc,
+ sync::{Arc, Mutex},
};
use paths::AbsPath;
-use rustc_hash::FxHashMap;
use stdx::JodChild;
use crate::{
+ json::{read_json, write_json},
msg::{Message, Request, Response, SpanMode, CURRENT_API_VERSION, RUST_ANALYZER_SPAN_SUPPORT},
ProcMacroKind, ServerError,
};
#[derive(Debug)]
pub(crate) struct ProcMacroProcessSrv {
+ /// The state of the proc-macro server process, the protocol is currently strictly sequential
+ /// hence the lock on the state.
+ state: Mutex<ProcessSrvState>,
+ version: u32,
+ mode: SpanMode,
+}
+
+#[derive(Debug)]
+struct ProcessSrvState {
process: Process,
stdin: ChildStdin,
stdout: BufReader<ChildStdout>,
/// Populated when the server exits.
server_exited: Option<ServerError>,
- version: u32,
- mode: SpanMode,
}
impl ProcMacroProcessSrv {
pub(crate) fn run(
process_path: &AbsPath,
- env: &FxHashMap<String, String>,
+ env: impl IntoIterator<Item = (impl AsRef<std::ffi::OsStr>, impl AsRef<std::ffi::OsStr>)>
+ + Clone,
) -> io::Result<ProcMacroProcessSrv> {
let create_srv = |null_stderr| {
- let mut process = Process::run(process_path, env, null_stderr)?;
+ let mut process = Process::run(process_path, env.clone(), null_stderr)?;
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
io::Result::Ok(ProcMacroProcessSrv {
- process,
- stdin,
- stdout,
- server_exited: None,
+ state: Mutex::new(ProcessSrvState { process, stdin, stdout, server_exited: None }),
version: 0,
mode: SpanMode::Id,
})
@@ -76,7 +81,7 @@ impl ProcMacroProcessSrv {
self.version
}
- pub(crate) fn version_check(&mut self) -> Result<u32, ServerError> {
+ fn version_check(&self) -> Result<u32, ServerError> {
let request = Request::ApiVersionCheck {};
let response = self.send_task(request)?;
@@ -86,7 +91,7 @@ impl ProcMacroProcessSrv {
}
}
- fn enable_rust_analyzer_spans(&mut self) -> Result<SpanMode, ServerError> {
+ fn enable_rust_analyzer_spans(&self) -> Result<SpanMode, ServerError> {
let request = Request::SetConfig(crate::msg::ServerConfig {
span_mode: crate::msg::SpanMode::RustAnalyzer,
});
@@ -99,7 +104,7 @@ impl ProcMacroProcessSrv {
}
pub(crate) fn find_proc_macros(
- &mut self,
+ &self,
dylib_path: &AbsPath,
) -> Result<Result<Vec<(String, ProcMacroKind)>, String>, ServerError> {
let request = Request::ListMacros { dylib_path: dylib_path.to_path_buf().into() };
@@ -112,20 +117,21 @@ impl ProcMacroProcessSrv {
}
}
- pub(crate) fn send_task(&mut self, req: Request) -> Result<Response, ServerError> {
- if let Some(server_error) = &self.server_exited {
+ 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());
}
let mut buf = String::new();
- send_request(&mut self.stdin, &mut self.stdout, req, &mut buf).map_err(|e| {
+ send_request(&mut state.stdin, &mut state.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() {
+ match state.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() {
+ if let Some(stderr) = state.process.child.stderr.as_mut() {
_ = stderr.read_to_string(&mut msg);
}
}
@@ -133,7 +139,7 @@ impl ProcMacroProcessSrv {
message: format!("server exited with {status}: {msg}"),
io: None,
};
- self.server_exited = Some(server_error.clone());
+ state.server_exited = Some(server_error.clone());
server_error
}
Err(_) => e,
@@ -153,7 +159,7 @@ struct Process {
impl Process {
fn run(
path: &AbsPath,
- env: &FxHashMap<String, String>,
+ env: impl IntoIterator<Item = (impl AsRef<std::ffi::OsStr>, impl AsRef<std::ffi::OsStr>)>,
null_stderr: bool,
) -> io::Result<Process> {
let child = JodChild(mk_child(path, env, null_stderr)?);
@@ -171,7 +177,7 @@ impl Process {
fn mk_child(
path: &AbsPath,
- env: &FxHashMap<String, String>,
+ env: impl IntoIterator<Item = (impl AsRef<std::ffi::OsStr>, impl AsRef<std::ffi::OsStr>)>,
null_stderr: bool,
) -> io::Result<Child> {
let mut cmd = Command::new(path);
@@ -196,11 +202,11 @@ fn send_request(
req: Request,
buf: &mut String,
) -> Result<Response, ServerError> {
- req.write(&mut writer).map_err(|err| ServerError {
+ req.write(write_json, &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 {
+ let res = Response::read(read_json, &mut reader, buf).map_err(|err| ServerError {
message: "failed to read response".into(),
io: Some(Arc::new(err)),
})?;