From 73706e8358dd8d8a792c6d596ddbda4080a0c227 Mon Sep 17 00:00:00 2001 From: Vinh Tran Date: Thu, 25 Jan 2024 19:08:43 -0500 Subject: [PATCH] Makes clippy-driver check for --sysroot in arg files Fixes https://github.com/rust-lang/rust-clippy/issues/12201 --- .github/driver.sh | 15 +++++++++++++++ src/driver.rs | 25 +++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) mode change 100644 => 100755 .github/driver.sh diff --git a/.github/driver.sh b/.github/driver.sh old mode 100644 new mode 100755 index c05c6ecc1151..40a2aad0f537 --- a/.github/driver.sh +++ b/.github/driver.sh @@ -11,9 +11,16 @@ if [[ ${OS} == "Windows" ]]; then else desired_sysroot=/tmp fi +# Set --sysroot in command line sysroot=$(./target/debug/clippy-driver --sysroot $desired_sysroot --print sysroot) test "$sysroot" = $desired_sysroot +# Set --sysroot in arg_file.txt and pass @arg_file.txt to command line +echo "--sysroot=$desired_sysroot" > arg_file.txt +sysroot=$(./target/debug/clippy-driver @arg_file.txt --print sysroot) +test "$sysroot" = $desired_sysroot + +# Setting SYSROOT in command line sysroot=$(SYSROOT=$desired_sysroot ./target/debug/clippy-driver --print sysroot) test "$sysroot" = $desired_sysroot @@ -24,6 +31,14 @@ test "$sysroot" = $desired_sysroot SYSROOT=/tmp RUSTFLAGS="--sysroot=$(rustc --print sysroot)" ../target/debug/cargo-clippy clippy --verbose ) +# Check that the --sysroot argument is only passed once via arg_file.txt (SYSROOT is ignored) +( + echo "fn main() {}" > target/driver_test.rs + echo "--sysroot="$(./target/debug/clippy-driver --print sysroot)"" > arg_file.txt + echo "--verbose" >> arg_file.txt + SYSROOT=/tmp ./target/debug/clippy-driver @arg_file.txt ./target/driver_test.rs +) + # Make sure this isn't set - clippy-driver should cope without it unset CARGO_MANIFEST_DIR diff --git a/src/driver.rs b/src/driver.rs index b944a299256c..f5e52f787ab4 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -22,9 +22,11 @@ use rustc_session::EarlyDiagCtxt; use rustc_span::symbol::Symbol; use std::env; +use std::fs::read_to_string; use std::ops::Deref; use std::path::Path; use std::process::exit; +use std::string::ToString; use anstream::println; @@ -188,12 +190,31 @@ pub fn main() { exit(rustc_driver::catch_with_exit_code(move || { let mut orig_args: Vec = env::args().collect(); - let has_sysroot_arg = arg_value(&orig_args, "--sysroot", |_| true).is_some(); + + let has_sysroot_arg = |args: &mut [String]| -> bool { + if arg_value(args, "--sysroot", |_| true).is_some() { + return true; + } + // https://doc.rust-lang.org/rustc/command-line-arguments.html#path-load-command-line-flags-from-a-path + // Beside checking for existence of `--sysroot` on the command line, we need to + // check for the arg files that are prefixed with @ as well to be consistent with rustc + for arg in args.iter() { + if let Some(arg_file_path) = arg.strip_prefix('@') { + if let Ok(arg_file) = read_to_string(arg_file_path) { + let split_arg_file: Vec = arg_file.lines().map(ToString::to_string).collect(); + if arg_value(&split_arg_file, "--sysroot", |_| true).is_some() { + return true; + } + } + } + } + false + }; let sys_root_env = std::env::var("SYSROOT").ok(); let pass_sysroot_env_if_given = |args: &mut Vec, sys_root_env| { if let Some(sys_root) = sys_root_env { - if !has_sysroot_arg { + if !has_sysroot_arg(args) { args.extend(vec!["--sysroot".into(), sys_root]); } };