Skip to content

Commit 4a383a0

Browse files
committed
fix: strip ANSI escape codes from flycheck messages
1 parent 3099a7f commit 4a383a0

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

Cargo.lock

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ semver = "1.0.26"
144144
serde = { version = "1.0.219" }
145145
serde_derive = { version = "1.0.219" }
146146
serde_json = "1.0.140"
147+
strip-ansi-escapes = "0.2.1"
147148
rustc-hash = "2.1.1"
148149
rustc-literal-escaper = "0.0.4"
149150
smallvec = { version = "1.15.1", features = [

crates/rust-analyzer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ semver.workspace = true
5353
memchr = "2.7.5"
5454
cargo_metadata.workspace = true
5555
process-wrap.workspace = true
56+
strip-ansi-escapes.workspace = true
5657

5758
cfg.workspace = true
5859
hir-def.workspace = true

crates/rust-analyzer/src/flycheck.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,16 @@ impl FlycheckActor {
421421

422422
let formatted_command = format!("{command:?}");
423423

424+
let strip_ansi_color_codes = !matches!(
425+
self.config,
426+
FlycheckConfig::CargoCommand { ansi_color_output: false, .. }
427+
);
428+
424429
tracing::debug!(?command, "will restart flycheck");
425430
let (sender, receiver) = unbounded();
426431
match CommandHandle::spawn(
427432
command,
428-
CargoCheckParser,
433+
CargoCheckParser { strip_ansi_color_codes },
429434
sender,
430435
match &self.config {
431436
FlycheckConfig::CargoCommand { options, .. } => Some(
@@ -655,11 +660,12 @@ impl FlycheckActor {
655660
};
656661
}
657662

658-
cmd.arg(if *ansi_color_output {
659-
"--message-format=json-diagnostic-rendered-ansi"
663+
if *ansi_color_output {
664+
cmd.arg("--message-format=json-diagnostic-rendered-ansi");
660665
} else {
661-
"--message-format=json"
662-
});
666+
cmd.arg("--message-format=json");
667+
cmd.arg("--color=never");
668+
}
663669

664670
if let Some(manifest_path) = &self.manifest_path {
665671
cmd.arg("--manifest-path");
@@ -725,7 +731,9 @@ enum CargoCheckMessage {
725731
Diagnostic { diagnostic: Diagnostic, package_id: Option<Arc<PackageId>> },
726732
}
727733

728-
struct CargoCheckParser;
734+
struct CargoCheckParser {
735+
strip_ansi_color_codes: bool,
736+
}
729737

730738
impl CargoParser<CargoCheckMessage> for CargoCheckParser {
731739
fn from_line(&self, line: &str, error: &mut String) -> Option<CargoCheckMessage> {
@@ -739,15 +747,24 @@ impl CargoParser<CargoCheckMessage> for CargoCheckParser {
739747
Some(CargoCheckMessage::CompilerArtifact(artifact))
740748
}
741749
cargo_metadata::Message::CompilerMessage(msg) => {
750+
let mut diagnostic = msg.message;
751+
if self.strip_ansi_color_codes {
752+
diagnostic.rendered =
753+
diagnostic.rendered.map(strip_ansi_escapes::strip_str);
754+
}
742755
Some(CargoCheckMessage::Diagnostic {
743-
diagnostic: msg.message,
756+
diagnostic,
744757
package_id: Some(Arc::new(msg.package_id)),
745758
})
746759
}
747760
_ => None,
748761
},
749-
JsonMessage::Rustc(message) => {
750-
Some(CargoCheckMessage::Diagnostic { diagnostic: message, package_id: None })
762+
JsonMessage::Rustc(mut diagnostic) => {
763+
if self.strip_ansi_color_codes {
764+
diagnostic.rendered =
765+
diagnostic.rendered.map(strip_ansi_escapes::strip_str);
766+
}
767+
Some(CargoCheckMessage::Diagnostic { diagnostic, package_id: None })
751768
}
752769
};
753770
}

0 commit comments

Comments
 (0)