From da303f74fceefa66614786320762a915f41230b9 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 3 Sep 2025 15:09:13 -0500 Subject: [PATCH 1/5] refactor(shell): Remove redundant verbosity check --- src/cargo/core/shell.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 918b59fbc..4080bb7ac 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -222,10 +222,7 @@ impl Shell { /// Prints an amber 'warning' message. pub fn warn(&mut self, message: T) -> CargoResult<()> { - match self.verbosity { - Verbosity::Quiet => Ok(()), - _ => self.print(&"warning", Some(&message), &WARN, false), - } + self.print(&"warning", Some(&message), &WARN, false) } /// Prints a cyan 'note' message. From d9d6c4a6512a071a22b798a3a89853217a8233a0 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 3 Sep 2025 15:06:26 -0500 Subject: [PATCH 2/5] refactor(shell): Make print_report consistent with message_stder --- src/cargo/core/shell.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 4080bb7ac..6a609015c 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -404,16 +404,14 @@ impl Shell { } /// Prints the passed in [`Report`] to stderr - pub fn print_report(&mut self, report: Report<'_>) -> std::io::Result<()> { + pub fn print_report(&mut self, report: Report<'_>) -> CargoResult<()> { let term_width = self .err_width() .diagnostic_terminal_width() .unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH); - writeln!( - self.err(), - "{}", - Renderer::styled().term_width(term_width).render(report) - ) + let rendered = Renderer::styled().term_width(term_width).render(report); + self.err().write_all(rendered.as_bytes())?; + Ok(()) } } From 30e00d90f517f7109e5cf66acee5f5980fb336dd Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 3 Sep 2025 15:25:07 -0500 Subject: [PATCH 3/5] fix(shell): Add trailing newline to reports --- src/cargo/core/shell.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 6a609015c..4653f4507 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -411,6 +411,7 @@ impl Shell { .unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH); let rendered = Renderer::styled().term_width(term_width).render(report); self.err().write_all(rendered.as_bytes())?; + self.err().write_all("\n".as_bytes())?; Ok(()) } } From e3e2b07290c874d631140876c54d57c34d7ca3c7 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 3 Sep 2025 15:03:44 -0500 Subject: [PATCH 4/5] fix(shell): Clear lines for Reports --- src/cargo/core/shell.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 4653f4507..4ec9883c2 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -405,6 +405,9 @@ impl Shell { /// Prints the passed in [`Report`] to stderr pub fn print_report(&mut self, report: Report<'_>) -> CargoResult<()> { + if self.needs_clear { + self.err_erase_line(); + } let term_width = self .err_width() .diagnostic_terminal_width() From 642411f01a892a757b05619b2000f86309eafb99 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 3 Sep 2025 15:59:18 -0500 Subject: [PATCH 5/5] refactor(shell): Allow callers to opt-in to quiet filtering --- src/cargo/core/shell.rs | 6 +++++- src/cargo/util/lints.rs | 15 ++++++++++++--- src/cargo/util/toml/mod.rs | 4 ++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 4ec9883c2..48c3f68dc 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -404,7 +404,11 @@ impl Shell { } /// Prints the passed in [`Report`] to stderr - pub fn print_report(&mut self, report: Report<'_>) -> CargoResult<()> { + pub fn print_report(&mut self, report: Report<'_>, force: bool) -> CargoResult<()> { + if !force && matches!(self.verbosity, Verbosity::Quiet) { + return Ok(()); + } + if self.needs_clear { self.err_erase_line(); } diff --git a/src/cargo/util/lints.rs b/src/cargo/util/lints.rs index 06ea6f470..0763be70e 100644 --- a/src/cargo/util/lints.rs +++ b/src/cargo/util/lints.rs @@ -171,7 +171,7 @@ fn verify_feature_enabled( } *error_count += 1; - gctx.shell().print_report(&report)?; + gctx.shell().print_report(&report, true)?; } Ok(()) } @@ -339,6 +339,15 @@ impl LintLevel { LintLevel::Forbid => Level::ERROR, } } + + fn force(self) -> bool { + match self { + Self::Allow => false, + Self::Warn => true, + Self::Deny => true, + Self::Forbid => true, + } + } } impl From for LintLevel { @@ -459,7 +468,7 @@ pub fn check_im_a_teapot( ) .element(Level::NOTE.message(&emitted_reason))]; - gctx.shell().print_report(report)?; + gctx.shell().print_report(report, lint_level.force())?; } Ok(()) } @@ -568,7 +577,7 @@ fn output_unknown_lints( ); } - gctx.shell().print_report(&report)?; + gctx.shell().print_report(&report, lint_level.force())?; } Ok(()) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 54401f0d1..deba531c1 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1932,7 +1932,7 @@ fn missing_dep_diagnostic( group.element(snippet) }; - if let Err(err) = gctx.shell().print_report(&[group]) { + if let Err(err) = gctx.shell().print_report(&[group], true) { return Err(err.into()); } Err(AlreadyPrintedError::new(anyhow!("").into()).into()) @@ -2800,7 +2800,7 @@ fn emit_diagnostic( .annotation(AnnotationKind::Primary.span(span)), ); - if let Err(err) = gctx.shell().print_report(&[group]) { + if let Err(err) = gctx.shell().print_report(&[group], true) { return err.into(); } return AlreadyPrintedError::new(e.into()).into();