-
Notifications
You must be signed in to change notification settings - Fork 11
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
Implement --errors
for ghcid.txt
#5
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,10 @@ pub struct Opts { | |
#[arg(long)] | ||
pub test: Option<String>, | ||
|
||
/// A file to write compilation errors to. This is analogous to `ghcid.txt`. | ||
#[arg(long)] | ||
pub errors: Option<Utf8PathBuf>, | ||
|
||
/// Options to modify file watching. | ||
#[command(flatten)] | ||
pub watch: WatchOpts, | ||
|
@@ -80,8 +84,7 @@ pub struct LoggingOpts { | |
#[arg(long, default_value = "ghcid_ng=info")] | ||
pub tracing_filter: String, | ||
|
||
/// How to display backtraces in error messages. '0' for no backtraces, '1' for standard | ||
/// backtraces, and 'full' to display source snippets. | ||
Comment on lines
-83
to
-84
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. Now that this argument is a
|
||
/// How to display backtraces in error messages. | ||
#[arg(long, env = "RUST_BACKTRACE", default_value = "0")] | ||
pub backtrace: RustBacktrace, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::sync::Weak; | ||
|
||
use camino::Utf8PathBuf; | ||
use miette::IntoDiagnostic; | ||
use tokio::fs::File; | ||
use tokio::io::AsyncWriteExt; | ||
use tokio::io::BufReader; | ||
use tokio::io::BufWriter; | ||
use tokio::io::Lines; | ||
use tokio::process::ChildStderr; | ||
use tokio::sync::mpsc; | ||
use tokio::sync::oneshot; | ||
use tokio::sync::Mutex; | ||
use tracing::instrument; | ||
|
||
use super::Ghci; | ||
|
||
/// An event sent to a `ghci` session's stderr channel. | ||
#[derive(Debug)] | ||
pub enum StderrEvent { | ||
/// Write to the `error_path` (`ghcid.txt`) file, if any. | ||
Write(oneshot::Sender<()>), | ||
} | ||
|
||
pub struct GhciStderr { | ||
pub ghci: Weak<Mutex<Ghci>>, | ||
pub reader: Lines<BufReader<ChildStderr>>, | ||
pub receiver: mpsc::Receiver<StderrEvent>, | ||
pub buffer: String, | ||
pub error_path: Option<Utf8PathBuf>, | ||
} | ||
|
||
impl GhciStderr { | ||
#[instrument(skip_all, name = "stderr", level = "debug")] | ||
pub async fn run(mut self) -> miette::Result<()> { | ||
loop { | ||
// TODO: Could this cause problems where we get an event and a final stderr line is only | ||
// processed after we write the error log? | ||
tokio::select! { | ||
Ok(Some(line)) = self.reader.next_line() => { | ||
self.ingest_line(line).await; | ||
} | ||
Some(event) = self.receiver.recv() => { | ||
match event { | ||
StderrEvent::Write(sender) => { | ||
let res = self.write().await; | ||
let _ = sender.send(()); | ||
res?; | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[instrument(skip(self), level = "debug")] | ||
async fn ingest_line(&mut self, line: String) { | ||
self.buffer.push_str(&line); | ||
self.buffer.push('\n'); | ||
eprintln!("{line}"); | ||
} | ||
|
||
#[instrument(skip_all, level = "debug")] | ||
async fn write(&mut self) -> miette::Result<()> { | ||
if self.buffer.is_empty() { | ||
// Nothing to do, don't wipe the error log file. | ||
tracing::debug!("Buffer empty, not writing"); | ||
return Ok(()); | ||
} | ||
|
||
if let Some(path) = &self.error_path { | ||
tracing::debug!(?path, bytes = self.buffer.len(), "Writing error log"); | ||
let file = File::create(path).await.into_diagnostic()?; | ||
let mut writer = BufWriter::new(file); | ||
writer | ||
.write_all(self.buffer.as_bytes()) | ||
.await | ||
.into_diagnostic()?; | ||
// This is load-bearing! If we don't properly flush/shutdown the handle, nothing gets | ||
// written! | ||
writer.shutdown().await.into_diagnostic()?; | ||
} | ||
|
||
self.buffer.clear(); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Unused imports from before CI.