681: Use the correct working directory for cargo metadata and rustfmt r=matklad a=DJMcNab

Fixes maybe #670. @bjorn3, is that true?

(Awkward wording due to GitHub's eager 'fixes' finding)

Co-authored-by: DJMcNab <36049421+djmcnab@users.noreply.github.com>
This commit is contained in:
bors[bot] 2019-01-26 21:18:52 +00:00
commit 691ffd2dcb
2 changed files with 27 additions and 11 deletions

View file

@ -520,21 +520,33 @@ 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())?;
let output = rustfmt.wait_with_output()?;
let captured_stdout = String::from_utf8(output.stdout)?;
if !output.status.success() {
failure::bail!(
"rustfmt exited with error code {}: {}.",
output.status,
captured_stdout,
);
return Err(LspError::new(
-32900,
format!(
r#"rustfmt exited with:
Status: {}
stdout: {}"#,
output.status, captured_stdout,
),
)
.into());
}
Ok(Some(vec![TextEdit {

View file

@ -117,9 +117,13 @@ impl Target {
impl CargoWorkspace {
pub fn from_cargo_metadata(cargo_toml: &Path) -> Result<CargoWorkspace> {
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();