From 632b0f290232913ef9950ee46917d5c195193102 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 26 Jan 2019 20:16:15 +0000 Subject: Use the correct working directory for cargo metadata and rustfmt --- crates/ra_lsp_server/src/main_loop/handlers.rs | 17 +++++++++++++---- .../ra_lsp_server/src/project_model/cargo_workspace.rs | 10 +++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 8ea9edc84..3f58e3c93 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -520,10 +520,17 @@ pub fn handle_formatting( let end_position = TextUnit::of_str(&file).conv_with(&file_line_index); use std::process; - let mut rustfmt = process::Command::new("rustfmt") + let mut rustfmt = process::Command::new("rustfmt"); + rustfmt .stdin(process::Stdio::piped()) - .stdout(process::Stdio::piped()) - .spawn()?; + .stdout(process::Stdio::piped()); + + if let Ok(path) = params.text_document.uri.to_file_path() { + if let Some(parent) = path.parent() { + rustfmt.current_dir(parent); + } + } + let mut rustfmt = rustfmt.spawn()?; rustfmt.stdin.as_mut().unwrap().write_all(file.as_bytes())?; @@ -531,7 +538,9 @@ pub fn handle_formatting( let captured_stdout = String::from_utf8(output.stdout)?; if !output.status.success() { failure::bail!( - "rustfmt exited with error code {}: {}.", + r#"rustfmt exited with: + Status: {} + stdout: {}"#, output.status, captured_stdout, ); diff --git a/crates/ra_lsp_server/src/project_model/cargo_workspace.rs b/crates/ra_lsp_server/src/project_model/cargo_workspace.rs index 75ae78bca..8cf99d586 100644 --- a/crates/ra_lsp_server/src/project_model/cargo_workspace.rs +++ b/crates/ra_lsp_server/src/project_model/cargo_workspace.rs @@ -117,9 +117,13 @@ impl Target { impl CargoWorkspace { pub fn from_cargo_metadata(cargo_toml: &Path) -> Result { - let meta = MetadataCommand::new() - .manifest_path(cargo_toml) - .features(CargoOpt::AllFeatures) + let mut meta = MetadataCommand::new(); + meta.manifest_path(cargo_toml) + .features(CargoOpt::AllFeatures); + if let Some(parent) = cargo_toml.parent() { + meta.current_dir(parent); + } + let meta = meta .exec() .map_err(|e| format_err!("cargo metadata failed: {}", e))?; let mut pkg_by_id = FxHashMap::default(); -- cgit v1.2.3 From d9456c3989bf1988e266b22e76a995b20eae7ae4 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 26 Jan 2019 20:43:51 +0000 Subject: Return an lsp error --- crates/ra_lsp_server/src/main_loop/handlers.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 3f58e3c93..4fc15c796 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -537,13 +537,16 @@ pub fn handle_formatting( let output = rustfmt.wait_with_output()?; let captured_stdout = String::from_utf8(output.stdout)?; if !output.status.success() { - failure::bail!( - r#"rustfmt exited with: + return Err(LspError::new( + gen_lsp_server::ErrorCode::ParseError as i32, + format!( + r#"rustfmt exited with: Status: {} stdout: {}"#, - output.status, - captured_stdout, - ); + output.status, captured_stdout, + ), + ) + .into()); } Ok(Some(vec![TextEdit { -- cgit v1.2.3 From 9fbbb8f6096836642704e34e42c9b9ea82807e7c Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 26 Jan 2019 20:58:50 +0000 Subject: Don't use the reserved error code --- crates/ra_lsp_server/src/main_loop/handlers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 4fc15c796..ace3da020 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -538,7 +538,7 @@ pub fn handle_formatting( let captured_stdout = String::from_utf8(output.stdout)?; if !output.status.success() { return Err(LspError::new( - gen_lsp_server::ErrorCode::ParseError as i32, + -32900, format!( r#"rustfmt exited with: Status: {} -- cgit v1.2.3