Unnamed repository; edit this file 'description' to name the repository.
add review suggestions
| -rw-r--r-- | crates/proc-macro-api/src/legacy_protocol.rs | 26 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/legacy_protocol/json.rs | 4 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/legacy_protocol/msg.rs | 24 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/legacy_protocol/postcard.rs | 8 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/lib.rs | 5 | ||||
| -rw-r--r-- | crates/proc-macro-api/src/process.rs | 180 | ||||
| -rw-r--r-- | crates/proc-macro-srv-cli/src/main.rs | 2 |
7 files changed, 112 insertions, 137 deletions
diff --git a/crates/proc-macro-api/src/legacy_protocol.rs b/crates/proc-macro-api/src/legacy_protocol.rs index cb869fddbc..6d521d00cd 100644 --- a/crates/proc-macro-api/src/legacy_protocol.rs +++ b/crates/proc-macro-api/src/legacy_protocol.rs @@ -21,6 +21,7 @@ use crate::{ ServerConfig, SpanDataIndexMap, deserialize_span_data_index_map, flat::serialize_span_data_index_map, }, + postcard::{read_postcard, write_postcard}, }, process::ProcMacroServerProcess, version, @@ -153,7 +154,7 @@ fn send_task(srv: &ProcMacroServerProcess, req: Request) -> Result<Response, Ser } if srv.use_postcard() { - srv.send_task_bin(send_request_postcard, req) + srv.send_task(send_request_postcard, req) } else { srv.send_task(send_request, req) } @@ -183,27 +184,12 @@ fn send_request_postcard( req: Request, buf: &mut Vec<u8>, ) -> Result<Option<Response>, ServerError> { - let bytes = postcard::encode_cobs(&req) - .map_err(|_| ServerError { message: "failed to write request".into(), io: None })?; - - postcard::write_postcard(&mut writer, &bytes).map_err(|err| ServerError { + req.write_postcard(write_postcard, &mut writer).map_err(|err| ServerError { message: "failed to write request".into(), io: Some(Arc::new(err)), })?; - - let frame = postcard::read_postcard(&mut reader, buf).map_err(|err| ServerError { - message: "failed to read response".into(), - io: Some(Arc::new(err)), + let res = Response::read_postcard(read_postcard, &mut reader, buf).map_err(|err| { + ServerError { message: "failed to read response".into(), io: Some(Arc::new(err)) } })?; - - match frame { - None => Ok(None), - Some(bytes) => { - let resp: Response = postcard::decode_cobs(bytes).map_err(|e| ServerError { - message: format!("failed to decode message: {e}"), - io: None, - })?; - Ok(Some(resp)) - } - } + Ok(res) } diff --git a/crates/proc-macro-api/src/legacy_protocol/json.rs b/crates/proc-macro-api/src/legacy_protocol/json.rs index c8f774031b..cf8535f77d 100644 --- a/crates/proc-macro-api/src/legacy_protocol/json.rs +++ b/crates/proc-macro-api/src/legacy_protocol/json.rs @@ -5,7 +5,7 @@ use std::io::{self, BufRead, Write}; pub fn read_json<'a>( inp: &mut impl BufRead, buf: &'a mut String, -) -> io::Result<Option<&'a String>> { +) -> io::Result<Option<&'a mut String>> { loop { buf.clear(); @@ -28,7 +28,7 @@ pub fn read_json<'a>( } /// Writes a JSON message to the output stream. -pub fn write_json(out: &mut impl Write, msg: &str) -> io::Result<()> { +pub fn write_json(out: &mut impl Write, msg: &String) -> io::Result<()> { tracing::debug!("> {}", msg); out.write_all(msg.as_bytes())?; out.write_all(b"\n")?; diff --git a/crates/proc-macro-api/src/legacy_protocol/msg.rs b/crates/proc-macro-api/src/legacy_protocol/msg.rs index d3744dee0e..6df184630d 100644 --- a/crates/proc-macro-api/src/legacy_protocol/msg.rs +++ b/crates/proc-macro-api/src/legacy_protocol/msg.rs @@ -153,7 +153,7 @@ impl ExpnGlobals { pub trait Message: serde::Serialize + DeserializeOwned { fn read<R: BufRead>( - from_proto: ProtocolRead<R>, + from_proto: ProtocolRead<R, String>, inp: &mut R, buf: &mut String, ) -> io::Result<Option<Self>> { @@ -168,13 +168,13 @@ pub trait Message: serde::Serialize + DeserializeOwned { } }) } - fn write<W: Write>(self, to_proto: ProtocolWrite<W>, out: &mut W) -> io::Result<()> { + fn write<W: Write>(self, to_proto: ProtocolWrite<W, String>, out: &mut W) -> io::Result<()> { let text = serde_json::to_string(&self)?; to_proto(out, &text) } fn read_postcard<R: BufRead>( - from_proto: ProtocolReadPostcard<R>, + from_proto: ProtocolRead<R, Vec<u8>>, inp: &mut R, buf: &mut Vec<u8>, ) -> io::Result<Option<Self>> { @@ -186,7 +186,7 @@ pub trait Message: serde::Serialize + DeserializeOwned { fn write_postcard<W: Write>( self, - to_proto: ProtocolWritePostcard<W>, + to_proto: ProtocolWrite<W, Vec<u8>>, out: &mut W, ) -> io::Result<()> { let buf = encode_cobs(&self)?; @@ -199,20 +199,12 @@ impl Message for Response {} /// Type alias for a function that reads protocol messages from a buffered input stream. #[allow(type_alias_bounds)] -type ProtocolRead<R: BufRead> = - for<'i, 'buf> fn(inp: &'i mut R, buf: &'buf mut String) -> io::Result<Option<&'buf String>>; +type ProtocolRead<R: BufRead, Buf> = + for<'i, 'buf> fn(inp: &'i mut R, buf: &'buf mut Buf) -> io::Result<Option<&'buf mut Buf>>; /// Type alias for a function that writes protocol messages to an output stream. #[allow(type_alias_bounds)] -type ProtocolWrite<W: Write> = for<'o, 'msg> fn(out: &'o mut W, msg: &'msg str) -> io::Result<()>; - -/// Type alias for a function that reads protocol postcard messages from a buffered input stream. -#[allow(type_alias_bounds)] -type ProtocolReadPostcard<R: BufRead> = - for<'i, 'buf> fn(inp: &'i mut R, buf: &'buf mut Vec<u8>) -> io::Result<Option<&'buf mut [u8]>>; -/// Type alias for a function that writes protocol postcard messages to an output stream. -#[allow(type_alias_bounds)] -type ProtocolWritePostcard<W: Write> = - for<'o, 'msg> fn(out: &'o mut W, msg: &'msg [u8]) -> io::Result<()>; +type ProtocolWrite<W: Write, Buf> = + for<'o, 'msg> fn(out: &'o mut W, msg: &'msg Buf) -> io::Result<()>; #[cfg(test)] mod tests { diff --git a/crates/proc-macro-api/src/legacy_protocol/postcard.rs b/crates/proc-macro-api/src/legacy_protocol/postcard.rs index eab26439db..305e4de934 100644 --- a/crates/proc-macro-api/src/legacy_protocol/postcard.rs +++ b/crates/proc-macro-api/src/legacy_protocol/postcard.rs @@ -5,15 +5,17 @@ use std::io::{self, BufRead, Write}; pub fn read_postcard<'a>( input: &mut impl BufRead, buf: &'a mut Vec<u8>, -) -> io::Result<Option<&'a mut [u8]>> { +) -> io::Result<Option<&'a mut Vec<u8>>> { buf.clear(); let n = input.read_until(0, buf)?; if n == 0 { return Ok(None); } - Ok(Some(&mut buf[..])) + Ok(Some(buf)) } -pub fn write_postcard(out: &mut impl Write, msg: &[u8]) -> io::Result<()> { + +#[allow(clippy::ptr_arg)] +pub fn write_postcard(out: &mut impl Write, msg: &Vec<u8>) -> io::Result<()> { out.write_all(msg)?; out.flush() } diff --git a/crates/proc-macro-api/src/lib.rs b/crates/proc-macro-api/src/lib.rs index 877c45f56c..2cdb33ff81 100644 --- a/crates/proc-macro-api/src/lib.rs +++ b/crates/proc-macro-api/src/lib.rs @@ -31,10 +31,9 @@ pub mod version { /// Whether literals encode their kind as an additional u32 field and idents their rawness as a u32 field. pub const EXTENDED_LEAF_DATA: u32 = 5; pub const HASHED_AST_ID: u32 = 6; - pub const POSTCARD_WIRE: u32 = 7; /// Current API version of the proc-macro protocol. - pub const CURRENT_API_VERSION: u32 = POSTCARD_WIRE; + pub const CURRENT_API_VERSION: u32 = HASHED_AST_ID; } /// Represents different kinds of procedural macros that can be expanded by the external server. @@ -124,7 +123,7 @@ impl ProcMacroClient { Item = (impl AsRef<std::ffi::OsStr>, &'a Option<impl 'a + AsRef<std::ffi::OsStr>>), > + Clone, ) -> io::Result<ProcMacroClient> { - let process = ProcMacroServerProcess::run(process_path, env)?; + let process = ProcMacroServerProcess::run(process_path, env, process::Protocol::default())?; Ok(ProcMacroClient { process: Arc::new(process), path: process_path.to_owned() }) } diff --git a/crates/proc-macro-api/src/process.rs b/crates/proc-macro-api/src/process.rs index 95b12d0b24..7f0cd05c80 100644 --- a/crates/proc-macro-api/src/process.rs +++ b/crates/proc-macro-api/src/process.rs @@ -28,12 +28,18 @@ pub(crate) struct ProcMacroServerProcess { exited: OnceLock<AssertUnwindSafe<ServerError>>, } -#[derive(Debug)] -enum Protocol { +#[derive(Debug, Clone)] +pub(crate) enum Protocol { LegacyJson { mode: SpanMode }, Postcard { mode: SpanMode }, } +impl Default for Protocol { + fn default() -> Self { + Protocol::Postcard { mode: SpanMode::Id } + } +} + /// Maintains the state of the proc-macro server process. #[derive(Debug)] struct ProcessSrvState { @@ -49,54 +55,83 @@ impl ProcMacroServerProcess { env: impl IntoIterator< Item = (impl AsRef<std::ffi::OsStr>, &'a Option<impl 'a + AsRef<std::ffi::OsStr>>), > + Clone, + protocol: Protocol, ) -> io::Result<ProcMacroServerProcess> { - let create_srv = || { - let mut process = Process::run(process_path, env.clone())?; + let mut srv = { + let mut process = match Process::run(process_path, env.clone(), &protocol) { + Ok(process) => process, + Err(e) => { + // fallback + if matches!(protocol, Protocol::Postcard { .. }) { + // retry with json + return Self::run( + process_path, + env, + Protocol::LegacyJson { mode: SpanMode::Id }, + ); + } + return Err(e); + } + }; let (stdin, stdout) = process.stdio().expect("couldn't access child stdio"); - io::Result::Ok(ProcMacroServerProcess { + ProcMacroServerProcess { state: Mutex::new(ProcessSrvState { process, stdin, stdout }), version: 0, - protocol: Protocol::LegacyJson { mode: SpanMode::Id }, + protocol: protocol.clone(), exited: OnceLock::new(), - }) + } }; - let mut srv = create_srv()?; tracing::info!("sending proc-macro server version check"); - match srv.version_check() { - Ok(v) if v > version::CURRENT_API_VERSION => { - #[allow(clippy::disallowed_methods)] - let process_version = Command::new(process_path) - .arg("--version") - .output() - .map(|output| String::from_utf8_lossy(&output.stdout).trim().to_owned()) - .unwrap_or_else(|_| "unknown version".to_owned()); - Err(io::Error::other(format!( - "Your installed proc-macro server is too new for your rust-analyzer. API version: {}, server version: {process_version}. \ - This will prevent proc-macro expansion from working. Please consider updating your rust-analyzer to ensure compatibility with your current toolchain.", - version::CURRENT_API_VERSION - ))) - } - Ok(v) => { - tracing::info!("Proc-macro server version: {v}"); - srv.version = v; - if srv.version >= version::RUST_ANALYZER_SPAN_SUPPORT - && let Ok(mode) = srv.enable_rust_analyzer_spans() - { - if srv.version >= version::POSTCARD_WIRE { - srv.protocol = Protocol::Postcard { mode }; - } else { - srv.protocol = Protocol::LegacyJson { mode }; - } - } - tracing::info!("Proc-macro server protocol: {:?}", srv.protocol); - Ok(srv) - } + let version = match srv.version_check() { + Ok(v) => v, Err(e) => { + if matches!(protocol, Protocol::Postcard { .. }) { + // retry with json + return Self::run( + process_path, + env, + Protocol::LegacyJson { mode: SpanMode::Id }, + ); + } + tracing::info!(%e, "proc-macro version check failed"); - Err(io::Error::other(format!("proc-macro server version check failed: {e}"))) + return Err(io::Error::other(format!( + "proc-macro server version check failed: {e}" + ))); } + }; + + if version > version::CURRENT_API_VERSION { + #[allow(clippy::disallowed_methods)] + let process_version = Command::new(process_path) + .arg("--version") + .output() + .map(|out| String::from_utf8_lossy(&out.stdout).trim().to_owned()) + .unwrap_or_else(|_| "unknown version".to_owned()); + + return Err(io::Error::other(format!( + "Your installed proc-macro server is too new for your rust-analyzer. API version: {}, server version: {process_version}. \ + This will prevent proc-macro expansion from working. Please consider updating your rust-analyzer to ensure compatibility with your current toolchain.", + version::CURRENT_API_VERSION + ))); + } + + tracing::info!("proc-macro server version: {version}"); + + srv.version = version; + + if version >= version::RUST_ANALYZER_SPAN_SUPPORT + && let Ok(mode) = srv.enable_rust_analyzer_spans() + { + srv.protocol = match protocol { + Protocol::Postcard { .. } => Protocol::Postcard { mode }, + Protocol::LegacyJson { .. } => Protocol::LegacyJson { mode }, + }; } + + tracing::info!("proc-macro server protocol: {:?}", srv.protocol); + Ok(srv) } /// Returns the server error if the process has exited. @@ -148,18 +183,21 @@ impl ProcMacroServerProcess { } } - pub(crate) fn send_task<Request, Response>( + pub(crate) fn send_task<Request, Response, Buf>( &self, serialize_req: impl FnOnce( &mut dyn Write, &mut dyn BufRead, Request, - &mut String, + &mut Buf, ) -> Result<Option<Response>, ServerError>, req: Request, - ) -> Result<Response, ServerError> { + ) -> Result<Response, ServerError> + where + Buf: Default, + { let state = &mut *self.state.lock().unwrap(); - let mut buf = String::new(); + let mut buf = Buf::default(); serialize_req(&mut state.stdin, &mut state.stdout, req, &mut buf) .and_then(|res| { res.ok_or_else(|| { @@ -201,55 +239,6 @@ impl ProcMacroServerProcess { } }) } - - pub(crate) fn send_task_bin<Request, Response>( - &self, - serialize_req: impl FnOnce( - &mut dyn Write, - &mut dyn BufRead, - Request, - &mut Vec<u8>, - ) -> Result<Option<Response>, ServerError>, - req: Request, - ) -> Result<Response, ServerError> { - let state = &mut *self.state.lock().unwrap(); - let mut buf = Vec::<u8>::new(); - serialize_req(&mut state.stdin, &mut state.stdout, req, &mut buf) - .and_then(|res| { - res.ok_or_else(|| ServerError { - message: "proc-macro server did not respond with data".to_owned(), - io: Some(Arc::new(io::Error::new( - io::ErrorKind::BrokenPipe, - "proc-macro server did not respond with data", - ))), - }) - }) - .map_err(|e| { - if e.io.as_ref().map(|it| it.kind()) == Some(io::ErrorKind::BrokenPipe) { - match state.process.child.try_wait() { - Ok(None) | Err(_) => e, - Ok(Some(status)) => { - let mut msg = String::new(); - if !status.success() - && let Some(stderr) = state.process.child.stderr.as_mut() - { - _ = stderr.read_to_string(&mut msg); - } - let server_error = ServerError { - message: format!( - "proc-macro server exited with {status}{}{msg}", - if msg.is_empty() { "" } else { ": " } - ), - io: None, - }; - self.exited.get_or_init(|| AssertUnwindSafe(server_error)).0.clone() - } - } - } else { - e - } - }) - } } /// Manages the execution of the proc-macro server process. @@ -265,8 +254,9 @@ impl Process { env: impl IntoIterator< Item = (impl AsRef<std::ffi::OsStr>, &'a Option<impl 'a + AsRef<std::ffi::OsStr>>), >, + protocol: &Protocol, ) -> io::Result<Process> { - let child = JodChild(mk_child(path, env)?); + let child = JodChild(mk_child(path, env, protocol)?); Ok(Process { child }) } @@ -286,9 +276,15 @@ fn mk_child<'a>( extra_env: impl IntoIterator< Item = (impl AsRef<std::ffi::OsStr>, &'a Option<impl 'a + AsRef<std::ffi::OsStr>>), >, + protocol: &Protocol, ) -> io::Result<Child> { #[allow(clippy::disallowed_methods)] let mut cmd = Command::new(path); + if matches!(protocol, Protocol::LegacyJson { .. }) { + cmd.args(["--format", "json"]); + } else { + cmd.args(["--format", "postcard"]); + } for env in extra_env { match env { (key, Some(val)) => cmd.env(key, val), diff --git a/crates/proc-macro-srv-cli/src/main.rs b/crates/proc-macro-srv-cli/src/main.rs index 9149adb468..b6c38da454 100644 --- a/crates/proc-macro-srv-cli/src/main.rs +++ b/crates/proc-macro-srv-cli/src/main.rs @@ -31,7 +31,7 @@ fn main() -> std::io::Result<()> { clap::Arg::new("format") .long("format") .action(clap::ArgAction::Set) - .default_value("postcard") + .default_value("json") .value_parser(clap::builder::EnumValueParser::<ProtocolFormat>::new()), clap::Arg::new("version") .long("version") |