From c40b0895f09777be72bb6e875c1c0aefb9ccf316 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 1 Feb 2023 11:04:20 +0100 Subject: [PATCH 1/2] Implement proc-macro-api versioning --- crates/proc-macro-api/src/lib.rs | 4 +-- crates/proc-macro-api/src/msg.rs | 5 ++++ crates/proc-macro-api/src/process.rs | 38 +++++++++++++++++++++++----- crates/proc-macro-srv/src/cli.rs | 3 +++ 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/crates/proc-macro-api/src/lib.rs b/crates/proc-macro-api/src/lib.rs index bb381c4d44e..3f5e8a5e80b 100644 --- a/crates/proc-macro-api/src/lib.rs +++ b/crates/proc-macro-api/src/lib.rs @@ -115,7 +115,7 @@ impl ProcMacroServer { /// Spawns an external process as the proc macro server and returns a client connected to it. pub fn spawn( process_path: AbsPathBuf, - args: impl IntoIterator>, + args: impl IntoIterator> + Clone, ) -> io::Result { let process = ProcMacroProcessSrv::run(process_path, args)?; Ok(ProcMacroServer { process: Arc::new(Mutex::new(process)) }) @@ -174,7 +174,7 @@ impl ProcMacro { let response = self.process.lock().unwrap_or_else(|e| e.into_inner()).send_task(request)?; match response { msg::Response::ExpandMacro(it) => Ok(it.map(FlatTree::to_subtree)), - msg::Response::ListMacros { .. } => { + msg::Response::ListMacros(..) | msg::Response::ApiVersionCheck(..) => { Err(ServerError { message: "unexpected response".to_string(), io: None }) } } diff --git a/crates/proc-macro-api/src/msg.rs b/crates/proc-macro-api/src/msg.rs index 9b7bcaeffe7..262483d2cbe 100644 --- a/crates/proc-macro-api/src/msg.rs +++ b/crates/proc-macro-api/src/msg.rs @@ -12,16 +12,21 @@ use crate::ProcMacroKind; pub use crate::msg::flat::FlatTree; +pub const NO_VERSION_CHECK_VERSION: u32 = 0; +pub const API_VERSION: u32 = 1; + #[derive(Debug, Serialize, Deserialize)] pub enum Request { ListMacros { dylib_path: PathBuf }, ExpandMacro(ExpandMacro), + ApiVersionCheck {}, } #[derive(Debug, Serialize, Deserialize)] pub enum Response { ListMacros(Result, String>), ExpandMacro(Result), + ApiVersionCheck(u32), } #[derive(Debug, Serialize, Deserialize)] diff --git a/crates/proc-macro-api/src/process.rs b/crates/proc-macro-api/src/process.rs index 54dcb17f4e8..da4527c64f2 100644 --- a/crates/proc-macro-api/src/process.rs +++ b/crates/proc-macro-api/src/process.rs @@ -19,19 +19,45 @@ pub(crate) struct ProcMacroProcessSrv { _process: Process, stdin: ChildStdin, stdout: BufReader, + version: u32, } impl ProcMacroProcessSrv { pub(crate) fn run( process_path: AbsPathBuf, - args: impl IntoIterator>, + args: impl IntoIterator> + Clone, ) -> io::Result { - let mut process = Process::run(process_path, args)?; - let (stdin, stdout) = process.stdio().expect("couldn't access child stdio"); + let create_srv = || { + let mut process = Process::run(process_path.clone(), args.clone())?; + let (stdin, stdout) = process.stdio().expect("couldn't access child stdio"); - let srv = ProcMacroProcessSrv { _process: process, stdin, stdout }; + io::Result::Ok(ProcMacroProcessSrv { _process: process, stdin, stdout, version: 0 }) + }; + let mut srv = create_srv()?; + tracing::info!("sending version check"); + match srv.version_check() { + Ok(v) => { + tracing::info!("got version {v}"); + srv.version = v; + Ok(srv) + } + Err(e) => { + tracing::info!(%e, "proc-macro version check failed, restarting and assuming version 0"); + create_srv() + } + } + } - Ok(srv) + pub(crate) fn version_check(&mut self) -> Result { + let request = Request::ApiVersionCheck {}; + let response = self.send_task(request)?; + + 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 +70,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 }) } } diff --git a/crates/proc-macro-srv/src/cli.rs b/crates/proc-macro-srv/src/cli.rs index f1e131c135d..eaf94714dd7 100644 --- a/crates/proc-macro-srv/src/cli.rs +++ b/crates/proc-macro-srv/src/cli.rs @@ -15,6 +15,9 @@ pub fn run() -> io::Result<()> { msg::Response::ListMacros(srv.list_macros(&dylib_path)) } msg::Request::ExpandMacro(task) => msg::Response::ExpandMacro(srv.expand(task)), + msg::Request::ApiVersionCheck {} => { + msg::Response::ApiVersionCheck(proc_macro_api::msg::API_VERSION) + } }; write_response(res)? } From 8e998c4aa79aab1fa0041bbc929f36b82a6c3aeb Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 3 Feb 2023 10:38:38 +0100 Subject: [PATCH 2/2] Fail spawning proc-macro servers when their api version is newer than r-a's --- crates/proc-macro-api/src/msg.rs | 2 +- crates/proc-macro-api/src/process.rs | 9 ++++++++- crates/proc-macro-srv/src/cli.rs | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/proc-macro-api/src/msg.rs b/crates/proc-macro-api/src/msg.rs index 262483d2cbe..4040efe93f0 100644 --- a/crates/proc-macro-api/src/msg.rs +++ b/crates/proc-macro-api/src/msg.rs @@ -13,7 +13,7 @@ use crate::ProcMacroKind; pub use crate::msg::flat::FlatTree; pub const NO_VERSION_CHECK_VERSION: u32 = 0; -pub const API_VERSION: u32 = 1; +pub const CURRENT_API_VERSION: u32 = 1; #[derive(Debug, Serialize, Deserialize)] pub enum Request { diff --git a/crates/proc-macro-api/src/process.rs b/crates/proc-macro-api/src/process.rs index da4527c64f2..e70b3850d66 100644 --- a/crates/proc-macro-api/src/process.rs +++ b/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, }; @@ -36,6 +36,13 @@ impl ProcMacroProcessSrv { let mut srv = create_srv()?; 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.version = v; diff --git a/crates/proc-macro-srv/src/cli.rs b/crates/proc-macro-srv/src/cli.rs index eaf94714dd7..05168feb629 100644 --- a/crates/proc-macro-srv/src/cli.rs +++ b/crates/proc-macro-srv/src/cli.rs @@ -16,7 +16,7 @@ pub fn run() -> io::Result<()> { } msg::Request::ExpandMacro(task) => msg::Response::ExpandMacro(srv.expand(task)), msg::Request::ApiVersionCheck {} => { - msg::Response::ApiVersionCheck(proc_macro_api::msg::API_VERSION) + msg::Response::ApiVersionCheck(proc_macro_api::msg::CURRENT_API_VERSION) } }; write_response(res)?