Skip to content

Commit c460a72

Browse files
authored
Merge pull request #21133 from Veykril/push-olvztmtwzmvv
fix: Do not try to connect via postcard to proc-macro-srv
2 parents 20fa8ab + c076ef3 commit c460a72

File tree

4 files changed

+54
-90
lines changed

4 files changed

+54
-90
lines changed

src/tools/rust-analyzer/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::Postcard { 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

src/tools/rust-analyzer/crates/proc-macro-api/src/process.rs

Lines changed: 40 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ pub(crate) struct ProcMacroServerProcess {
3030

3131
#[derive(Debug, Clone)]
3232
pub(crate) enum Protocol {
33-
LegacyJson { mode: SpanMode },
34-
Postcard { mode: SpanMode },
33+
LegacyJson {
34+
mode: SpanMode,
35+
},
36+
#[expect(dead_code)]
37+
Postcard {
38+
mode: SpanMode,
39+
},
3540
}
3641

3742
/// Maintains the state of the proc-macro server process.
@@ -49,82 +54,50 @@ impl ProcMacroServerProcess {
4954
env: impl IntoIterator<
5055
Item = (impl AsRef<std::ffi::OsStr>, &'a Option<impl 'a + AsRef<std::ffi::OsStr>>),
5156
> + Clone,
52-
protocol: Protocol,
5357
) -> io::Result<ProcMacroServerProcess> {
54-
let mut srv = {
55-
let mut process = match Process::run(process_path, env.clone(), &protocol) {
56-
Ok(process) => process,
57-
Err(e) => {
58-
// fallback
59-
if matches!(protocol, Protocol::Postcard { .. }) {
60-
// retry with json
61-
return Self::run(
62-
process_path,
63-
env,
64-
Protocol::LegacyJson { mode: SpanMode::Id },
65-
);
66-
}
67-
return Err(e);
68-
}
69-
};
58+
let create_srv = || {
59+
let mut process = Process::run(process_path, env.clone())?;
7060
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
7161

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

130103
/// Returns the server error if the process has exited.
@@ -247,9 +220,8 @@ impl Process {
247220
env: impl IntoIterator<
248221
Item = (impl AsRef<std::ffi::OsStr>, &'a Option<impl 'a + AsRef<std::ffi::OsStr>>),
249222
>,
250-
protocol: &Protocol,
251223
) -> io::Result<Process> {
252-
let child = JodChild(mk_child(path, env, protocol)?);
224+
let child = JodChild(mk_child(path, env)?);
253225
Ok(Process { child })
254226
}
255227

@@ -269,15 +241,9 @@ fn mk_child<'a>(
269241
extra_env: impl IntoIterator<
270242
Item = (impl AsRef<std::ffi::OsStr>, &'a Option<impl 'a + AsRef<std::ffi::OsStr>>),
271243
>,
272-
protocol: &Protocol,
273244
) -> io::Result<Child> {
274245
#[allow(clippy::disallowed_methods)]
275246
let mut cmd = Command::new(path);
276-
if matches!(protocol, Protocol::LegacyJson { .. }) {
277-
cmd.args(["--format", "json"]);
278-
} else {
279-
cmd.args(["--format", "postcard"]);
280-
}
281247
for env in extra_env {
282248
match env {
283249
(key, Some(val)) => cmd.env(key, val),

src/tools/rust-analyzer/crates/proc-macro-srv-cli/src/main.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn main() -> std::io::Result<()> {
3131
clap::Arg::new("format")
3232
.long("format")
3333
.action(clap::ArgAction::Set)
34-
.default_value("json")
34+
.default_value("json-legacy")
3535
.value_parser(clap::builder::EnumValueParser::<ProtocolFormat>::new()),
3636
clap::Arg::new("version")
3737
.long("version")
@@ -50,25 +50,27 @@ fn main() -> std::io::Result<()> {
5050

5151
#[derive(Copy, Clone)]
5252
enum ProtocolFormat {
53-
Json,
54-
Postcard,
53+
JsonLegacy,
54+
PostcardLegacy,
5555
}
5656

5757
impl ValueEnum for ProtocolFormat {
5858
fn value_variants<'a>() -> &'a [Self] {
59-
&[ProtocolFormat::Json, ProtocolFormat::Postcard]
59+
&[ProtocolFormat::JsonLegacy, ProtocolFormat::PostcardLegacy]
6060
}
6161

6262
fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
6363
match self {
64-
ProtocolFormat::Json => Some(clap::builder::PossibleValue::new("json")),
65-
ProtocolFormat::Postcard => Some(clap::builder::PossibleValue::new("postcard")),
64+
ProtocolFormat::JsonLegacy => Some(clap::builder::PossibleValue::new("json-legacy")),
65+
ProtocolFormat::PostcardLegacy => {
66+
Some(clap::builder::PossibleValue::new("postcard-legacy"))
67+
}
6668
}
6769
}
6870
fn from_str(input: &str, _ignore_case: bool) -> Result<Self, String> {
6971
match input {
70-
"json" => Ok(ProtocolFormat::Json),
71-
"postcard" => Ok(ProtocolFormat::Postcard),
72+
"json-legacy" => Ok(ProtocolFormat::JsonLegacy),
73+
"postcard-legacy" => Ok(ProtocolFormat::PostcardLegacy),
7274
_ => Err(format!("unknown protocol format: {input}")),
7375
}
7476
}

src/tools/rust-analyzer/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)