Skip to content

Commit c40b089

Browse files
committed
Implement proc-macro-api versioning
1 parent 41a46a7 commit c40b089

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

crates/proc-macro-api/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl ProcMacroServer {
115115
/// Spawns an external process as the proc macro server and returns a client connected to it.
116116
pub fn spawn(
117117
process_path: AbsPathBuf,
118-
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
118+
args: impl IntoIterator<Item = impl AsRef<OsStr>> + Clone,
119119
) -> io::Result<ProcMacroServer> {
120120
let process = ProcMacroProcessSrv::run(process_path, args)?;
121121
Ok(ProcMacroServer { process: Arc::new(Mutex::new(process)) })
@@ -174,7 +174,7 @@ impl ProcMacro {
174174
let response = self.process.lock().unwrap_or_else(|e| e.into_inner()).send_task(request)?;
175175
match response {
176176
msg::Response::ExpandMacro(it) => Ok(it.map(FlatTree::to_subtree)),
177-
msg::Response::ListMacros { .. } => {
177+
msg::Response::ListMacros(..) | msg::Response::ApiVersionCheck(..) => {
178178
Err(ServerError { message: "unexpected response".to_string(), io: None })
179179
}
180180
}

crates/proc-macro-api/src/msg.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ use crate::ProcMacroKind;
1212

1313
pub use crate::msg::flat::FlatTree;
1414

15+
pub const NO_VERSION_CHECK_VERSION: u32 = 0;
16+
pub const API_VERSION: u32 = 1;
17+
1518
#[derive(Debug, Serialize, Deserialize)]
1619
pub enum Request {
1720
ListMacros { dylib_path: PathBuf },
1821
ExpandMacro(ExpandMacro),
22+
ApiVersionCheck {},
1923
}
2024

2125
#[derive(Debug, Serialize, Deserialize)]
2226
pub enum Response {
2327
ListMacros(Result<Vec<(String, ProcMacroKind)>, String>),
2428
ExpandMacro(Result<FlatTree, PanicMessage>),
29+
ApiVersionCheck(u32),
2530
}
2631

2732
#[derive(Debug, Serialize, Deserialize)]

crates/proc-macro-api/src/process.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,45 @@ pub(crate) struct ProcMacroProcessSrv {
1919
_process: Process,
2020
stdin: ChildStdin,
2121
stdout: BufReader<ChildStdout>,
22+
version: u32,
2223
}
2324

2425
impl 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
}

crates/proc-macro-srv/src/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub fn run() -> io::Result<()> {
1515
msg::Response::ListMacros(srv.list_macros(&dylib_path))
1616
}
1717
msg::Request::ExpandMacro(task) => msg::Response::ExpandMacro(srv.expand(task)),
18+
msg::Request::ApiVersionCheck {} => {
19+
msg::Response::ApiVersionCheck(proc_macro_api::msg::API_VERSION)
20+
}
1821
};
1922
write_response(res)?
2023
}

0 commit comments

Comments
 (0)