diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 13a11c95b0f..9068064f7ac 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -85,9 +85,9 @@ jobs: - name: Compile run: cargo test --no-run - # We have to build ra_proc_macro_srv first for running related heavy tests - - name: Build ra_proc_macro_srv - run: cargo build -p ra_proc_macro_srv + # We have to build rust-analyzer first for running related heavy tests + - name: Build rust-analyzer + run: cargo build -p rust-analyzer - name: Test run: cargo test diff --git a/Cargo.lock b/Cargo.lock index 01fa64e6fe4..8e204d39ff2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1341,6 +1341,7 @@ dependencies = [ "ra_hir_def", "ra_hir_ty", "ra_ide", + "ra_proc_macro_srv", "ra_prof", "ra_project_model", "ra_syntax", diff --git a/crates/ra_proc_macro/src/lib.rs b/crates/ra_proc_macro/src/lib.rs index 63da9f1b45b..14a675db224 100644 --- a/crates/ra_proc_macro/src/lib.rs +++ b/crates/ra_proc_macro/src/lib.rs @@ -56,8 +56,11 @@ pub struct ProcMacroClient { } impl ProcMacroClient { - pub fn extern_process(process_path: &Path) -> Result { - let (thread, process) = ProcMacroProcessSrv::run(process_path)?; + pub fn extern_process>( + process_path: &Path, + args: &[T], + ) -> Result { + let (thread, process) = ProcMacroProcessSrv::run(process_path, args)?; Ok(ProcMacroClient { kind: ProcMacroClientKind::Process { process: Arc::new(process), thread }, }) diff --git a/crates/ra_proc_macro/src/process.rs b/crates/ra_proc_macro/src/process.rs index e8c85be38ae..b5e8493ac82 100644 --- a/crates/ra_proc_macro/src/process.rs +++ b/crates/ra_proc_macro/src/process.rs @@ -44,8 +44,9 @@ impl Drop for Process { } impl Process { - fn run(process_path: &Path) -> Result { + fn run>(process_path: &Path, args: &[T]) -> Result { let child = Command::new(process_path.clone()) + .args(args.iter().map(|it| it.as_ref())) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::null()) @@ -74,10 +75,11 @@ impl Process { } impl ProcMacroProcessSrv { - pub fn run( + pub fn run>( process_path: &Path, + args: &[T], ) -> Result<(ProcMacroProcessThread, ProcMacroProcessSrv), io::Error> { - let process = Process::run(process_path)?; + let process = Process::run(process_path, args)?; let (task_tx, task_rx) = bounded(0); let handle = jod_thread::spawn(move || { diff --git a/crates/ra_proc_macro_srv/src/main.rs b/crates/ra_proc_macro_srv/src/cli.rs similarity index 96% rename from crates/ra_proc_macro_srv/src/main.rs rename to crates/ra_proc_macro_srv/src/cli.rs index 70743c1f40e..c771f2b3891 100644 --- a/crates/ra_proc_macro_srv/src/main.rs +++ b/crates/ra_proc_macro_srv/src/cli.rs @@ -1,7 +1,7 @@ //! Driver for proc macro server +use crate::{expand_task, list_macros}; use ra_proc_macro::msg::{self, Message}; -use ra_proc_macro_srv::{expand_task, list_macros}; use std::io; @@ -24,7 +24,8 @@ fn write_response(res: Result) -> Result<(), io::Error> { let mut stdout = stdout.lock(); msg.write(&mut stdout) } -fn main() { + +pub fn run() { loop { let req = match read_request() { Err(err) => { diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index 59716cbb3b0..c62b0ed893c 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -22,7 +22,7 @@ mod dylib; use proc_macro::bridge::client::TokenStream; use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; -pub fn expand_task(task: &ExpansionTask) -> Result { +pub(crate) fn expand_task(task: &ExpansionTask) -> Result { let expander = dylib::Expander::new(&task.lib) .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib)); @@ -39,7 +39,7 @@ pub fn expand_task(task: &ExpansionTask) -> Result { } } -pub fn list_macros(task: &ListMacrosTask) -> Result { +pub(crate) fn list_macros(task: &ListMacrosTask) -> Result { let expander = dylib::Expander::new(&task.lib) .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib)); @@ -53,5 +53,7 @@ pub fn list_macros(task: &ListMacrosTask) -> Result { } } +pub mod cli; + #[cfg(test)] mod tests; diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index f5f77343258..cee0248b62c 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml @@ -46,7 +46,7 @@ ra_db = { path = "../ra_db" } hir = { path = "../ra_hir", package = "ra_hir" } hir_def = { path = "../ra_hir_def", package = "ra_hir_def" } hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" } - +ra_proc_macro_srv = { path = "../ra_proc_macro_srv" } [target.'cfg(windows)'.dependencies] winapi = "0.3.8" diff --git a/crates/rust-analyzer/src/bin/args.rs b/crates/rust-analyzer/src/bin/args.rs index ec93e9e4e5f..5e19253a65c 100644 --- a/crates/rust-analyzer/src/bin/args.rs +++ b/crates/rust-analyzer/src/bin/args.rs @@ -45,6 +45,7 @@ pub(crate) enum Command { /// this would include the parser test files. all: bool, }, + ProcMacro, RunServer, Version, } @@ -264,6 +265,7 @@ ARGS: Command::Diagnostics { path, load_output_dirs, with_proc_macro, all } } + "proc-macro" => Command::ProcMacro, _ => { eprintln!( "\ diff --git a/crates/rust-analyzer/src/bin/main.rs b/crates/rust-analyzer/src/bin/main.rs index 5f614a01386..28b67cfe2aa 100644 --- a/crates/rust-analyzer/src/bin/main.rs +++ b/crates/rust-analyzer/src/bin/main.rs @@ -51,6 +51,7 @@ fn main() -> Result<()> { cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro, all)? } + args::Command::ProcMacro => run_proc_macro_sv()?, args::Command::RunServer => run_server()?, args::Command::Version => println!("rust-analyzer {}", env!("REV")), } @@ -64,6 +65,11 @@ fn setup_logging() -> Result<()> { Ok(()) } +fn run_proc_macro_sv() -> Result<()> { + ra_proc_macro_srv::cli::run(); + Ok(()) +} + fn run_server() -> Result<()> { log::info!("lifecycle: server started"); diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs index dfda488fb83..32a9ee339ad 100644 --- a/crates/rust-analyzer/src/cli/load_cargo.rs +++ b/crates/rust-analyzer/src/cli/load_cargo.rs @@ -73,7 +73,10 @@ pub(crate) fn load_cargo( let proc_macro_client = if !with_proc_macro { ProcMacroClient::dummy() } else { - ProcMacroClient::extern_process(Path::new("ra_proc_macro_srv")).unwrap() + let mut path = std::env::current_exe()?; + path.pop(); + path.push("rust-analyzer"); + ProcMacroClient::extern_process(&path, &["proc-macro"]).unwrap() }; let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs, &proc_macro_client); Ok((host, source_roots)) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 46a89b37ed6..2b45f131031 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -20,7 +20,7 @@ pub struct Config { pub with_sysroot: bool, pub publish_diagnostics: bool, pub lru_capacity: Option, - pub proc_macro_srv: Option, + pub proc_macro_srv: Option<(String, Vec)>, pub files: FilesConfig, pub notifications: NotificationsConfig, @@ -134,7 +134,11 @@ impl Config { match get::(value, "/procMacro/enabled") { Some(true) => { - set(value, "/procMacro/serverPath", &mut self.proc_macro_srv); + if let Ok(mut path) = std::env::current_exe() { + path.pop(); + path.push("rust-analyzer"); + self.proc_macro_srv = Some((path.to_string_lossy().to_string(), vec!["proc-macro".to_string()])); + } } _ => self.proc_macro_srv = None, } diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs index b7142e83ba1..f2ad453fafe 100644 --- a/crates/rust-analyzer/src/world.rs +++ b/crates/rust-analyzer/src/world.rs @@ -148,9 +148,9 @@ impl WorldState { let proc_macro_client = match &config.proc_macro_srv { None => ProcMacroClient::dummy(), - Some(srv) => { - let path = Path::new(&srv); - match ProcMacroClient::extern_process(path) { + Some((path, args)) => { + let path = std::path::Path::new(path); + match ProcMacroClient::extern_process(path, args) { Ok(it) => it, Err(err) => { log::error!( diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs index 26ab81a8f6e..1dd2676b6e9 100644 --- a/crates/rust-analyzer/tests/heavy_tests/main.rs +++ b/crates/rust-analyzer/tests/heavy_tests/main.rs @@ -692,13 +692,15 @@ pub fn foo(_input: TokenStream) -> TokenStream { "###, ) .with_config(|config| { + // FIXME: Use env!("CARGO_BIN_EXE_ra-analyzer") instead after + // https://github.com/rust-lang/cargo/pull/7697 landed let macro_srv_path = std::path::Path::new(std::env!("CARGO_MANIFEST_DIR")) - .join("../../target/debug/ra_proc_macro_srv") + .join("../../target/debug/rust-analyzer") .to_string_lossy() .to_string(); config.cargo.load_out_dirs_from_check = true; - config.proc_macro_srv = Some(macro_srv_path) + config.proc_macro_srv = Some((macro_srv_path, vec!["proc-macro".to_string()])); }) .root("foo") .root("bar") diff --git a/editors/code/package.json b/editors/code/package.json index d39a9b6d883..5ce59e54a96 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -393,11 +393,6 @@ "description": "Enable Proc macro support, cargo.loadOutDirsFromCheck must be enabled.", "type": "boolean", "default": false - }, - "rust-analyzer.procMacro.serverPath": { - "description": "Proc macro server path", - "type": "string", - "default": "ra_proc_macro_srv" } } },