@@ -19,19 +19,45 @@ pub(crate) struct ProcMacroProcessSrv {
1919 _process : Process ,
2020 stdin : ChildStdin ,
2121 stdout : BufReader < ChildStdout > ,
22+ version : u32 ,
2223}
2324
2425impl ProcMacroProcessSrv {
2526 pub ( crate ) fn run (
2627 process_path : AbsPathBuf ,
27- args : impl IntoIterator < Item = impl AsRef < OsStr > > ,
28+ args : impl IntoIterator < Item = impl AsRef < OsStr > > + Clone ,
2829 ) -> io:: Result < ProcMacroProcessSrv > {
29- let mut process = Process :: run ( process_path, args) ?;
30- let ( stdin, stdout) = process. stdio ( ) . expect ( "couldn't access child stdio" ) ;
30+ let create_srv = || {
31+ let mut process = Process :: run ( process_path. clone ( ) , args. clone ( ) ) ?;
32+ let ( stdin, stdout) = process. stdio ( ) . expect ( "couldn't access child stdio" ) ;
33+
34+ io:: Result :: Ok ( ProcMacroProcessSrv { _process : process, stdin, stdout, version : 0 } )
35+ } ;
36+ let mut srv = create_srv ( ) ?;
37+ tracing:: info!( "sending version check" ) ;
38+ match srv. version_check ( ) {
39+ Ok ( v) => {
40+ tracing:: info!( "got version {v}" ) ;
41+ srv. version = v;
42+ Ok ( srv)
43+ }
44+ Err ( e) => {
45+ tracing:: info!( %e, "proc-macro version check failed, restarting and assuming version 0" ) ;
46+ create_srv ( )
47+ }
48+ }
49+ }
3150
32- let srv = ProcMacroProcessSrv { _process : process, stdin, stdout } ;
51+ pub ( crate ) fn version_check ( & mut self ) -> Result < u32 , ServerError > {
52+ let request = Request :: ApiVersionCheck { } ;
53+ let response = self . send_task ( request) ?;
3354
34- Ok ( srv)
55+ match response {
56+ Response :: ApiVersionCheck ( version) => Ok ( version) ,
57+ Response :: ExpandMacro { .. } | Response :: ListMacros { .. } => {
58+ Err ( ServerError { message : "unexpected response" . to_string ( ) , io : None } )
59+ }
60+ }
3561 }
3662
3763 pub ( crate ) fn find_proc_macros (
@@ -44,7 +70,7 @@ impl ProcMacroProcessSrv {
4470
4571 match response {
4672 Response :: ListMacros ( it) => Ok ( it) ,
47- Response :: ExpandMacro { .. } => {
73+ Response :: ExpandMacro { .. } | Response :: ApiVersionCheck { .. } => {
4874 Err ( ServerError { message : "unexpected response" . to_string ( ) , io : None } )
4975 }
5076 }
0 commit comments