@@ -391,7 +391,11 @@ impl Executor for RlsExecutor {
391
391
_on_stdout_line : & mut dyn FnMut ( & str ) -> CargoResult < ( ) > ,
392
392
_on_stderr_line : & mut dyn FnMut ( & str ) -> CargoResult < ( ) > ,
393
393
) -> CargoResult < ( ) > {
394
- // Use JSON output so that we can parse the rustc output.
394
+ // Enforce JSON output so that we can parse the rustc output by
395
+ // stripping --error-format if it was specified (e.g. Cargo pipelined
396
+ // build)
397
+ let filtered_args = filter_arg ( cargo_cmd. get_args ( ) , "--error-format" ) ;
398
+ cargo_cmd. args_replace ( & filtered_args) ;
395
399
cargo_cmd. arg ( "--error-format=json" ) ;
396
400
// Delete any stale data. We try and remove any json files with
397
401
// the same crate name as Cargo would emit. This includes files
@@ -755,6 +759,27 @@ fn dedup_flags(flag_str: &str) -> String {
755
759
result
756
760
}
757
761
762
+ /// Removes a selected flag of a `--flag=VALUE` or `--flag VALUE` shape from `args` (command line args for Rust).
763
+ fn filter_arg ( args : & [ OsString ] , key : & str ) -> Vec < String > {
764
+ let key_as_prefix = key. to_owned ( ) + "=" ;
765
+ let mut ret = vec ! [ ] ;
766
+
767
+ let mut iter = args. iter ( ) ;
768
+ while let Some ( arg) = iter. next ( ) {
769
+ let first = arg. to_str ( ) . unwrap ( ) ;
770
+
771
+ if first == key {
772
+ iter. next ( ) ;
773
+ } else if first. starts_with ( & key_as_prefix) {
774
+ // no-op
775
+ } else {
776
+ ret. push ( first. to_owned ( ) ) ;
777
+ }
778
+ }
779
+
780
+ ret
781
+ }
782
+
758
783
/// Error wrapper that tries to figure out which manifest the cause best relates to in the project
759
784
#[ derive( Debug ) ]
760
785
pub struct ManifestAwareError {
@@ -837,7 +862,7 @@ impl failure::Fail for ManifestAwareError {
837
862
838
863
#[ cfg( test) ]
839
864
mod test {
840
- use super :: dedup_flags;
865
+ use super :: { dedup_flags, filter_arg } ;
841
866
842
867
#[ test]
843
868
fn test_dedup_flags ( ) {
@@ -870,4 +895,21 @@ mod test {
870
895
) == " -Clink-args=-fuse-ld=gold -Ctarget-cpu=native"
871
896
) ;
872
897
}
898
+
899
+ #[ test]
900
+ fn test_filter_arg ( ) {
901
+ use std:: ffi:: OsString ;
902
+
903
+ fn args ( input : & str ) -> Vec < OsString > {
904
+ input. split_whitespace ( ) . map ( OsString :: from) . collect ( )
905
+ }
906
+
907
+ assert ! ( filter_arg( & args( "--error-format=json" ) , "--error-format" ) . is_empty( ) ) ;
908
+ assert ! ( filter_arg( & args( "--error-format json" ) , "--error-format" ) . is_empty( ) ) ;
909
+ assert_eq ! ( filter_arg( & args( "-a --error-format=json" ) , "--error-format" ) , [ "-a" ] ) ;
910
+ assert_eq ! ( filter_arg( & args( "-a --error-format json" ) , "--error-format" ) , [ "-a" ] ) ;
911
+ assert_eq ! ( filter_arg( & args( "-a --error-format=json -b" ) , "--error-format" ) , [ "-a" , "-b" ] ) ;
912
+ assert_eq ! ( filter_arg( & args( "-a --error-format json -b" ) , "--error-format" ) , [ "-a" , "-b" ] ) ;
913
+ assert_eq ! ( filter_arg( & args( "-a -b -x" ) , "--error-format" ) , [ "-a" , "-b" , "-x" ] ) ;
914
+ }
873
915
}
0 commit comments