From 5075c77957e127be51e1b5271567f906abeb50c6 Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Wed, 13 Nov 2019 20:51:57 +0100 Subject: Use anyhow::Result in xtask, add contexts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This builds on #2231 but was actually done before that. You see, the cause for #2231 was that I got this error message: Error: Error { kind: Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }) } Just switching to `anyhow::Result` got me stack traces (when setting `RUST_LIB_BACKTRACE=1`) that at least showed stack backtrace: 0: std::backtrace::Backtrace::create 1: std::backtrace::Backtrace::capture 2: anyhow::error:: for anyhow::Error>::from 3: xtask::install_server 4: xtask::install 5: xtask::main 6: std::rt::lang_start::{{closure}} 7: std::panicking::try::do_call 8: __rust_maybe_catch_panic 9: std::rt::lang_start_internal 10: std::rt::lang_start 11: main With the added contexts (not at all exhaustive), the error became Error: install server Caused by: 0: build AutoCfg with target directory 1: No such file or directory (os error 2) Since anyhow is such a small thing (no new transitive dependencies!), and in general gives you `Result>` on steroids, I think this a nice small change. The only slightly annoying thing was to replace all the `Err(format!(…))?` calls (haven't even looked at whether we can make it support wrapping strings though), but the `bail!` macro is shorter anyway :) --- xtask/src/lib.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'xtask/src/lib.rs') diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index bae4c4650..bfee2f9c8 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -2,10 +2,10 @@ pub mod codegen; +use anyhow::Context; +pub use anyhow::Result; use std::{ - env, - error::Error, - fs, + env, fs, io::{Error as IoError, ErrorKind}, path::{Path, PathBuf}, process::{Command, Output, Stdio}, @@ -13,8 +13,6 @@ use std::{ use crate::codegen::Mode; -pub type Result = std::result::Result>; - const TOOLCHAIN: &str = "stable"; pub fn project_root() -> PathBuf { @@ -69,7 +67,7 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> { .status() { Ok(status) if status.success() => (), - _ => install_rustfmt()?, + _ => install_rustfmt().context("install rustfmt")?, }; if mode == Mode::Verify { @@ -112,7 +110,7 @@ pub fn run_clippy() -> Result<()> { .status() { Ok(status) if status.success() => (), - _ => install_clippy()?, + _ => install_clippy().context("install clippy")?, }; let allowed_lints = [ @@ -162,9 +160,9 @@ where let exec = args.next().unwrap(); let mut cmd = Command::new(exec); f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit())); - let output = cmd.output()?; + let output = cmd.output().with_context(|| format!("running `{}`", cmdline))?; if !output.status.success() { - Err(format!("`{}` exited with {}", cmdline, output.status))?; + anyhow::bail!("`{}` exited with {}", cmdline, output.status); } Ok(output) } -- cgit v1.2.3