From ec331a088cf7a16f3312bc247c58488ac2a6c72b Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Fri, 10 Jul 2020 13:51:58 -0700 Subject: [PATCH] run-examples: Provide more error context for debugging Use `anyhow` for nice errors and provide error context on commands that we run. --- Cargo.lock | 1 + crates/misc/run-examples/Cargo.toml | 1 + crates/misc/run-examples/src/main.rs | 53 +++++++++++++++------------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 91cc09f5d6..855f8bfc4f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1742,6 +1742,7 @@ dependencies = [ name = "run-examples" version = "0.18.0" dependencies = [ + "anyhow", "cc", ] diff --git a/crates/misc/run-examples/Cargo.toml b/crates/misc/run-examples/Cargo.toml index 6dc0ead7e9..616e4ffc1d 100644 --- a/crates/misc/run-examples/Cargo.toml +++ b/crates/misc/run-examples/Cargo.toml @@ -6,4 +6,5 @@ edition = "2018" publish = false [dependencies] +anyhow = "1.0.31" cc = "1.0" diff --git a/crates/misc/run-examples/src/main.rs b/crates/misc/run-examples/src/main.rs index a0cc098ab7..358b738311 100644 --- a/crates/misc/run-examples/src/main.rs +++ b/crates/misc/run-examples/src/main.rs @@ -1,26 +1,25 @@ +use anyhow::Context; use std::collections::BTreeSet; use std::process::Command; -fn main() { +fn main() -> anyhow::Result<()> { let example_to_run = std::env::args().nth(1); - let examples = std::fs::read_dir("examples").unwrap(); - let examples = examples - .filter_map(|e| { - let e = e.unwrap(); - let path = e.path(); - let dir = e.metadata().unwrap().is_dir(); - if let Some("wat") = path.extension().and_then(|s| s.to_str()) { - return None; - } + let mut examples = BTreeSet::new(); + for e in std::fs::read_dir("examples")? { + let e = e?; + let path = e.path(); + let dir = e.metadata()?.is_dir(); + if let Some("wat") = path.extension().and_then(|s| s.to_str()) { + continue; + } - Some((path.file_stem().unwrap().to_str().unwrap().to_owned(), dir)) - }) - .collect::>(); + examples.insert((path.file_stem().unwrap().to_str().unwrap().to_owned(), dir)); + } println!("======== Building libwasmtime.a ==========="); run(Command::new("cargo") .args(&["build"]) - .current_dir("crates/c-api")); + .current_dir("crates/c-api"))?; for (example, is_dir) in examples { if example == "README" { @@ -43,13 +42,13 @@ fn main() { .arg("-p") .arg(format!("example-{}-wasm", example)) .arg("--target") - .arg(target)); + .arg(target))?; } println!("======== Rust example `{}` ============", example); run(Command::new("cargo") .arg("run") .arg("--example") - .arg(&example)); + .arg(&example))?; println!("======== C/C++ example `{}` ============", example); for extension in ["c", "cc"].iter() { @@ -93,18 +92,22 @@ fn main() { if cfg!(target_os = "linux") { cmd.arg("-lpthread").arg("-ldl").arg("-lm"); } - run(&mut cmd); + run(&mut cmd)?; - run(&mut Command::new(exe)); + run(&mut Command::new(exe))?; } } + + Ok(()) } -fn run(cmd: &mut Command) { - let s = cmd.status().unwrap(); - if !s.success() { - eprintln!("failed to run {:?}", cmd); - eprintln!("status: {}", s); - std::process::exit(1); - } +fn run(cmd: &mut Command) -> anyhow::Result<()> { + (|| -> anyhow::Result<()> { + let s = cmd.status()?; + if !s.success() { + anyhow::bail!("Exited with failure status: {}", s); + } + Ok(()) + })() + .with_context(|| format!("failed to run `{:?}`", cmd)) }