Skip to content

Commit 1156375

Browse files
committed
Auto merge of #12203 - daivinhtran:fix-clippy-driver-to-accept-param-file, r=flip1995
Makes clippy-driver check for --sysroot in arg files Fixes rust-lang/rust-clippy#12201 --- changelog: none cc: `@UebelAndre` `@illicitonion`
2 parents 3cd713a + 73706e8 commit 1156375

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

.github/driver.sh

100644100755
+15
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ if [[ ${OS} == "Windows" ]]; then
1111
else
1212
desired_sysroot=/tmp
1313
fi
14+
# Set --sysroot in command line
1415
sysroot=$(./target/debug/clippy-driver --sysroot $desired_sysroot --print sysroot)
1516
test "$sysroot" = $desired_sysroot
1617

18+
# Set --sysroot in arg_file.txt and pass @arg_file.txt to command line
19+
echo "--sysroot=$desired_sysroot" > arg_file.txt
20+
sysroot=$(./target/debug/clippy-driver @arg_file.txt --print sysroot)
21+
test "$sysroot" = $desired_sysroot
22+
23+
# Setting SYSROOT in command line
1724
sysroot=$(SYSROOT=$desired_sysroot ./target/debug/clippy-driver --print sysroot)
1825
test "$sysroot" = $desired_sysroot
1926

@@ -24,6 +31,14 @@ test "$sysroot" = $desired_sysroot
2431
SYSROOT=/tmp RUSTFLAGS="--sysroot=$(rustc --print sysroot)" ../target/debug/cargo-clippy clippy --verbose
2532
)
2633

34+
# Check that the --sysroot argument is only passed once via arg_file.txt (SYSROOT is ignored)
35+
(
36+
echo "fn main() {}" > target/driver_test.rs
37+
echo "--sysroot="$(./target/debug/clippy-driver --print sysroot)"" > arg_file.txt
38+
echo "--verbose" >> arg_file.txt
39+
SYSROOT=/tmp ./target/debug/clippy-driver @arg_file.txt ./target/driver_test.rs
40+
)
41+
2742
# Make sure this isn't set - clippy-driver should cope without it
2843
unset CARGO_MANIFEST_DIR
2944

src/driver.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ use rustc_session::EarlyDiagCtxt;
2222
use rustc_span::symbol::Symbol;
2323

2424
use std::env;
25+
use std::fs::read_to_string;
2526
use std::ops::Deref;
2627
use std::path::Path;
2728
use std::process::exit;
29+
use std::string::ToString;
2830

2931
use anstream::println;
3032

@@ -188,12 +190,31 @@ pub fn main() {
188190

189191
exit(rustc_driver::catch_with_exit_code(move || {
190192
let mut orig_args: Vec<String> = env::args().collect();
191-
let has_sysroot_arg = arg_value(&orig_args, "--sysroot", |_| true).is_some();
193+
194+
let has_sysroot_arg = |args: &mut [String]| -> bool {
195+
if arg_value(args, "--sysroot", |_| true).is_some() {
196+
return true;
197+
}
198+
// https://doc.rust-lang.org/rustc/command-line-arguments.html#path-load-command-line-flags-from-a-path
199+
// Beside checking for existence of `--sysroot` on the command line, we need to
200+
// check for the arg files that are prefixed with @ as well to be consistent with rustc
201+
for arg in args.iter() {
202+
if let Some(arg_file_path) = arg.strip_prefix('@') {
203+
if let Ok(arg_file) = read_to_string(arg_file_path) {
204+
let split_arg_file: Vec<String> = arg_file.lines().map(ToString::to_string).collect();
205+
if arg_value(&split_arg_file, "--sysroot", |_| true).is_some() {
206+
return true;
207+
}
208+
}
209+
}
210+
}
211+
false
212+
};
192213

193214
let sys_root_env = std::env::var("SYSROOT").ok();
194215
let pass_sysroot_env_if_given = |args: &mut Vec<String>, sys_root_env| {
195216
if let Some(sys_root) = sys_root_env {
196-
if !has_sysroot_arg {
217+
if !has_sysroot_arg(args) {
197218
args.extend(vec!["--sysroot".into(), sys_root]);
198219
}
199220
};

0 commit comments

Comments
 (0)