From 7815fde43e735f0cd52abf979addf10dfa4b9098 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Mon, 8 Jul 2024 11:49:07 -0700 Subject: [PATCH] fix: Fix console dropping. --- Cargo.lock | 1 + crates/console/Cargo.toml | 1 + crates/console/src/buffer.rs | 21 ++++++++++++++------- crates/console/src/console.rs | 11 +++++------ packages/cli/CHANGELOG.md | 1 + 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5e74a9a00e5..97e95fd8420 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3454,6 +3454,7 @@ dependencies = [ "moon_target", "parking_lot", "starbase_styles", + "tracing", ] [[package]] diff --git a/crates/console/Cargo.toml b/crates/console/Cargo.toml index 0d0893de2c5..2e5741576fe 100644 --- a/crates/console/Cargo.toml +++ b/crates/console/Cargo.toml @@ -17,6 +17,7 @@ inquire = "0.7.5" miette = { workspace = true } parking_lot = "0.12.3" starbase_styles = { workspace = true } +tracing = { workspace = true } [lints] workspace = true diff --git a/crates/console/src/buffer.rs b/crates/console/src/buffer.rs index 3a0a3da8080..eaab8a3552d 100644 --- a/crates/console/src/buffer.rs +++ b/crates/console/src/buffer.rs @@ -7,6 +7,7 @@ use std::sync::mpsc::{self, Receiver, Sender, TryRecvError}; use std::sync::Arc; use std::thread::{sleep, spawn, JoinHandle}; use std::time::Duration; +use tracing::{debug, warn}; #[derive(Clone, Copy)] pub enum ConsoleStream { @@ -82,6 +83,14 @@ impl ConsoleBuffer { } pub fn close(&self) -> miette::Result<()> { + debug!( + "Closing {} stream", + match self.stream { + ConsoleStream::Stderr => "stderr", + ConsoleStream::Stdout => "stdout", + } + ); + self.flush()?; // Send the closed message @@ -150,12 +159,6 @@ impl ConsoleBuffer { } } -impl Drop for ConsoleBuffer { - fn drop(&mut self) { - self.close().unwrap(); - } -} - impl Clone for ConsoleBuffer { fn clone(&self) -> Self { Self { @@ -191,7 +194,11 @@ fn flush_on_loop(buffer: Arc>>, stream: ConsoleStream, receiver: R // Has the thread been closed? match receiver.try_recv() { - Ok(true) | Err(TryRecvError::Disconnected) => { + Ok(true) => { + break; + } + Err(TryRecvError::Disconnected) => { + warn!("Console auto-flush has been disconnected, output will be heavily buffered"); break; } _ => {} diff --git a/crates/console/src/console.rs b/crates/console/src/console.rs index 64072a6b49c..5b6c954332f 100644 --- a/crates/console/src/console.rs +++ b/crates/console/src/console.rs @@ -6,6 +6,7 @@ use moon_common::is_formatted_output; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::thread::JoinHandle; +use tracing::debug; pub type ConsoleTheme = RenderConfig<'static>; @@ -24,6 +25,8 @@ pub struct Console { impl Console { pub fn new(quiet: bool) -> Self { + debug!("Creating buffered console"); + let quiet = Arc::new(AtomicBool::new(quiet || is_formatted_output())); let mut err = ConsoleBuffer::new(ConsoleStream::Stderr); @@ -56,6 +59,8 @@ impl Console { } pub fn close(&mut self) -> miette::Result<()> { + debug!("Closing console and flushing buffered output"); + self.err.close()?; self.out.close()?; @@ -107,9 +112,3 @@ impl Clone for Console { } } } - -impl Drop for Console { - fn drop(&mut self) { - self.close().unwrap(); - } -} diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index e89e9886974..1f9aebcc15f 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -11,6 +11,7 @@ - Fixed `moon run --affected` not auto-installing dependencies in CI. - Fixed an issue where the project graph cache would not invalidate based on certain environment variables. +- Fixed an issue where our console would be prematurely dropped, causing output flushing issues. ## 1.26.6