Skip to content
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

Evaluating commands fails after #168 #171

Merged
merged 6 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions src/ghci/compilation_log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::ghci::parse::CompilationResult;
use crate::ghci::parse::CompilationSummary;
use crate::ghci::parse::GhcDiagnostic;
use crate::ghci::parse::GhcMessage;
use crate::ghci::parse::Severity;

/// A log of messages from compilation, used to write the error log.
#[derive(Debug, Clone, Default)]
pub struct CompilationLog {
pub summary: Option<CompilationSummary>,
pub diagnostics: Vec<GhcDiagnostic>,
}

impl CompilationLog {
/// Get the result of compilation.
pub fn result(&self) -> Option<CompilationResult> {
self.summary.map(|summary| summary.result)
}
}

impl Extend<GhcMessage> for CompilationLog {
fn extend<T: IntoIterator<Item = GhcMessage>>(&mut self, iter: T) {
for message in iter {
match message {
GhcMessage::Compiling(module) => {
tracing::debug!(module = %module.name, path = %module.path, "Compiling");
}
GhcMessage::Diagnostic(diagnostic) => {
if let GhcDiagnostic {
severity: Severity::Error,
path: Some(path),
message,
..
} = &diagnostic
{
// We can't use 'message' for the field name because that's what tracing uses
// for the message.
tracing::debug!(%path, error = message, "Module failed to compile");
}
self.diagnostics.push(diagnostic);
}
GhcMessage::Summary(summary) => {
self.summary = Some(summary);
match summary.result {
CompilationResult::Ok => {
tracing::debug!("Compilation succeeded");
}
CompilationResult::Err => {
tracing::debug!("Compilation failed");
}
}
}
_ => {}
}
}
}
}
25 changes: 9 additions & 16 deletions src/ghci/error_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use tokio::io::BufWriter;
use tracing::instrument;

use super::parse::CompilationResult;
use super::parse::CompilationSummary;
use super::parse::GhcMessage;
use super::CompilationLog;

/// Error log writer.
///
Expand All @@ -24,12 +23,8 @@ impl ErrorLog {
}

/// Write the error log, if any, with the given compilation summary and diagnostic messages.
#[instrument(skip(self, messages), name = "error_log_write", level = "debug")]
pub async fn write(
&mut self,
compilation_summary: Option<CompilationSummary>,
messages: &[GhcMessage],
) -> miette::Result<()> {
#[instrument(skip(self, log), name = "error_log_write", level = "debug")]
pub async fn write(&mut self, log: &CompilationLog) -> miette::Result<()> {
let path = match &self.path {
Some(path) => path,
None => {
Expand All @@ -41,7 +36,7 @@ impl ErrorLog {
let file = File::create(path).await.into_diagnostic()?;
let mut writer = BufWriter::new(file);

if let Some(summary) = compilation_summary {
if let Some(summary) = log.summary {
// `ghcid` only writes the headline if there's no errors.
if let CompilationResult::Ok = summary.result {
tracing::debug!(%path, "Writing 'All good'");
Expand All @@ -57,13 +52,11 @@ impl ErrorLog {
}
}

for message in messages {
if let GhcMessage::Diagnostic(diagnostic) = message {
writer
.write_all(diagnostic.to_string().as_bytes())
.await
.into_diagnostic()?;
}
for diagnostic in &log.diagnostics {
writer
.write_all(diagnostic.to_string().as_bytes())
.await
.into_diagnostic()?;
}

// This is load-bearing! If we don't properly flush/shutdown the handle, nothing gets
Expand Down
4 changes: 3 additions & 1 deletion src/ghci/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tokio::sync::Mutex;
use tracing::instrument;

use crate::event_filter::FileEvent;
use crate::ghci::CompilationLog;
use crate::shutdown::ShutdownHandle;

use super::Ghci;
Expand Down Expand Up @@ -60,11 +61,12 @@ pub async fn run_ghci(
.wrap_err("Failed to start `ghci`")?;

// Wait for ghci to finish loading.
let mut log = CompilationLog::default();
tokio::select! {
_ = handle.on_shutdown_requested() => {
ghci.stop().await.wrap_err("Failed to quit ghci")?;
}
startup_result = ghci.initialize() => {
startup_result = ghci.initialize(&mut log) => {
startup_result?;
}
}
Expand Down
Loading