about summary refs log tree commit diff
path: root/tvix/eval
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-10-03T10·21+0300
committertazjin <tazjin@tvl.su>2022-10-04T21·04+0000
commitd6643f66b1cb8db5fc802ad53ef0d2633a51e815 (patch)
tree5e2fdcd5c5a9bed3969f6d4081e71bda389a175e /tvix/eval
parent546f445f0788e35b8299f53e2c79d29180c73142 (diff)
feat(tvix/eval): use fancy error formatting in REPL r/5032
Change-Id: I8d5652d797b012bc3d5248ca2b2d0c87572dbf2e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6851
Tested-by: BuildkiteCI
Reviewed-by: wpcarro <wpcarro@gmail.com>
Diffstat (limited to '')
-rw-r--r--tvix/eval/src/errors.rs8
-rw-r--r--tvix/eval/src/eval.rs15
-rw-r--r--tvix/eval/src/main.rs2
3 files changed, 16 insertions, 9 deletions
diff --git a/tvix/eval/src/errors.rs b/tvix/eval/src/errors.rs
index b3b45c6cee..8ecad80535 100644
--- a/tvix/eval/src/errors.rs
+++ b/tvix/eval/src/errors.rs
@@ -3,7 +3,7 @@ use std::path::PathBuf;
 use std::{fmt::Display, num::ParseIntError};
 
 use codemap::{CodeMap, Span};
-use codemap_diagnostic::{Diagnostic, Emitter, Level, SpanLabel, SpanStyle};
+use codemap_diagnostic::{ColorConfig, Diagnostic, Emitter, Level, SpanLabel, SpanStyle};
 use smol_str::SmolStr;
 
 use crate::Value;
@@ -141,6 +141,12 @@ impl Error {
         String::from_utf8_lossy(&out).to_string()
     }
 
+    /// Render a fancy, human-readable output of this error and print
+    /// it to stderr.
+    pub fn fancy_format_stderr(&self, codemap: &CodeMap) {
+        Emitter::stderr(ColorConfig::Auto, Some(codemap)).emit(&[self.diagnostic(codemap)]);
+    }
+
     /// Create the optional span label displayed as an annotation on
     /// the underlined span of the error.
     fn span_label(&self) -> Option<String> {
diff --git a/tvix/eval/src/eval.rs b/tvix/eval/src/eval.rs
index 21591802a4..e6199e00b0 100644
--- a/tvix/eval/src/eval.rs
+++ b/tvix/eval/src/eval.rs
@@ -81,21 +81,22 @@ pub fn interpret(code: &str, location: Option<PathBuf>, options: Options) -> Eva
     }
 
     for error in &result.errors {
-        eprintln!(
-            "compiler error: {:?} at `{}`[line {}]",
-            error.kind,
-            file.source_slice(error.span),
-            file.find_line(error.span.low()) + 1
-        );
+        error.fancy_format_stderr(&codemap);
     }
 
     if let Some(err) = result.errors.last() {
         return Err(err.clone());
     }
 
-    if options.trace_runtime {
+    let result = if options.trace_runtime {
         crate::vm::run_lambda(&mut TracingObserver::new(std::io::stderr()), result.lambda)
     } else {
         crate::vm::run_lambda(&mut NoOpObserver::default(), result.lambda)
+    };
+
+    if let Err(err) = &result {
+        err.fancy_format_stderr(&codemap);
     }
+
+    result
 }
diff --git a/tvix/eval/src/main.rs b/tvix/eval/src/main.rs
index 351554c2d5..efd4d14c3b 100644
--- a/tvix/eval/src/main.rs
+++ b/tvix/eval/src/main.rs
@@ -72,7 +72,7 @@ fn run_prompt(eval_options: tvix_eval::Options) {
                     Ok(result) => {
                         println!("=> {} :: {}", result, result.type_of());
                     }
-                    Err(err) => println!("{}", err),
+                    Err(_) => { /* interpret takes care of error formatting */ }
                 }
             }
             Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break,