Skip to content

Commit 71d03ae

Browse files
committed
clippy-driver: if --sysroot is specified on the command line, use that
If the user explicitly sets sysroot on the command line, then use that value. Issue #3663
1 parent b5fd010 commit 71d03ae

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/driver.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,19 @@ pub fn main() {
7272
exit(0);
7373
}
7474

75-
let sys_root = option_env!("SYSROOT")
76-
.map(String::from)
75+
let mut orig_args: Vec<String> = env::args().collect();
76+
77+
// Get the sysroot, looking from most specific to this invocation to the least:
78+
// - command line
79+
// - runtime environment
80+
// - SYSROOT
81+
// - RUSTUP_HOME, MULTIRUST_HOME, RUSTUP_TOOLCHAIN, MULTIRUST_TOOLCHAIN
82+
// - sysroot from rustc in the path
83+
// - compile-time environment
84+
let sys_root_arg = arg_value(&orig_args, "--sysroot", |_| true);
85+
let have_sys_root_arg = sys_root_arg.is_some();
86+
let sys_root = sys_root_arg
87+
.map(|s| s.to_string())
7788
.or_else(|| std::env::var("SYSROOT").ok())
7889
.or_else(|| {
7990
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
@@ -89,11 +100,11 @@ pub fn main() {
89100
.and_then(|out| String::from_utf8(out.stdout).ok())
90101
.map(|s| s.trim().to_owned())
91102
})
103+
.or_else(|| option_env!("SYSROOT").map(String::from))
92104
.expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
93105

94106
// Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
95107
// We're invoking the compiler programmatically, so we ignore this/
96-
let mut orig_args: Vec<String> = env::args().collect();
97108
if orig_args.len() <= 1 {
98109
std::process::exit(1);
99110
}
@@ -104,7 +115,7 @@ pub fn main() {
104115
// this conditional check for the --sysroot flag is there so users can call
105116
// `clippy_driver` directly
106117
// without having to pass --sysroot or anything
107-
let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
118+
let mut args: Vec<String> = if have_sys_root_arg {
108119
orig_args.clone()
109120
} else {
110121
orig_args

0 commit comments

Comments
 (0)