Skip to content

Commit b9b4e2b

Browse files
committed
Set RUSTC_WRAPPER instead of RUSTC when invoking Cargo
Some build scripts rely on the RUSTC binary being the actual compiler (e.g. parsing the output of 'RUSTC --version'). To prevent clippy from breaking these build scripts, this commit sets RUSTC_WRAPPER instead. This will cause Cargo to leave RUSTC unchanged, making the use of clippy transparent to build scripts.
1 parent 387efd4 commit b9b4e2b

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/main.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub fn main() {
181181
return;
182182
}
183183

184-
if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
184+
if "clippy" == std::env::args().nth(1).as_ref().expect("cargo-clippy should be called with at least one argument!") {
185185
// this arm is executed on the initial call to `cargo clippy`
186186

187187
let manifest_path_arg = std::env::args()
@@ -285,7 +285,7 @@ pub fn main() {
285285
}
286286
}
287287
} else {
288-
// this arm is executed when cargo-clippy runs `cargo rustc` with the `RUSTC`
288+
// this arm is executed when cargo-clippy runs `cargo rustc` with the `RUSTC_WRAPPER`
289289
// env var set to itself
290290

291291
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
@@ -310,13 +310,17 @@ pub fn main() {
310310
};
311311

312312
rustc_driver::in_rustc_thread(|| {
313+
// Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
314+
// We're invoking the compiler programatically, so we ignore this/
315+
let orig_args: Vec<String> = env::args().skip(1).collect();
316+
313317
// this conditional check for the --sysroot flag is there so users can call
314318
// `cargo-clippy` directly
315319
// without having to pass --sysroot or anything
316-
let mut args: Vec<String> = if env::args().any(|s| s == "--sysroot") {
317-
env::args().collect()
320+
let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
321+
orig_args.clone()
318322
} else {
319-
env::args()
323+
orig_args.clone().into_iter()
320324
.chain(Some("--sysroot".to_owned()))
321325
.chain(Some(sys_root))
322326
.collect()
@@ -325,7 +329,7 @@ pub fn main() {
325329
// this check ensures that dependencies are built but not linted and the final
326330
// crate is
327331
// linted but not built
328-
let clippy_enabled = env::args().any(|s| s == "--emit=metadata");
332+
let clippy_enabled = orig_args.iter().any(|s| s == "--emit=metadata");
329333

330334
if clippy_enabled {
331335
args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
@@ -361,7 +365,7 @@ where
361365
let path = std::env::current_exe().expect("current executable path invalid");
362366
let exit_status = std::process::Command::new("cargo")
363367
.args(&args)
364-
.env("RUSTC", path)
368+
.env("RUSTC_WRAPPER", path)
365369
.spawn()
366370
.expect("could not run cargo")
367371
.wait()

0 commit comments

Comments
 (0)