Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 597c9be

Browse files
authored
Merge pull request #1500 from Xanewok/error-format
Ensure that --error-format is only passed once to `rustc`
2 parents bc871c7 + 202ce5d commit 597c9be

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

rls/src/build/cargo.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,11 @@ impl Executor for RlsExecutor {
391391
_on_stdout_line: &mut dyn FnMut(&str) -> CargoResult<()>,
392392
_on_stderr_line: &mut dyn FnMut(&str) -> CargoResult<()>,
393393
) -> 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);
395399
cargo_cmd.arg("--error-format=json");
396400
// Delete any stale data. We try and remove any json files with
397401
// the same crate name as Cargo would emit. This includes files
@@ -755,6 +759,27 @@ fn dedup_flags(flag_str: &str) -> String {
755759
result
756760
}
757761

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+
758783
/// Error wrapper that tries to figure out which manifest the cause best relates to in the project
759784
#[derive(Debug)]
760785
pub struct ManifestAwareError {
@@ -837,7 +862,7 @@ impl failure::Fail for ManifestAwareError {
837862

838863
#[cfg(test)]
839864
mod test {
840-
use super::dedup_flags;
865+
use super::{dedup_flags, filter_arg};
841866

842867
#[test]
843868
fn test_dedup_flags() {
@@ -870,4 +895,21 @@ mod test {
870895
) == " -Clink-args=-fuse-ld=gold -Ctarget-cpu=native"
871896
);
872897
}
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+
}
873915
}

0 commit comments

Comments
 (0)