-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add error context to failures in ra_project_model using anyhow crate
#3119
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 error context to failures in ra_project_model using anyhow crate
#3119
Conversation
ra_project_model using anyhow crate
| meta.current_dir(parent); | ||
| } | ||
| let meta = meta.exec().map_err(|e| format!("cargo metadata failed: {}", e))?; | ||
| let meta = meta.exec().map_err(|e| anyhow!("cargo metadata failed: {}", e))?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest to check that we get a good error message here. I am concerned because we got a report #3118 where the error message was not very helpful... I also believe meta.exec().context("cargo metadata failed") would give us a bit better error message, though not sure...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, I think this should use .context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran the function with a path which wasn't pointing to a Cargo.toml file and neither setup records he path that was used in the error message. Here's what I got with your change:
cargo metadata failed
Caused by:
Error during execution of `cargo metadata`: error: the manifest-path must be a path to a Cargo.toml file
Maybe it would make sense to add a new method on MetadataCommand which returns the full command called and include that in the error message?
I can't tell from that bug report exactly what info would have clarified things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think something like this should work:
meta.exec()
.with_context(|| format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display()))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
crates/ra_project_model/Cargo.toml
Outdated
| serde = { version = "1.0.89", features = ["derive"] } | ||
| serde_json = "1.0.39" | ||
|
|
||
| anyhow = "1.0.26" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing trailing newline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
crates/ra_project_model/src/lib.rs
Outdated
| } | ||
|
|
||
| match (|| -> Result<_> { | ||
| match (|| -> ::std::result::Result<String, Box<dyn Error + Send + Sync + 'static>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably wants to use anyhow::Error rather than Box<dyn Error> as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matklad , maybe anyhow::Result<String>, anyhow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
| .args(&["--print", "sysroot"]) | ||
| .output() | ||
| .map_err(|e| format!("rustc --print sysroot failed: {}", e))?; | ||
| .map_err(|e| anyhow!("rustc --print sysroot failed: {}", e))?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here .context would be more appropriate I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
| Err("failed to locate sysroot")?; | ||
| let exit_message = match rustc_output.status.code() { | ||
| Some(code) => { | ||
| anyhow!("failed to locate sysroot: rustc --print sysroot exited with code {}", code) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can use bail! macro, rather than an Err(...)? pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
Looks like CI is failing due to |
5dc8045 to
d908c9a
Compare
My mistake - just mixed up changes addressing separate comments from the review. |
| let cargo_toml = PathBuf::from("/"); | ||
| let result = CargoWorkspace::from_cargo_metadata(&cargo_toml, &CargoFeatures::default()); | ||
| println!("error: {:?}", result) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test doesn't assert anything and probably should be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops - forgot to take it out.
|
bors r+ |
…ate (rust-lang#3119) Add error context to failures in ra_project_model using anyhow crate
Addresses #3085