Skip to content

Commit 268df15

Browse files
committed
Revert to spawning proc-macro-srv without format flag
1 parent 157e09e commit 268df15

File tree

3 files changed

+38
-81
lines changed

3 files changed

+38
-81
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use span::{ErasedFileAstId, FIXUP_ERASED_FILE_AST_ID_MARKER, Span};
2222
use std::{fmt, io, sync::Arc, time::SystemTime};
2323

2424
pub use crate::codec::Codec;
25-
use crate::{legacy_protocol::SpanMode, process::ProcMacroServerProcess};
25+
use crate::process::ProcMacroServerProcess;
2626

2727
/// The versions of the server protocol
2828
pub mod version {
@@ -126,11 +126,7 @@ impl ProcMacroClient {
126126
Item = (impl AsRef<std::ffi::OsStr>, &'a Option<impl 'a + AsRef<std::ffi::OsStr>>),
127127
> + Clone,
128128
) -> io::Result<ProcMacroClient> {
129-
let process = ProcMacroServerProcess::run(
130-
process_path,
131-
env,
132-
process::Protocol::LegacyJson { mode: SpanMode::Id },
133-
)?;
129+
let process = ProcMacroServerProcess::run(process_path, env)?;
134130
Ok(ProcMacroClient { process: Arc::new(process), path: process_path.to_owned() })
135131
}
136132

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

Lines changed: 34 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub(crate) enum Protocol {
3333
LegacyJson {
3434
mode: SpanMode,
3535
},
36-
#[expected(dead_code)]
36+
#[expect(dead_code)]
3737
Postcard {
3838
mode: SpanMode,
3939
},
@@ -54,82 +54,50 @@ impl ProcMacroServerProcess {
5454
env: impl IntoIterator<
5555
Item = (impl AsRef<std::ffi::OsStr>, &'a Option<impl 'a + AsRef<std::ffi::OsStr>>),
5656
> + Clone,
57-
protocol: Protocol,
5857
) -> io::Result<ProcMacroServerProcess> {
59-
let mut srv = {
60-
let mut process = match Process::run(process_path, env.clone(), &protocol) {
61-
Ok(process) => process,
62-
Err(e) => {
63-
// fallback
64-
if matches!(protocol, Protocol::Postcard { .. }) {
65-
// retry with json
66-
return Self::run(
67-
process_path,
68-
env,
69-
Protocol::LegacyJson { mode: SpanMode::Id },
70-
);
71-
}
72-
return Err(e);
73-
}
74-
};
58+
let create_srv = || {
59+
let mut process = Process::run(process_path, env.clone())?;
7560
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
7661

77-
ProcMacroServerProcess {
62+
io::Result::Ok(ProcMacroServerProcess {
7863
state: Mutex::new(ProcessSrvState { process, stdin, stdout }),
7964
version: 0,
80-
protocol: protocol.clone(),
65+
protocol: Protocol::LegacyJson { mode: SpanMode::Id },
8166
exited: OnceLock::new(),
82-
}
67+
})
8368
};
69+
let mut srv = create_srv()?;
8470
tracing::info!("sending proc-macro server version check");
85-
let version = match srv.version_check() {
86-
Ok(v) => v,
87-
Err(e) => {
88-
if matches!(protocol, Protocol::Postcard { .. }) {
89-
// retry with json
90-
return Self::run(
91-
process_path,
92-
env,
93-
Protocol::LegacyJson { mode: SpanMode::Id },
94-
);
71+
match srv.version_check() {
72+
Ok(v) if v > version::CURRENT_API_VERSION => {
73+
#[allow(clippy::disallowed_methods)]
74+
let process_version = Command::new(process_path)
75+
.arg("--version")
76+
.output()
77+
.map(|output| String::from_utf8_lossy(&output.stdout).trim().to_owned())
78+
.unwrap_or_else(|_| "unknown version".to_owned());
79+
Err(io::Error::other(format!(
80+
"Your installed proc-macro server is too new for your rust-analyzer. API version: {}, server version: {process_version}. \
81+
This will prevent proc-macro expansion from working. Please consider updating your rust-analyzer to ensure compatibility with your current toolchain.",
82+
version::CURRENT_API_VERSION
83+
)))
84+
}
85+
Ok(v) => {
86+
tracing::info!("Proc-macro server version: {v}");
87+
srv.version = v;
88+
if srv.version >= version::RUST_ANALYZER_SPAN_SUPPORT
89+
&& let Ok(mode) = srv.enable_rust_analyzer_spans()
90+
{
91+
srv.protocol = Protocol::LegacyJson { mode };
9592
}
96-
93+
tracing::info!("Proc-macro server protocol: {:?}", srv.protocol);
94+
Ok(srv)
95+
}
96+
Err(e) => {
9797
tracing::info!(%e, "proc-macro version check failed");
98-
return Err(io::Error::other(format!(
99-
"proc-macro server version check failed: {e}"
100-
)));
98+
Err(io::Error::other(format!("proc-macro server version check failed: {e}")))
10199
}
102-
};
103-
104-
if version > version::CURRENT_API_VERSION {
105-
#[allow(clippy::disallowed_methods)]
106-
let process_version = Command::new(process_path)
107-
.arg("--version")
108-
.output()
109-
.map(|out| String::from_utf8_lossy(&out.stdout).trim().to_owned())
110-
.unwrap_or_else(|_| "unknown version".to_owned());
111-
112-
return Err(io::Error::other(format!(
113-
"Your installed proc-macro server is too new for your rust-analyzer. API version: {}, server version: {process_version}. \
114-
This will prevent proc-macro expansion from working. Please consider updating your rust-analyzer to ensure compatibility with your current toolchain.",
115-
version::CURRENT_API_VERSION
116-
)));
117-
}
118-
119-
tracing::info!("proc-macro server version: {version}");
120-
121-
srv.version = version;
122-
123-
if version >= version::RUST_ANALYZER_SPAN_SUPPORT
124-
&& let Ok(new_mode) = srv.enable_rust_analyzer_spans()
125-
{
126-
match &mut srv.protocol {
127-
Protocol::Postcard { mode } | Protocol::LegacyJson { mode } => *mode = new_mode,
128-
};
129100
}
130-
131-
tracing::info!("proc-macro server protocol: {:?}", srv.protocol);
132-
Ok(srv)
133101
}
134102

135103
/// Returns the server error if the process has exited.
@@ -252,9 +220,8 @@ impl Process {
252220
env: impl IntoIterator<
253221
Item = (impl AsRef<std::ffi::OsStr>, &'a Option<impl 'a + AsRef<std::ffi::OsStr>>),
254222
>,
255-
protocol: &Protocol,
256223
) -> io::Result<Process> {
257-
let child = JodChild(mk_child(path, env, protocol)?);
224+
let child = JodChild(mk_child(path, env)?);
258225
Ok(Process { child })
259226
}
260227

@@ -274,15 +241,9 @@ fn mk_child<'a>(
274241
extra_env: impl IntoIterator<
275242
Item = (impl AsRef<std::ffi::OsStr>, &'a Option<impl 'a + AsRef<std::ffi::OsStr>>),
276243
>,
277-
protocol: &Protocol,
278244
) -> io::Result<Child> {
279245
#[allow(clippy::disallowed_methods)]
280246
let mut cmd = Command::new(path);
281-
if matches!(protocol, Protocol::LegacyJson { .. }) {
282-
cmd.args(["--format", "json-legacy"]);
283-
} else {
284-
cmd.args(["--format", "postcard-legacy"]);
285-
}
286247
for env in extra_env {
287248
match env {
288249
(key, Some(val)) => cmd.env(key, val),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ impl SpanTransformer for SpanTrans {
3737

3838
pub(crate) fn run(format: ProtocolFormat) -> io::Result<()> {
3939
match format {
40-
ProtocolFormat::Json => run_::<JsonProtocol>(),
41-
ProtocolFormat::Postcard => run_::<PostcardProtocol>(),
40+
ProtocolFormat::JsonLegacy => run_::<JsonProtocol>(),
41+
ProtocolFormat::PostcardLegacy => run_::<PostcardProtocol>(),
4242
}
4343
}
4444

0 commit comments

Comments
 (0)