diff --git a/apps/oxlint/src/output_formatter/github.rs b/apps/oxlint/src/output_formatter/github.rs index 112c4a8ff2414..16fcbcbc287a3 100644 --- a/apps/oxlint/src/output_formatter/github.rs +++ b/apps/oxlint/src/output_formatter/github.rs @@ -26,6 +26,10 @@ impl DiagnosticReporter for GithubReporter { Some(get_diagnostic_result_output(result)) } + fn supports_minified_file_fallback(&self) -> bool { + false + } + fn render_error(&mut self, error: Error) -> Option { Some(format_github(&error)) } @@ -83,7 +87,7 @@ fn escape_property(value: &str) -> String { #[cfg(test)] mod test { use oxc_diagnostics::{ - NamedSource, OxcDiagnostic, + DiagnosticService, NamedSource, OxcDiagnostic, reporter::{DiagnosticReporter, DiagnosticResult}, }; use oxc_span::Span; @@ -123,4 +127,25 @@ mod test { "::warning file=file%3A//test.ts,line=1,endLine=1,col=1,endColumn=9,title=oxlint::error message\n" ); } + + #[test] + fn reporter_does_not_use_minified_fallback_for_long_annotations() { + let source_text = format!("{}\n", "a".repeat(1300)); + let diagnostic = OxcDiagnostic::warn("error message") + .with_label(Span::new(0, 1300)) + .with_source_code(NamedSource::new("file://test.ts", source_text)); + + let (mut service, sender) = DiagnosticService::new(Box::new(GithubReporter)); + sender.send(vec![diagnostic]).unwrap(); + drop(sender); + + let mut output = Vec::new(); + service.run(&mut output); + let output = String::from_utf8(output).unwrap(); + + assert!(output.starts_with("::warning file=file%3A//test.ts,line=1,endLine=1,col=1,")); + assert!(output.contains("title=oxlint::error message")); + assert!(!output.contains("File is too long to fit on the screen")); + assert!(!output.contains("file=,line=0,endLine=0,col=0,endColumn=0")); + } } diff --git a/apps/oxlint/src/output_formatter/unix.rs b/apps/oxlint/src/output_formatter/unix.rs index d81b8d1ec9f30..5b73e7908331f 100644 --- a/apps/oxlint/src/output_formatter/unix.rs +++ b/apps/oxlint/src/output_formatter/unix.rs @@ -33,6 +33,10 @@ impl DiagnosticReporter for UnixReporter { None } + fn supports_minified_file_fallback(&self) -> bool { + false + } + fn render_error(&mut self, error: Error) -> Option { self.total += 1; Some(format_unix(&error)) diff --git a/apps/oxlint/test/fixtures/github_report_long_message/.oxlintrc.json b/apps/oxlint/test/fixtures/github_report_long_message/.oxlintrc.json new file mode 100644 index 0000000000000..dfbc5f0520682 --- /dev/null +++ b/apps/oxlint/test/fixtures/github_report_long_message/.oxlintrc.json @@ -0,0 +1,7 @@ +{ + "jsPlugins": ["./plugin.ts"], + "categories": { "correctness": "off" }, + "rules": { + "plugin/long-message": "error" + } +} diff --git a/apps/oxlint/test/fixtures/github_report_long_message/files/index.ts b/apps/oxlint/test/fixtures/github_report_long_message/files/index.ts new file mode 100644 index 0000000000000..e405565d6f4bc --- /dev/null +++ b/apps/oxlint/test/fixtures/github_report_long_message/files/index.ts @@ -0,0 +1 @@ +export function test() {} diff --git a/apps/oxlint/test/fixtures/github_report_long_message/options.json b/apps/oxlint/test/fixtures/github_report_long_message/options.json new file mode 100644 index 0000000000000..1e21d7f8e6030 --- /dev/null +++ b/apps/oxlint/test/fixtures/github_report_long_message/options.json @@ -0,0 +1,3 @@ +{ + "args": ["--format", "github"] +} diff --git a/apps/oxlint/test/fixtures/github_report_long_message/output.snap.md b/apps/oxlint/test/fixtures/github_report_long_message/output.snap.md new file mode 100644 index 0000000000000..9840822e4a409 --- /dev/null +++ b/apps/oxlint/test/fixtures/github_report_long_message/output.snap.md @@ -0,0 +1,13 @@ +# Exit code +1 + +# stdout +``` +::error file=files/index.ts,line=1,endLine=2,col=1,endColumn=1,title=plugin(long-message)::This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long message + +Found 0 warnings and 1 error. +``` + +# stderr +``` +``` diff --git a/apps/oxlint/test/fixtures/github_report_long_message/plugin.ts b/apps/oxlint/test/fixtures/github_report_long_message/plugin.ts new file mode 100644 index 0000000000000..2445bd328c1ee --- /dev/null +++ b/apps/oxlint/test/fixtures/github_report_long_message/plugin.ts @@ -0,0 +1,25 @@ +import type { Plugin, Rule } from "#oxlint/plugins"; + +const rule: Rule = { + create(context) { + return { + Program(node) { + context.report({ + message: `This is a ${Array.from({ length: 240 }, () => "very").join(" ")} long message`, + node, + }); + }, + }; + }, +}; + +const plugin: Plugin = { + meta: { + name: "plugin", + }, + rules: { + "long-message": rule, + }, +}; + +export default plugin; diff --git a/crates/oxc_diagnostics/src/reporter.rs b/crates/oxc_diagnostics/src/reporter.rs index 201a3f7b996c9..312e538edcaa2 100644 --- a/crates/oxc_diagnostics/src/reporter.rs +++ b/crates/oxc_diagnostics/src/reporter.rs @@ -47,6 +47,15 @@ pub trait DiagnosticReporter { /// upheld in Oxc's API. Do not rely on this behavior. fn finish(&mut self, result: &DiagnosticResult) -> Option; + /// Whether [`DiagnosticService`](crate::service::DiagnosticService) should replace very long + /// rendered lines with the synthetic "minified file" warning. + /// + /// Human-readable reporters generally want this behavior to avoid dumping unreadable output. + /// Machine-readable or intentionally single-line reporters should disable it. + fn supports_minified_file_fallback(&self) -> bool { + true + } + /// Render a diagnostic into this reporter's desired format. For example, a JSONLinesReporter /// might return a stringified JSON object on a single line. Returns [`None`] to skip reporting /// of this diagnostic. diff --git a/crates/oxc_diagnostics/src/service.rs b/crates/oxc_diagnostics/src/service.rs index c30886139c4a1..6fbe8850af217 100644 --- a/crates/oxc_diagnostics/src/service.rs +++ b/crates/oxc_diagnostics/src/service.rs @@ -154,6 +154,7 @@ impl DiagnosticService { pub fn run(&mut self, writer: &mut dyn Write) -> DiagnosticResult { let mut warnings_count: usize = 0; let mut errors_count: usize = 0; + let supports_minified_file_fallback = self.reporter.supports_minified_file_fallback(); while let Ok(diagnostics) = self.receiver.recv() { let mut is_minified = false; @@ -187,7 +188,9 @@ impl DiagnosticService { if let Some(err_str) = self.reporter.render_error(diagnostic) { // Skip large output and print only once. // Setting to 1200 because graphical output may contain ansi escape codes and other decorations. - if err_str.lines().any(|line| line.len() >= 1200) { + if supports_minified_file_fallback + && err_str.lines().any(|line| line.len() >= 1200) + { let mut diagnostic = OxcDiagnostic::warn("File is too long to fit on the screen"); if let Some(path) = path { @@ -339,9 +342,16 @@ fn strict_canonicalize>(path: P) -> std::io::Result { #[cfg(test)] mod tests { - use crate::service::from_file_path; use std::path::PathBuf; + use crate::{ + Error, OxcDiagnostic, + reporter::{DiagnosticReporter, DiagnosticResult}, + service::from_file_path, + }; + + use super::DiagnosticService; + fn with_schema(path: &str) -> String { const EXPECTED_SCHEMA: &str = if cfg!(windows) { "file:///" } else { "file://" }; format!("{EXPECTED_SCHEMA}{path}") @@ -407,4 +417,41 @@ mod tests { assert_eq!(uri, expected); } } + + #[test] + fn preserves_long_lines_for_single_line_reporters() { + #[derive(Default)] + struct LongLineReporter { + fallback_enabled: bool, + } + + impl DiagnosticReporter for LongLineReporter { + fn finish(&mut self, _result: &DiagnosticResult) -> Option { + None + } + + fn supports_minified_file_fallback(&self) -> bool { + self.fallback_enabled + } + + fn render_error(&mut self, error: Error) -> Option { + let message = error.to_string(); + if message == "original diagnostic" { + Some(format!("{}\n", "x".repeat(1200))) + } else { + Some(format!("{message}\n")) + } + } + } + let (mut service, sender) = + DiagnosticService::new(Box::new(LongLineReporter { fallback_enabled: false })); + sender.send(vec![Error::new(OxcDiagnostic::warn("original diagnostic"))]).unwrap(); + drop(sender); + + let mut output = Vec::new(); + service.run(&mut output); + let output = String::from_utf8(output).unwrap(); + + assert_eq!(output, format!("{}\n", "x".repeat(1200))); + } }