Skip to content

Commit c21860b

Browse files
committed
Remove proc-macro server command from the rust-analyzer binary
1 parent 943d2a8 commit c21860b

File tree

15 files changed

+109
-162
lines changed

15 files changed

+109
-162
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ mod version;
1313

1414
use paths::AbsPathBuf;
1515
use std::{
16-
ffi::OsStr,
1716
fmt, io,
1817
sync::{Arc, Mutex},
1918
};
@@ -103,11 +102,8 @@ pub struct MacroPanic {
103102

104103
impl ProcMacroServer {
105104
/// Spawns an external process as the proc macro server and returns a client connected to it.
106-
pub fn spawn(
107-
process_path: AbsPathBuf,
108-
args: impl IntoIterator<Item = impl AsRef<OsStr>> + Clone,
109-
) -> io::Result<ProcMacroServer> {
110-
let process = ProcMacroProcessSrv::run(process_path, args)?;
105+
pub fn spawn(process_path: AbsPathBuf) -> io::Result<ProcMacroServer> {
106+
let process = ProcMacroProcessSrv::run(process_path)?;
111107
Ok(ProcMacroServer { process: Arc::new(Mutex::new(process)) })
112108
}
113109

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

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Handle process life-time and message passing for proc-macro client
22
33
use std::{
4-
ffi::{OsStr, OsString},
54
io::{self, BufRead, BufReader, Write},
65
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
76
};
@@ -23,12 +22,9 @@ pub(crate) struct ProcMacroProcessSrv {
2322
}
2423

2524
impl ProcMacroProcessSrv {
26-
pub(crate) fn run(
27-
process_path: AbsPathBuf,
28-
args: impl IntoIterator<Item = impl AsRef<OsStr>> + Clone,
29-
) -> io::Result<ProcMacroProcessSrv> {
25+
pub(crate) fn run(process_path: AbsPathBuf) -> io::Result<ProcMacroProcessSrv> {
3026
let create_srv = |null_stderr| {
31-
let mut process = Process::run(process_path.clone(), args.clone(), null_stderr)?;
27+
let mut process = Process::run(process_path.clone(), null_stderr)?;
3228
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
3329

3430
io::Result::Ok(ProcMacroProcessSrv { _process: process, stdin, stdout, version: 0 })
@@ -100,13 +96,8 @@ struct Process {
10096
}
10197

10298
impl Process {
103-
fn run(
104-
path: AbsPathBuf,
105-
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
106-
null_stderr: bool,
107-
) -> io::Result<Process> {
108-
let args: Vec<OsString> = args.into_iter().map(|s| s.as_ref().into()).collect();
109-
let child = JodChild(mk_child(&path, args, null_stderr)?);
99+
fn run(path: AbsPathBuf, null_stderr: bool) -> io::Result<Process> {
100+
let child = JodChild(mk_child(&path, null_stderr)?);
110101
Ok(Process { child })
111102
}
112103

@@ -119,13 +110,8 @@ impl Process {
119110
}
120111
}
121112

122-
fn mk_child(
123-
path: &AbsPath,
124-
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
125-
null_stderr: bool,
126-
) -> io::Result<Child> {
113+
fn mk_child(path: &AbsPath, null_stderr: bool) -> io::Result<Child> {
127114
Command::new(path.as_os_str())
128-
.args(args)
129115
.env("RUST_ANALYZER_INTERNALS_DO_NOT_USE", "this is unstable")
130116
.stdin(Stdio::piped())
131117
.stdout(Stdio::piped())

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

-54
This file was deleted.

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

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! A standalone binary for `proc-macro-srv`.
22
//! Driver for proc macro server
3+
use std::io;
34

45
fn main() -> std::io::Result<()> {
56
let v = std::env::var("RUST_ANALYZER_INTERNALS_DO_NOT_USE");
@@ -14,5 +15,37 @@ fn main() -> std::io::Result<()> {
1415
}
1516
}
1617

17-
proc_macro_srv_cli::run()
18+
run()
19+
}
20+
21+
#[cfg(not(feature = "sysroot-abi"))]
22+
fn run() -> io::Result<()> {
23+
panic!("proc-macro-srv-cli requires the `sysroot-abi` feature to be enabled");
24+
}
25+
26+
#[cfg(feature = "sysroot-abi")]
27+
fn run() -> io::Result<()> {
28+
use proc_macro_api::msg::{self, Message};
29+
30+
let read_request = |buf: &mut String| msg::Request::read(&mut io::stdin().lock(), buf);
31+
32+
let write_response = |msg: msg::Response| msg.write(&mut io::stdout().lock());
33+
34+
let mut srv = proc_macro_srv::ProcMacroSrv::default();
35+
let mut buf = String::new();
36+
37+
while let Some(req) = read_request(&mut buf)? {
38+
let res = match req {
39+
msg::Request::ListMacros { dylib_path } => {
40+
msg::Response::ListMacros(srv.list_macros(&dylib_path))
41+
}
42+
msg::Request::ExpandMacro(task) => msg::Response::ExpandMacro(srv.expand(task)),
43+
msg::Request::ApiVersionCheck {} => {
44+
msg::Response::ApiVersionCheck(proc_macro_api::msg::CURRENT_API_VERSION)
45+
}
46+
};
47+
write_response(res)?
48+
}
49+
50+
Ok(())
1851
}

crates/project-model/src/workspace.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -459,18 +459,35 @@ impl ProjectWorkspace {
459459
}
460460
}
461461

462-
pub fn find_sysroot_proc_macro_srv(&self) -> Option<AbsPathBuf> {
462+
pub fn find_sysroot_proc_macro_srv(&self) -> Result<AbsPathBuf> {
463463
match self {
464464
ProjectWorkspace::Cargo { sysroot: Ok(sysroot), .. }
465-
| ProjectWorkspace::Json { sysroot: Ok(sysroot), .. } => {
465+
| ProjectWorkspace::Json { sysroot: Ok(sysroot), .. }
466+
| ProjectWorkspace::DetachedFiles { sysroot: Ok(sysroot), .. } => {
466467
let standalone_server_name =
467468
format!("rust-analyzer-proc-macro-srv{}", std::env::consts::EXE_SUFFIX);
468469
["libexec", "lib"]
469470
.into_iter()
470471
.map(|segment| sysroot.root().join(segment).join(&standalone_server_name))
471472
.find(|server_path| std::fs::metadata(server_path).is_ok())
473+
.ok_or_else(|| {
474+
anyhow::anyhow!(
475+
"cannot find proc-macro server in sysroot `{}`",
476+
sysroot.root().display()
477+
)
478+
})
479+
}
480+
ProjectWorkspace::DetachedFiles { .. } => {
481+
Err(anyhow::anyhow!("cannot find proc-macro server, no sysroot was found"))
472482
}
473-
_ => None,
483+
ProjectWorkspace::Cargo { cargo, .. } => Err(anyhow::anyhow!(
484+
"cannot find proc-macro-srv, the workspace `{}` is missing a sysroot",
485+
cargo.workspace_root().display()
486+
)),
487+
ProjectWorkspace::Json { project, .. } => Err(anyhow::anyhow!(
488+
"cannot find proc-macro-srv, the workspace `{}` is missing a sysroot",
489+
project.path().display()
490+
)),
474491
}
475492
}
476493

crates/rust-analyzer/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ ide-db.workspace = true
6767
ide-ssr.workspace = true
6868
ide.workspace = true
6969
proc-macro-api.workspace = true
70-
proc-macro-srv-cli.workspace = true
7170
profile.workspace = true
7271
project-model.workspace = true
7372
stdx.workspace = true
@@ -95,9 +94,7 @@ mbe.workspace = true
9594
[features]
9695
jemalloc = ["jemallocator", "profile/jemalloc"]
9796
force-always-assert = ["always-assert/force"]
98-
sysroot-abi = ["proc-macro-srv-cli/sysroot-abi"]
9997
in-rust-tree = [
100-
"sysroot-abi",
10198
"ide/in-rust-tree",
10299
"syntax/in-rust-tree",
103100
]

crates/rust-analyzer/src/bin/main.rs

-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ fn try_main(flags: flags::RustAnalyzer) -> Result<()> {
7676
}
7777
with_extra_thread("LspServer", run_server)?;
7878
}
79-
flags::RustAnalyzerCmd::ProcMacro(flags::ProcMacro) => {
80-
with_extra_thread("MacroExpander", || proc_macro_srv_cli::run().map_err(Into::into))?;
81-
}
8279
flags::RustAnalyzerCmd::Parse(cmd) => cmd.run()?,
8380
flags::RustAnalyzerCmd::Symbols(cmd) => cmd.run()?,
8481
flags::RustAnalyzerCmd::Highlight(cmd) => cmd.run()?,

crates/rust-analyzer/src/cli/flags.rs

-6
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ xflags::xflags! {
106106
optional --debug snippet: String
107107
}
108108

109-
cmd proc-macro {}
110-
111109
cmd lsif {
112110
required path: PathBuf
113111
}
@@ -141,7 +139,6 @@ pub enum RustAnalyzerCmd {
141139
Diagnostics(Diagnostics),
142140
Ssr(Ssr),
143141
Search(Search),
144-
ProcMacro(ProcMacro),
145142
Lsif(Lsif),
146143
Scip(Scip),
147144
}
@@ -203,9 +200,6 @@ pub struct Search {
203200
pub debug: Option<String>,
204201
}
205202

206-
#[derive(Debug)]
207-
pub struct ProcMacro;
208-
209203
#[derive(Debug)]
210204
pub struct Lsif {
211205
pub path: PathBuf,

crates/rust-analyzer/src/cli/load_cargo.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Loads a Cargo project into a static instance of analysis, without support
22
//! for incorporating changes.
3-
use std::{convert::identity, path::Path, sync::Arc};
3+
use std::{path::Path, sync::Arc};
44

5-
use anyhow::Result;
5+
use anyhow::{anyhow, Result};
66
use crossbeam_channel::{unbounded, Receiver};
77
use ide::{AnalysisHost, Change};
88
use ide_db::{
@@ -26,7 +26,7 @@ pub struct LoadCargoConfig {
2626
#[derive(Debug, Clone, PartialEq, Eq)]
2727
pub enum ProcMacroServerChoice {
2828
Sysroot,
29-
Explicit(AbsPathBuf, Vec<String>),
29+
Explicit(AbsPathBuf),
3030
None,
3131
}
3232

@@ -71,14 +71,11 @@ pub fn load_workspace(
7171
let proc_macro_server = match &load_config.with_proc_macro_server {
7272
ProcMacroServerChoice::Sysroot => ws
7373
.find_sysroot_proc_macro_srv()
74-
.ok_or_else(|| "failed to find sysroot proc-macro server".to_owned())
75-
.and_then(|it| {
76-
ProcMacroServer::spawn(it, identity::<&[&str]>(&[])).map_err(|e| e.to_string())
77-
}),
78-
ProcMacroServerChoice::Explicit(path, args) => {
79-
ProcMacroServer::spawn(path.clone(), args).map_err(|e| e.to_string())
74+
.and_then(|it| ProcMacroServer::spawn(it).map_err(Into::into)),
75+
ProcMacroServerChoice::Explicit(path) => {
76+
ProcMacroServer::spawn(path.clone()).map_err(Into::into)
8077
}
81-
ProcMacroServerChoice::None => Err("proc macro server disabled".to_owned()),
78+
ProcMacroServerChoice::None => Err(anyhow!("proc macro server disabled")),
8279
};
8380

8481
let (crate_graph, proc_macros) = ws.to_crate_graph(
@@ -93,7 +90,7 @@ pub fn load_workspace(
9390
let proc_macros = {
9491
let proc_macro_server = match &proc_macro_server {
9592
Ok(it) => Ok(it),
96-
Err(e) => Err(e.as_str()),
93+
Err(e) => Err(e.to_string()),
9794
};
9895
proc_macros
9996
.into_iter()
@@ -102,7 +99,11 @@ pub fn load_workspace(
10299
crate_id,
103100
path.map_or_else(
104101
|_| Err("proc macro crate is missing dylib".to_owned()),
105-
|(_, path)| load_proc_macro(proc_macro_server, &path, &[]),
102+
|(_, path)| {
103+
proc_macro_server.as_ref().map_err(Clone::clone).and_then(
104+
|proc_macro_server| load_proc_macro(proc_macro_server, &path, &[]),
105+
)
106+
},
106107
),
107108
)
108109
})

crates/rust-analyzer/src/config.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,7 @@ config_data! {
437437
///
438438
/// This config takes a map of crate names with the exported proc-macro names to ignore as values.
439439
procMacro_ignored: FxHashMap<Box<str>, Box<[Box<str>]>> = "{}",
440-
/// Internal config, path to proc-macro server executable (typically,
441-
/// this is rust-analyzer itself, but we override this in tests).
440+
/// Internal config, path to proc-macro server executable.
442441
procMacro_server: Option<PathBuf> = "null",
443442

444443
/// Exclude imports from find-all-references.
@@ -1102,17 +1101,13 @@ impl Config {
11021101
self.data.lru_query_capacities.is_empty().not().then(|| &self.data.lru_query_capacities)
11031102
}
11041103

1105-
pub fn proc_macro_srv(&self) -> Option<(AbsPathBuf, /* is path explicitly set */ bool)> {
1106-
if !self.data.procMacro_enable {
1107-
return None;
1108-
}
1109-
Some(match &self.data.procMacro_server {
1110-
Some(it) => (
1111-
AbsPathBuf::try_from(it.clone()).unwrap_or_else(|path| self.root_path.join(path)),
1112-
true,
1113-
),
1114-
None => (AbsPathBuf::assert(std::env::current_exe().ok()?), false),
1115-
})
1104+
pub fn proc_macro_srv(&self) -> Option<AbsPathBuf> {
1105+
self.data
1106+
.procMacro_server
1107+
.clone()
1108+
.map(AbsPathBuf::try_from)?
1109+
.ok()
1110+
.map(|path| self.root_path.join(path))
11161111
}
11171112

11181113
pub fn dummy_replacements(&self) -> &FxHashMap<Box<str>, Box<[Box<str>]>> {

crates/rust-analyzer/src/global_state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub(crate) struct GlobalState {
6363
pub(crate) source_root_config: SourceRootConfig,
6464

6565
pub(crate) proc_macro_changed: bool,
66-
pub(crate) proc_macro_clients: Arc<[Result<ProcMacroServer, String>]>,
66+
pub(crate) proc_macro_clients: Arc<[anyhow::Result<ProcMacroServer>]>,
6767

6868
pub(crate) flycheck: Arc<[FlycheckHandle]>,
6969
pub(crate) flycheck_sender: Sender<flycheck::Message>,

0 commit comments

Comments
 (0)