Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proc-macro related config and tests #3958

Merged
merged 6 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ jobs:
- name: Compile
run: cargo test --no-run

# We have to build rust-analyzer first for running related heavy tests
- name: Build rust-analyzer
run: cargo build -p rust-analyzer
edwin0cheng marked this conversation as resolved.
Show resolved Hide resolved

- name: Test
run: cargo test

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions crates/ra_proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ pub struct ProcMacroClient {
}

impl ProcMacroClient {
pub fn extern_process(process_path: &Path) -> Result<ProcMacroClient, std::io::Error> {
let (thread, process) = ProcMacroProcessSrv::run(process_path)?;
pub fn extern_process<T: AsRef<str>>(
process_path: &Path,
args: &[T],
) -> Result<ProcMacroClient, std::io::Error> {
let (thread, process) = ProcMacroProcessSrv::run(process_path, args)?;
Ok(ProcMacroClient {
kind: ProcMacroClientKind::Process { process: Arc::new(process), thread },
})
Expand Down
8 changes: 5 additions & 3 deletions crates/ra_proc_macro/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ impl Drop for Process {
}

impl Process {
fn run(process_path: &Path) -> Result<Process, io::Error> {
fn run<T: AsRef<str>>(process_path: &Path, args: &[T]) -> Result<Process, io::Error> {
edwin0cheng marked this conversation as resolved.
Show resolved Hide resolved
let child = Command::new(process_path.clone())
.args(args.iter().map(|it| it.as_ref()))
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
Expand Down Expand Up @@ -74,10 +75,11 @@ impl Process {
}

impl ProcMacroProcessSrv {
pub fn run(
pub fn run<T: AsRef<str>>(
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 || {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -24,7 +24,8 @@ fn write_response(res: Result<msg::Response, String>) -> 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) => {
Expand Down
6 changes: 4 additions & 2 deletions crates/ra_proc_macro_srv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExpansionResult, String> {
pub(crate) fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
let expander = dylib::Expander::new(&task.lib)
.expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib));

Expand All @@ -39,7 +39,7 @@ pub fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
}
}

pub fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
pub(crate) fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
let expander = dylib::Expander::new(&task.lib)
.expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib));

Expand All @@ -53,5 +53,7 @@ pub fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
}
}

pub mod cli;

#[cfg(test)]
mod tests;
2 changes: 1 addition & 1 deletion crates/rust-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 21 additions & 3 deletions crates/rust-analyzer/src/bin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,23 @@ pub(crate) enum Command {
with_deps: bool,
path: PathBuf,
load_output_dirs: bool,
with_proc_macro: bool,
},
Bench {
path: PathBuf,
what: BenchWhat,
load_output_dirs: bool,
with_proc_macro: bool,
},
Diagnostics {
path: PathBuf,
load_output_dirs: bool,
with_proc_macro: bool,
/// Include files which are not modules. In rust-analyzer
/// this would include the parser test files.
all: bool,
},
ProcMacro,
RunServer,
Version,
}
Expand Down Expand Up @@ -148,6 +152,7 @@ FLAGS:
-h, --help Prints help information
--memory-usage
--load-output-dirs Load OUT_DIR values by running `cargo check` before analysis
--with-proc-macro Use ra-proc-macro-srv for proc-macro expanding
-v, --verbose
-q, --quiet

Expand All @@ -165,6 +170,7 @@ ARGS:
let only: Option<String> = matches.opt_value_from_str(["-o", "--only"])?;
let with_deps: bool = matches.contains("--with-deps");
let load_output_dirs = matches.contains("--load-output-dirs");
let with_proc_macro = matches.contains("--with-proc-macro");
let path = {
let mut trailing = matches.free()?;
if trailing.len() != 1 {
Expand All @@ -173,7 +179,15 @@ ARGS:
trailing.pop().unwrap().into()
};

Command::Stats { randomize, memory_usage, only, with_deps, path, load_output_dirs }
Command::Stats {
randomize,
memory_usage,
only,
with_deps,
path,
load_output_dirs,
with_proc_macro,
}
}
"analysis-bench" => {
if matches.contains(["-h", "--help"]) {
Expand All @@ -187,6 +201,7 @@ USAGE:
FLAGS:
-h, --help Prints help information
--load-output-dirs Load OUT_DIR values by running `cargo check` before analysis
--with-proc-macro Use ra-proc-macro-srv for proc-macro expanding
-v, --verbose

OPTIONS:
Expand Down Expand Up @@ -214,7 +229,8 @@ ARGS:
),
};
let load_output_dirs = matches.contains("--load-output-dirs");
Command::Bench { path, what, load_output_dirs }
let with_proc_macro = matches.contains("--with-proc-macro");
Command::Bench { path, what, load_output_dirs, with_proc_macro }
}
"diagnostics" => {
if matches.contains(["-h", "--help"]) {
Expand All @@ -237,6 +253,7 @@ ARGS:
}

let load_output_dirs = matches.contains("--load-output-dirs");
let with_proc_macro = matches.contains("--with-proc-macro");
let all = matches.contains("--all");
let path = {
let mut trailing = matches.free()?;
Expand All @@ -246,8 +263,9 @@ ARGS:
trailing.pop().unwrap().into()
};

Command::Diagnostics { path, load_output_dirs, all }
Command::Diagnostics { path, load_output_dirs, with_proc_macro, all }
}
"proc-macro" => Command::ProcMacro,
_ => {
eprintln!(
"\
Expand Down
22 changes: 18 additions & 4 deletions crates/rust-analyzer/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn main() -> Result<()> {
with_deps,
path,
load_output_dirs,
with_proc_macro,
} => cli::analysis_stats(
args.verbosity,
memory_usage,
Expand All @@ -33,16 +34,24 @@ fn main() -> Result<()> {
with_deps,
randomize,
load_output_dirs,
with_proc_macro,
)?,

args::Command::Bench { path, what, load_output_dirs } => {
cli::analysis_bench(args.verbosity, path.as_ref(), what, load_output_dirs)?
args::Command::Bench { path, what, load_output_dirs, with_proc_macro } => {
cli::analysis_bench(
args.verbosity,
path.as_ref(),
what,
load_output_dirs,
with_proc_macro,
)?
}

args::Command::Diagnostics { path, load_output_dirs, all } => {
cli::diagnostics(path.as_ref(), load_output_dirs, all)?
args::Command::Diagnostics { path, load_output_dirs, with_proc_macro, all } => {
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")),
}
Expand All @@ -56,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");

Expand Down
3 changes: 2 additions & 1 deletion crates/rust-analyzer/src/cli/analysis_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ pub fn analysis_bench(
path: &Path,
what: BenchWhat,
load_output_dirs: bool,
with_proc_macro: bool,
) -> Result<()> {
ra_prof::init();

let start = Instant::now();
eprint!("loading: ");
let (mut host, roots) = load_cargo(path, load_output_dirs)?;
let (mut host, roots) = load_cargo(path, load_output_dirs, with_proc_macro)?;
let db = host.raw_database();
eprintln!("{:?}\n", start.elapsed());

Expand Down
3 changes: 2 additions & 1 deletion crates/rust-analyzer/src/cli/analysis_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ pub fn analysis_stats(
with_deps: bool,
randomize: bool,
load_output_dirs: bool,
with_proc_macro: bool,
) -> Result<()> {
let db_load_time = Instant::now();
let (mut host, roots) = load_cargo(path, load_output_dirs)?;
let (mut host, roots) = load_cargo(path, load_output_dirs, with_proc_macro)?;
let db = host.raw_database();
println!("Database loaded, {} roots, {:?}", roots.len(), db_load_time.elapsed());
let analysis_time = Instant::now();
Expand Down
9 changes: 7 additions & 2 deletions crates/rust-analyzer/src/cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ use std::{collections::HashSet, path::Path};
use crate::cli::{load_cargo::load_cargo, Result};
use hir::Semantics;

pub fn diagnostics(path: &Path, load_output_dirs: bool, all: bool) -> Result<()> {
let (host, roots) = load_cargo(path, load_output_dirs)?;
pub fn diagnostics(
path: &Path,
load_output_dirs: bool,
with_proc_macro: bool,
all: bool,
) -> Result<()> {
let (host, roots) = load_cargo(path, load_output_dirs, with_proc_macro)?;
let db = host.raw_database();
let analysis = host.analysis();
let semantics = Semantics::new(db);
Expand Down
12 changes: 10 additions & 2 deletions crates/rust-analyzer/src/cli/load_cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId {
pub(crate) fn load_cargo(
root: &Path,
load_out_dirs_from_check: bool,
with_proc_macro: bool,
) -> Result<(AnalysisHost, FxHashMap<SourceRootId, PackageRoot>)> {
let root = std::env::current_dir()?.join(root);
let ws = ProjectWorkspace::discover(
Expand Down Expand Up @@ -69,7 +70,14 @@ pub(crate) fn load_cargo(
})
.collect::<FxHashMap<_, _>>();

let proc_macro_client = ProcMacroClient::dummy();
let proc_macro_client = if !with_proc_macro {
ProcMacroClient::dummy()
} else {
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))
}
Expand Down Expand Up @@ -175,7 +183,7 @@ mod tests {
#[test]
fn test_loading_rust_analyzer() {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
let (host, _roots) = load_cargo(path, false).unwrap();
let (host, _roots) = load_cargo(path, false, false).unwrap();
let n_crates = Crate::all(host.raw_database()).len();
// RA has quite a few crates, but the exact count doesn't matter
assert!(n_crates > 20);
Expand Down
14 changes: 13 additions & 1 deletion crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Config {
pub with_sysroot: bool,
pub publish_diagnostics: bool,
pub lru_capacity: Option<usize>,
pub proc_macro_srv: Option<String>,
pub proc_macro_srv: Option<(String, Vec<String>)>,
pub files: FilesConfig,
pub notifications: NotificationsConfig,

Expand Down Expand Up @@ -131,6 +131,18 @@ impl Config {
set(value, "/cargo/allFeatures", &mut self.cargo.all_features);
set(value, "/cargo/features", &mut self.cargo.features);
set(value, "/cargo/loadOutDirsFromCheck", &mut self.cargo.load_out_dirs_from_check);

match get::<bool>(value, "/procMacro/enabled") {
Some(true) => {
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,
}

match get::<Vec<String>>(value, "/rustfmt/overrideCommand") {
Some(mut args) if !args.is_empty() => {
let command = args.remove(0);
Expand Down
8 changes: 5 additions & 3 deletions crates/rust-analyzer/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub struct WorldState {
pub latest_requests: Arc<RwLock<LatestRequests>>,
pub flycheck: Option<Flycheck>,
pub diagnostics: DiagnosticCollection,
pub proc_macro_client: ProcMacroClient,
}

/// An immutable snapshot of the world's state at a point in time.
Expand Down Expand Up @@ -147,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!(
Expand Down Expand Up @@ -192,6 +193,7 @@ impl WorldState {
latest_requests: Default::default(),
flycheck,
diagnostics: Default::default(),
proc_macro_client,
}
}

Expand Down
Loading