summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs')
-rw-r--r--src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs54
1 files changed, 45 insertions, 9 deletions
diff --git a/src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs b/src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs
index 54dcb17f4..1ccbd780f 100644
--- a/src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs
+++ b/src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs
@@ -10,7 +10,7 @@ use paths::{AbsPath, AbsPathBuf};
use stdx::JodChild;
use crate::{
- msg::{Message, Request, Response},
+ msg::{Message, Request, Response, CURRENT_API_VERSION},
ProcMacroKind, ServerError,
};
@@ -19,19 +19,53 @@ pub(crate) struct ProcMacroProcessSrv {
_process: Process,
stdin: ChildStdin,
stdout: BufReader<ChildStdout>,
+ version: u32,
}
impl ProcMacroProcessSrv {
pub(crate) fn run(
process_path: AbsPathBuf,
- args: impl IntoIterator<Item = impl AsRef<OsStr>>,
+ args: impl IntoIterator<Item = impl AsRef<OsStr>> + Clone,
) -> io::Result<ProcMacroProcessSrv> {
- let mut process = Process::run(process_path, args)?;
- let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
+ let create_srv = |null_stderr| {
+ let mut process = Process::run(process_path.clone(), args.clone(), null_stderr)?;
+ let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
+
+ io::Result::Ok(ProcMacroProcessSrv { _process: process, stdin, stdout, version: 0 })
+ };
+ let mut srv = create_srv(true)?;
+ tracing::info!("sending version check");
+ match srv.version_check() {
+ Ok(v) if v > CURRENT_API_VERSION => Err(io::Error::new(
+ io::ErrorKind::Other,
+ format!(
+ "proc-macro server's api version ({}) is newer than rust-analyzer's ({})",
+ v, CURRENT_API_VERSION
+ ),
+ )),
+ Ok(v) => {
+ tracing::info!("got version {v}");
+ srv = create_srv(false)?;
+ srv.version = v;
+ Ok(srv)
+ }
+ Err(e) => {
+ tracing::info!(%e, "proc-macro version check failed, restarting and assuming version 0");
+ create_srv(false)
+ }
+ }
+ }
- let srv = ProcMacroProcessSrv { _process: process, stdin, stdout };
+ pub(crate) fn version_check(&mut self) -> Result<u32, ServerError> {
+ let request = Request::ApiVersionCheck {};
+ let response = self.send_task(request)?;
- Ok(srv)
+ match response {
+ Response::ApiVersionCheck(version) => Ok(version),
+ Response::ExpandMacro { .. } | Response::ListMacros { .. } => {
+ Err(ServerError { message: "unexpected response".to_string(), io: None })
+ }
+ }
}
pub(crate) fn find_proc_macros(
@@ -44,7 +78,7 @@ impl ProcMacroProcessSrv {
match response {
Response::ListMacros(it) => Ok(it),
- Response::ExpandMacro { .. } => {
+ Response::ExpandMacro { .. } | Response::ApiVersionCheck { .. } => {
Err(ServerError { message: "unexpected response".to_string(), io: None })
}
}
@@ -65,9 +99,10 @@ impl Process {
fn run(
path: AbsPathBuf,
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
+ null_stderr: bool,
) -> io::Result<Process> {
let args: Vec<OsString> = args.into_iter().map(|s| s.as_ref().into()).collect();
- let child = JodChild(mk_child(&path, args)?);
+ let child = JodChild(mk_child(&path, args, null_stderr)?);
Ok(Process { child })
}
@@ -83,13 +118,14 @@ impl Process {
fn mk_child(
path: &AbsPath,
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
+ null_stderr: bool,
) -> io::Result<Child> {
Command::new(path.as_os_str())
.args(args)
.env("RUST_ANALYZER_INTERNALS_DO_NOT_USE", "this is unstable")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
- .stderr(Stdio::inherit())
+ .stderr(if null_stderr { Stdio::null() } else { Stdio::inherit() })
.spawn()
}