Do not require a special env var to be set for the proc-macro-srv

This commit is contained in:
Lukas Wirth 2024-12-12 15:09:18 +01:00
parent acd469681f
commit a5adfafd93

View file

@ -6,21 +6,16 @@
#[cfg(feature = "in-rust-tree")] #[cfg(feature = "in-rust-tree")]
extern crate rustc_driver as _; extern crate rustc_driver as _;
use proc_macro_api::json::{read_json, write_json};
use std::io; use std::io;
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let v = std::env::var("RUST_ANALYZER_INTERNALS_DO_NOT_USE"); let v = std::env::var("RUST_ANALYZER_INTERNALS_DO_NOT_USE");
match v.as_deref() { if v.is_err() {
Ok("this is unstable") => { eprintln!("This is an IDE implementation detail, you can use this tool by exporting RUST_ANALYZER_INTERNALS_DO_NOT_USE.");
// very well, if you must eprintln!(
} "Note that this tool's API is highly unstable and may break without prior notice"
_ => { );
eprintln!("If you're rust-analyzer, you can use this tool by exporting RUST_ANALYZER_INTERNALS_DO_NOT_USE='this is unstable'."); std::process::exit(122);
eprintln!("If not, you probably shouldn't use this tool. But do what you want: I'm an error message, not a cop.");
std::process::exit(122);
}
} }
run() run()
@ -28,40 +23,19 @@ fn main() -> std::io::Result<()> {
#[cfg(not(any(feature = "sysroot-abi", rust_analyzer)))] #[cfg(not(any(feature = "sysroot-abi", rust_analyzer)))]
fn run() -> io::Result<()> { fn run() -> io::Result<()> {
let err = "proc-macro-srv-cli needs to be compiled with the `sysroot-abi` feature to function"; Err(io::Error::new(
eprintln!("{err}"); io::ErrorKind::Unsupported,
use proc_macro_api::msg::{self, Message}; "proc-macro-srv-cli needs to be compiled with the `sysroot-abi` feature to function"
.to_owned(),
let read_request = ))
|buf: &mut String| msg::Request::read(read_json, &mut io::stdin().lock(), buf);
let write_response = |msg: msg::Response| msg.write(write_json, &mut io::stdout().lock());
let mut buf = String::new();
while let Some(req) = read_request(&mut buf)? {
let res = match req {
msg::Request::ListMacros { .. } => msg::Response::ListMacros(Err(err.to_owned())),
msg::Request::ExpandMacro(_) => {
msg::Response::ExpandMacro(Err(msg::PanicMessage(err.to_owned())))
}
msg::Request::ApiVersionCheck {} => {
msg::Response::ApiVersionCheck(proc_macro_api::msg::CURRENT_API_VERSION)
}
msg::Request::SetConfig(_) => {
msg::Response::SetConfig(proc_macro_api::msg::ServerConfig {
span_mode: msg::SpanMode::Id,
})
}
};
write_response(res)?
}
Ok(())
} }
#[cfg(any(feature = "sysroot-abi", rust_analyzer))] #[cfg(any(feature = "sysroot-abi", rust_analyzer))]
fn run() -> io::Result<()> { fn run() -> io::Result<()> {
use proc_macro_api::msg::{self, Message}; use proc_macro_api::{
json::{read_json, write_json},
msg::{self, Message},
};
use proc_macro_srv::EnvSnapshot; use proc_macro_srv::EnvSnapshot;
let read_request = let read_request =