7107: add working dir to cargo metadata fail messages r=matklad a=lf-

Context: I was having an error in my workspace config that I couldn't figure out without this added output. In particular, I have a Code workspace with a docs folder and one of my top level crates [which each have their own custom target], which was picking up the Cargo workspace level Cargo.toml incorrectly [which in my project should not ever be used for the editor], and ultimately had to override it with `linkedProjects`.

Here's a sample of the changed output:

```
[INFO rust_analyzer::main_loop] handle_event(Workspaces([Err(Failed to read Cargo metadata from Cargo.toml file /home/jade/dev/mu/Cargo.toml, cargo 1.50.0-nightly (75d5d8cff 2020-12-22)

Caused by:
    0: Failed to run `cargo metadata --manifest-path /home/jade/dev/mu/Cargo.toml` in `/home/jade/dev/mu`
    1: Error during execution of `cargo metadata`: error: target path "../../riscv64imac-mu-shoo-elf.json" is not a valid file
       
       Caused by:
         No such file or directory (os error 2)
       ), Err(Failed to read Cargo metadata from Cargo.toml file /home/jade/dev/mu/shoo/Cargo.toml, cargo 1.50.0-nightly (75d5d8cff 2020-12-22)

Caused by:
    0: Failed to run `cargo metadata --manifest-path /home/jade/dev/mu/shoo/Cargo.toml` in `/home/jade/dev/mu/shoo`
    1: Error during execution of `cargo metadata`: error: target path "../../riscv64imac-mu-shoo-elf.json" is not a valid file
       
       Caused by:
         No such file or directory (os error 2)
       )]))
```

Co-authored-by: lf- <lf-@users.noreply.github.com>
This commit is contained in:
bors[bot] 2020-12-31 13:04:29 +00:00 committed by GitHub
commit fda022592b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
//! FIXME: write short doc here
use std::{
convert::TryInto,
ffi::OsStr,
ops,
path::{Path, PathBuf},
@ -196,8 +197,23 @@ impl CargoWorkspace {
if let Some(target) = target {
meta.other_options(vec![String::from("--filter-platform"), target]);
}
let mut meta = meta.exec().with_context(|| {
format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display())
let cwd: Option<AbsPathBuf> =
std::env::current_dir().ok().and_then(|p| p.try_into().ok());
let workdir = cargo_toml
.parent()
.map(|p| p.to_path_buf())
.or(cwd)
.map(|dir| dir.to_string_lossy().to_string())
.unwrap_or_else(|| "<failed to get path>".into());
format!(
"Failed to run `cargo metadata --manifest-path {}` in `{}`",
cargo_toml.display(),
workdir
)
})?;
let mut out_dir_by_id = FxHashMap::default();