@@ -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
730738impl 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