-
Notifications
You must be signed in to change notification settings - Fork 13
Parse exceptions during reloads #366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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)?; | ||
|
|
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||||||
|
|
||||||||||
|
|
@@ -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), | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
If you think
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Not true. All of the
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's all you'd need to change: Diffdiff --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 | ||||||||||
|
|
@@ -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 | ||||||||||
|
|
@@ -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. | ||||||||||
| "# | ||||||||||
| )) | ||||||||||
|
|
@@ -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, | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| {-# OPTIONS_GHC -F -pgmF false #-} | ||
|
|
||
| module MyModule (example) where | ||
|
|
||
| example :: String | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.