From 030d78345fa79af07f8ebd89a9d244576fac992b Mon Sep 17 00:00:00 2001 From: veetaha Date: Sat, 23 May 2020 04:58:22 +0300 Subject: Fix invoking cargo without consulting CARGO or standard installation paths --- crates/rust-analyzer/Cargo.toml | 1 + crates/rust-analyzer/src/main_loop/handlers.rs | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index 65b487db3..2e49448cc 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml @@ -48,6 +48,7 @@ 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" } +ra_toolchain = { path = "../ra_toolchain" } [target.'cfg(windows)'.dependencies] winapi = "0.3.8" diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 1f910ff82..1b5b3325c 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -40,6 +40,7 @@ use crate::{ world::WorldSnapshot, LspError, Result, }; +use anyhow::Context; pub fn handle_analyzer_status(world: WorldSnapshot, _: ()) -> Result { let _p = profile("handle_analyzer_status"); @@ -982,10 +983,15 @@ fn to_lsp_runnable( target.map_or_else(|| "run binary".to_string(), |t| format!("run {}", t)) } }; + let cargo_path = ra_toolchain::cargo() + .to_str() + .context("Path to cargo executable contains invalid UTF8 characters")? + .to_owned(); + Ok(lsp_ext::Runnable { range: to_proto::range(&line_index, runnable.range), label, - bin: "cargo".to_string(), + bin: cargo_path, args, extra_args, env: { -- cgit v1.2.3 From a419cedb1cc661349a022262c8b03993e063252f Mon Sep 17 00:00:00 2001 From: veetaha Date: Sat, 23 May 2020 16:31:56 +0300 Subject: Fix tests, apply code review proposals --- crates/ra_proc_macro_srv/Cargo.toml | 1 + crates/ra_proc_macro_srv/src/tests/utils.rs | 3 +-- crates/rust-analyzer/src/main_loop/handlers.rs | 17 ++++++++++------- crates/rust-analyzer/tests/heavy_tests/main.rs | 14 +++++++++----- 4 files changed, 21 insertions(+), 14 deletions(-) (limited to 'crates') diff --git a/crates/ra_proc_macro_srv/Cargo.toml b/crates/ra_proc_macro_srv/Cargo.toml index bb3003278..582102945 100644 --- a/crates/ra_proc_macro_srv/Cargo.toml +++ b/crates/ra_proc_macro_srv/Cargo.toml @@ -22,3 +22,4 @@ cargo_metadata = "0.10.0" difference = "2.0.0" # used as proc macro test target serde_derive = "1.0.106" +ra_toolchain = { path = "../ra_toolchain" } diff --git a/crates/ra_proc_macro_srv/src/tests/utils.rs b/crates/ra_proc_macro_srv/src/tests/utils.rs index 84348b5de..8d85f2d8a 100644 --- a/crates/ra_proc_macro_srv/src/tests/utils.rs +++ b/crates/ra_proc_macro_srv/src/tests/utils.rs @@ -2,7 +2,6 @@ use crate::dylib; use crate::ProcMacroSrv; -pub use difference::Changeset as __Changeset; use ra_proc_macro::ListMacrosTask; use std::str::FromStr; use test_utils::assert_eq_text; @@ -13,7 +12,7 @@ mod fixtures { // Use current project metadata to get the proc-macro dylib path pub fn dylib_path(crate_name: &str, version: &str) -> std::path::PathBuf { - let command = Command::new("cargo") + let command = Command::new(ra_toolchain::cargo()) .args(&["check", "--message-format", "json"]) .output() .unwrap() diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 1b5b3325c..d42cfa300 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -427,7 +427,7 @@ pub fn handle_runnables( res.push(lsp_ext::Runnable { range: Default::default(), label: format!("cargo {} -p {}", cmd, spec.package), - bin: "cargo".to_string(), + bin: cargo_path()?, args: vec![cmd.to_string(), "--package".to_string(), spec.package.clone()], extra_args: Vec::new(), env: FxHashMap::default(), @@ -439,7 +439,7 @@ pub fn handle_runnables( res.push(lsp_ext::Runnable { range: Default::default(), label: "cargo check --workspace".to_string(), - bin: "cargo".to_string(), + bin: cargo_path()?, args: vec!["check".to_string(), "--workspace".to_string()], extra_args: Vec::new(), env: FxHashMap::default(), @@ -450,6 +450,13 @@ pub fn handle_runnables( Ok(res) } +fn cargo_path() -> Result { + Ok(ra_toolchain::cargo() + .to_str() + .context("Path to `cargo` executable contains invalid UTF8 characters")? + .to_owned()) +} + pub fn handle_completion( world: WorldSnapshot, params: lsp_types::CompletionParams, @@ -983,15 +990,11 @@ fn to_lsp_runnable( target.map_or_else(|| "run binary".to_string(), |t| format!("run {}", t)) } }; - let cargo_path = ra_toolchain::cargo() - .to_str() - .context("Path to cargo executable contains invalid UTF8 characters")? - .to_owned(); Ok(lsp_ext::Runnable { range: to_proto::range(&line_index, runnable.range), label, - bin: cargo_path, + bin: cargo_path()?, args, extra_args, env: { diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs index 405ddb362..a31580c86 100644 --- a/crates/rust-analyzer/tests/heavy_tests/main.rs +++ b/crates/rust-analyzer/tests/heavy_tests/main.rs @@ -58,6 +58,10 @@ use std::collections::Spam; eprintln!("completion took {:?}", completion_start.elapsed()); } +fn cargo_path() -> String { + ra_toolchain::cargo().to_str().unwrap().to_owned() +} + #[test] fn test_runnables_no_project() { if skip_slow_tests() { @@ -79,7 +83,7 @@ fn foo() { { "args": [ "test" ], "extraArgs": [ "foo", "--nocapture" ], - "bin": "cargo", + "bin": cargo_path(), "env": { "RUST_BACKTRACE": "short" }, "cwd": null, "label": "test foo", @@ -91,7 +95,7 @@ fn foo() { { "args": ["check", "--workspace"], "extraArgs": [], - "bin": "cargo", + "bin": cargo_path(), "env": {}, "cwd": null, "label": "cargo check --workspace", @@ -141,7 +145,7 @@ fn main() {} { "args": [ "test", "--package", "foo", "--test", "spam" ], "extraArgs": [ "test_eggs", "--exact", "--nocapture" ], - "bin": "cargo", + "bin": cargo_path(), "env": { "RUST_BACKTRACE": "short" }, "label": "test test_eggs", "range": { @@ -153,7 +157,7 @@ fn main() {} { "args": [ "check", "--package", "foo" ], "extraArgs": [], - "bin": "cargo", + "bin": cargo_path(), "env": {}, "label": "cargo check -p foo", "range": { @@ -165,7 +169,7 @@ fn main() {} { "args": [ "test", "--package", "foo" ], "extraArgs": [], - "bin": "cargo", + "bin": cargo_path(), "env": {}, "label": "cargo test -p foo", "range": { -- cgit v1.2.3 From d605ec9c321392d9c7ee4b440c560e1e405d92e6 Mon Sep 17 00:00:00 2001 From: veetaha Date: Sun, 31 May 2020 05:13:08 +0300 Subject: Change Runnable.bin -> Runnable.kind As per matklad, we now pass the responsibility for finding the binary to the frontend. Also, added caching for finding the binary path to reduce the amount of filesystem interactions. --- crates/rust-analyzer/Cargo.toml | 1 - crates/rust-analyzer/src/lsp_ext.rs | 11 ++++++++++- crates/rust-analyzer/src/main_loop/handlers.rs | 14 +++----------- crates/rust-analyzer/tests/heavy_tests/main.rs | 14 +++++--------- 4 files changed, 18 insertions(+), 22 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index 2e49448cc..65b487db3 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml @@ -48,7 +48,6 @@ 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" } -ra_toolchain = { path = "../ra_toolchain" } [target.'cfg(windows)'.dependencies] winapi = "0.3.8" diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index acb1dacb6..173c23b9e 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -121,12 +121,21 @@ pub struct RunnablesParams { pub position: Option, } +// Must strictly correspond to the executable name +#[derive(Serialize, Deserialize, Debug)] +#[serde(rename_all = "lowercase")] +pub enum RunnableKind { + Cargo, + Rustc, + Rustup, +} + #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "camelCase")] pub struct Runnable { pub range: Range, pub label: String, - pub bin: String, + pub kind: RunnableKind, pub args: Vec, pub extra_args: Vec, pub env: FxHashMap, diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index d42cfa300..bc7c7f1ef 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -40,7 +40,6 @@ use crate::{ world::WorldSnapshot, LspError, Result, }; -use anyhow::Context; pub fn handle_analyzer_status(world: WorldSnapshot, _: ()) -> Result { let _p = profile("handle_analyzer_status"); @@ -427,7 +426,7 @@ pub fn handle_runnables( res.push(lsp_ext::Runnable { range: Default::default(), label: format!("cargo {} -p {}", cmd, spec.package), - bin: cargo_path()?, + kind: lsp_ext::RunnableKind::Cargo, args: vec![cmd.to_string(), "--package".to_string(), spec.package.clone()], extra_args: Vec::new(), env: FxHashMap::default(), @@ -439,7 +438,7 @@ pub fn handle_runnables( res.push(lsp_ext::Runnable { range: Default::default(), label: "cargo check --workspace".to_string(), - bin: cargo_path()?, + kind: lsp_ext::RunnableKind::Cargo, args: vec!["check".to_string(), "--workspace".to_string()], extra_args: Vec::new(), env: FxHashMap::default(), @@ -450,13 +449,6 @@ pub fn handle_runnables( Ok(res) } -fn cargo_path() -> Result { - Ok(ra_toolchain::cargo() - .to_str() - .context("Path to `cargo` executable contains invalid UTF8 characters")? - .to_owned()) -} - pub fn handle_completion( world: WorldSnapshot, params: lsp_types::CompletionParams, @@ -994,7 +986,7 @@ fn to_lsp_runnable( Ok(lsp_ext::Runnable { range: to_proto::range(&line_index, runnable.range), label, - bin: cargo_path()?, + kind: lsp_ext::RunnableKind::Cargo, args, extra_args, env: { diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs index a31580c86..8b473ff74 100644 --- a/crates/rust-analyzer/tests/heavy_tests/main.rs +++ b/crates/rust-analyzer/tests/heavy_tests/main.rs @@ -58,10 +58,6 @@ use std::collections::Spam; eprintln!("completion took {:?}", completion_start.elapsed()); } -fn cargo_path() -> String { - ra_toolchain::cargo().to_str().unwrap().to_owned() -} - #[test] fn test_runnables_no_project() { if skip_slow_tests() { @@ -83,7 +79,7 @@ fn foo() { { "args": [ "test" ], "extraArgs": [ "foo", "--nocapture" ], - "bin": cargo_path(), + "kind": "cargo", "env": { "RUST_BACKTRACE": "short" }, "cwd": null, "label": "test foo", @@ -95,7 +91,7 @@ fn foo() { { "args": ["check", "--workspace"], "extraArgs": [], - "bin": cargo_path(), + "kind": "cargo", "env": {}, "cwd": null, "label": "cargo check --workspace", @@ -145,7 +141,7 @@ fn main() {} { "args": [ "test", "--package", "foo", "--test", "spam" ], "extraArgs": [ "test_eggs", "--exact", "--nocapture" ], - "bin": cargo_path(), + "kind": "cargo", "env": { "RUST_BACKTRACE": "short" }, "label": "test test_eggs", "range": { @@ -157,7 +153,7 @@ fn main() {} { "args": [ "check", "--package", "foo" ], "extraArgs": [], - "bin": cargo_path(), + "kind": "cargo", "env": {}, "label": "cargo check -p foo", "range": { @@ -169,7 +165,7 @@ fn main() {} { "args": [ "test", "--package", "foo" ], "extraArgs": [], - "bin": cargo_path(), + "kind": "cargo", "env": {}, "label": "cargo test -p foo", "range": { -- cgit v1.2.3