Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/ghci/parse/ghc_message/exception.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use winnow::PResult;
use winnow::Parser;

use crate::ghci::parse::lines::until_newline;

use super::GhcMessage;

/// Parse an "Exception" message like this:
///
/// ```plain
/// *** Exception: /Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)
/// ```
pub fn exception(input: &mut &str) -> PResult<GhcMessage> {
let _ = "*** Exception: ".parse_next(input)?;

let message = until_newline.parse_next(input)?;
Copy link
Member

@evanrelf evanrelf Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: I realized you already know this in the comment below this.

I think the most correct parser of a GHCi exception would parse until a prompt, not a newline. But until a newline would handle this particular exception, if that's all you're going for right now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the issue is we get these in a stream of arbitrary GHC output, which may include other diagnostics we want to parse, compiling-such-and-such-a-module messages, etc.


Ok(GhcMessage::Exception(message.to_owned()))
}

#[cfg(test)]
mod tests {
use super::*;

use indoc::indoc;
use pretty_assertions::assert_eq;

#[test]
fn test_parse_exception() {
assert_eq!(
exception.parse("*** Exception: Uh oh!\n").unwrap(),
GhcMessage::Exception("Uh oh!".into())
);

assert_eq!(
exception.parse("*** Exception: /Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)\n").unwrap(),
GhcMessage::Exception("/Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)".into())
);

// Doesn't parse subsequent lines (even if they're relevant, unfortunately).
assert_eq!(
exception
.parse(indoc!(
"
*** Exception: puppy doggy
CallStack (from HasCallStack):
error, called at <interactive>:3:1 in interactive:Ghci1
"
))
.unwrap(),
GhcMessage::Exception("puppy doggy".into())
);
Comment on lines +40 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay you already know that. Glad to see it still works in the presence of multi-line exception messages?

}
}
15 changes: 15 additions & 0 deletions src/ghci/parse/ghc_message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ use module_import_cycle_diagnostic::module_import_cycle_diagnostic;
mod no_location_info_diagnostic;
use no_location_info_diagnostic::no_location_info_diagnostic;

mod exception;
use exception::exception;

use super::rest_of_line;
use super::CompilingModule;

Expand All @@ -64,6 +67,15 @@ pub enum GhcMessage {
/// Foo.hs:81:1: Warning: Defined but not used: `bar'
/// ```
Diagnostic(GhcDiagnostic),
/// An exception while running a command or similar.
///
/// These may be multiple lines, but there's no good way to determine where the message ends,
/// so we just parse the first line.
///
/// ```text
/// *** Exception: /Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)
/// ```
Exception(String),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't plan to mutate this string, you could use less memory:

Suggested change
Exception(String),
Exception(Box<str>),

If you think GhcMessages will be cloned a lot, you could account for that too:

Suggested change
Exception(String),
Exception(Arc<str>),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like kinda a yak shave imo and the ergonomics of dealing with both of those types are worse. it's pretty regular to just put String places that it doesn't strictly have to be because it's easy. if the memory usage starts mattering it can be changed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm pretty sure zero-copy parsing is kind of an ergonomic nightmare, even if it's technically possible

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ergonomics of dealing with both of those types are worse

Not true. All of the &str API is still available thanks to the Deref impl for Box<T> and Arc<T>.

zero-copy parsing is kind of an ergonomic nightmare

This is not zero-copy, and this would not require adding lifetimes.


I don't feel strongly about which type you choose, but I do wanna make sure you understand the tradeoffs correctly! I am not suggesting any ergonomic sacrifice here!

This video explains things better: https://www.youtube.com/watch?v=A4cKi7PTJSs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's all you'd need to change:

Diff
diff --git a/src/ghci/parse/ghc_message/exception.rs b/src/ghci/parse/ghc_message/exception.rs
index b09906d..55f4222 100644
--- a/src/ghci/parse/ghc_message/exception.rs
+++ b/src/ghci/parse/ghc_message/exception.rs
@@ -1,3 +1,4 @@
+use std::sync::Arc;
 use winnow::PResult;
 use winnow::Parser;
 
@@ -15,7 +16,7 @@ pub fn exception(input: &mut &str) -> PResult<GhcMessage> {
 
     let message = until_newline.parse_next(input)?;
 
-    Ok(GhcMessage::Exception(message.to_owned()))
+    Ok(GhcMessage::Exception(Arc::from(message)))
 }
 
 #[cfg(test)]
@@ -29,12 +30,12 @@ mod tests {
     fn test_parse_exception() {
         assert_eq!(
             exception.parse("*** Exception: Uh oh!\n").unwrap(),
-            GhcMessage::Exception("Uh oh!".into())
+            GhcMessage::Exception(Arc::new("Uh oh!"))
         );
 
         assert_eq!(
             exception.parse("*** Exception: /Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)\n").unwrap(),
-            GhcMessage::Exception("/Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)".into())
+            GhcMessage::Exception(Arc::new("/Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)"))
         );
 
         // Doesn't parse subsequent lines (even if they're relevant, unfortunately).
@@ -48,7 +49,7 @@ mod tests {
                     "
                 ))
                 .unwrap(),
-            GhcMessage::Exception("puppy doggy".into())
+            GhcMessage::Exception(Arc::new("puppy doggy"))
         );
     }
 }
diff --git a/src/ghci/parse/ghc_message/mod.rs b/src/ghci/parse/ghc_message/mod.rs
index 6036a1d..34e2692 100644
--- a/src/ghci/parse/ghc_message/mod.rs
+++ b/src/ghci/parse/ghc_message/mod.rs
@@ -1,6 +1,7 @@
 //! Parser for GHC compiler output.
 
 use std::fmt::Display;
+use std::sync::Arc;
 
 use camino::Utf8PathBuf;
 use miette::miette;
@@ -75,7 +76,7 @@ pub enum GhcMessage {
     /// ```text
     /// *** Exception: /Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)
     /// ```
-    Exception(String),
+    Exception(Arc<str>),
     /// A configuration file being loaded.
     ///
     /// ```text
@@ -336,7 +337,7 @@ mod tests {
                         ].join("\n"),
                     },
                 ),
-                GhcMessage::Exception("/Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)".to_owned()),
+                GhcMessage::Exception(Arc::new("/Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)")),
                 GhcMessage::Summary(
                     CompilationSummary {
                         result: CompilationResult::Err,

/// A configuration file being loaded.
///
/// ```text
Expand Down Expand Up @@ -181,6 +193,7 @@ fn parse_messages_inner(input: &mut &str) -> PResult<Vec<GhcMessage>> {
.map(Item::One),
module_import_cycle_diagnostic.map(Item::Many),
loaded_configuration.map(Item::One),
exception.map(Item::One),
rest_of_line.map(|line| {
tracing::debug!(line, "Ignoring GHC output line");
Item::Ignore
Expand Down Expand Up @@ -290,6 +303,7 @@ mod tests {
|
4 | example = "example"
| ^^^^^^^^^
*** Exception: /Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)
Failed, two modules loaded.
"#
))
Expand Down Expand Up @@ -322,6 +336,7 @@ mod tests {
].join("\n"),
},
),
GhcMessage::Exception("/Users/.../dist-newstyle/ghc82733_tmp_1/ghc_tmp_34657.h: withFile: does not exist (No such file or directory)".to_owned()),
GhcMessage::Summary(
CompilationSummary {
result: CompilationResult::Err,
Expand Down
27 changes: 27 additions & 0 deletions tests/all_good.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,30 @@ async fn can_detect_compilation_failure() {
.await
.unwrap();
}

/// Test that `ghciwatch` can detect an `*** Exception` diagnostic.
///
/// Regression test for DUX-3144.
#[test]
async fn can_detect_exception() {
let mut session = GhciWatchBuilder::new("tests/data/simple")
.with_args(["--after-reload-ghci", r#"error "oopsie daisy!""#])
.start()
.await
.expect("ghciwatch starts");
let module_path = session.path("src/MyModule.hs");

session.wait_until_ready().await.expect("ghciwatch loads");

session.fs().append(&module_path, "\n").await.unwrap();

session
.wait_for_log(BaseMatcher::compilation_failed())
.await
.unwrap();

session
.wait_for_log(BaseMatcher::reload_completes().but_not(BaseMatcher::message("All good!")))
.await
.unwrap();
}