Browse Source

add backtrace and print double printing of cargo_metadata errors

Jonathan Kelley 6 days ago
parent
commit
378ac772c8

+ 1 - 1
packages/cli/src/build/context.rs

@@ -105,7 +105,7 @@ impl BuildContext {
     }
 
     pub(crate) fn status_build_error(&self, line: String) {
-        tracing::error!(dx_src = ?TraceSrc::Cargo, "{line}");
+        tracing::debug!(dx_src = ?TraceSrc::Cargo, "{line}");
     }
 
     pub(crate) fn status_build_message(&self, line: String) {

+ 1 - 1
packages/cli/src/build/request.rs

@@ -925,7 +925,7 @@ impl BuildRequest {
                 //       since that is a really bad user experience.
                 Message::BuildFinished(finished) => {
                     if !finished.success {
-                        bail!("Cargo build failed, signaled by the compiler.")
+                        bail!("cargo build finished with errors.")
                     }
                 }
                 _ => {}

+ 5 - 4
packages/cli/src/error.rs

@@ -2,10 +2,7 @@ pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;
 pub use anyhow::Error;
 
 pub fn log_stacktrace(err: &anyhow::Error) -> String {
-    let mut trace = format!(
-        "{ERROR_STYLE}{err}{ERROR_STYLE:#}",
-        ERROR_STYLE = crate::styles::ERROR_STYLE,
-    );
+    let mut trace = format!("{err}",);
 
     for (idx, cause) in err.chain().enumerate().skip(1) {
         trace.push_str(&format!(
@@ -16,5 +13,9 @@ pub fn log_stacktrace(err: &anyhow::Error) -> String {
         ));
     }
 
+    if crate::VERBOSITY.get().unwrap().trace {
+        trace.push_str(&format!("\nBacktrace:\n{}", err.backtrace()));
+    }
+
     trace
 }

+ 5 - 1
packages/cli/src/main.rs

@@ -77,7 +77,11 @@ async fn main() {
                 },
             );
 
-            eprintln!("{}", crate::error::log_stacktrace(&err));
+            eprintln!(
+                "{ERROR_STYLE}Failed{ERROR_STYLE:#}: {}",
+                crate::error::log_stacktrace(&err),
+                ERROR_STYLE = crate::styles::ERROR_STYLE,
+            );
 
             std::process::exit(1);
         }

+ 6 - 1
packages/cli/src/serve/mod.rs

@@ -162,7 +162,12 @@ pub(crate) async fn serve_all(args: ServeArgs, tracer: &mut TraceController) ->
                         screen.push_cargo_log(message);
                     }
                     BuilderUpdate::BuildFailed { err } => {
-                        tracing::error!("Build failed: {}", crate::error::log_stacktrace(&err));
+                        tracing::error!(
+                            "{ERROR_STYLE}Build failed{ERROR_STYLE:#}: {}",
+                            crate::error::log_stacktrace(&err),
+                            ERROR_STYLE = crate::styles::ERROR_STYLE,
+                        );
+
                         if exit_on_error {
                             return Err(err);
                         }

+ 9 - 5
packages/cli/src/workspace.rs

@@ -48,9 +48,7 @@ impl Workspace {
 
             let mut builder = krates::Builder::new();
             builder.workspace(true);
-            let res = builder
-                .build(cmd, |_| {})
-                .context("Failed to run cargo metadata");
+            let res = builder.build(cmd, |_| {})?;
 
             if !lock_options.offline {
                 if let Ok(res) = std::env::var("SIMULATE_SLOW_NETWORK") {
@@ -58,7 +56,7 @@ impl Workspace {
                 }
             }
 
-            res
+            Ok(res) as Result<Krates, krates::Error>
         });
 
         let spin_future = async move {
@@ -76,7 +74,13 @@ impl Workspace {
         };
 
         let krates = tokio::select! {
-            f = krates_future => f.context("failed to run cargo metadata")??,
+            f = krates_future => {
+                let res = f?;
+                if let Err(krates::Error::Metadata(e)) = res {
+                    bail!("{e}");
+                }
+                res?
+            },
             _ = spin_future => bail!("cargo metadata took too long to respond, try again with --offline"),
         };