Skip to content

Commit 980bcd8

Browse files
committed
Auto merge of rust-lang#3546 - matthiaskrgr:fix_install, r=oli-obk
Revert "Merge pull request rust-lang#3257 from o01eg/remove-sysroot" This reverts commit 041c49c, reversing changes made to 1df5766. The PR broke running a cargo-install'd clippy. The installed clippy would not be able to find a crate for std. Fixes rust-lang#3523 Reopens rust-lang#2874
2 parents fdb4d98 + 9fb8022 commit 980bcd8

File tree

4 files changed

+44
-61
lines changed

4 files changed

+44
-61
lines changed

ci/base-tests.sh

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ cd clippy_lints && cargo test && cd ..
2626
cd rustc_tools_util && cargo test && cd ..
2727
cd clippy_dev && cargo test && cd ..
2828

29+
# make sure clippy can be called via ./path/to/cargo-clippy
30+
cd clippy_workspace_tests
31+
../target/debug/cargo-clippy
32+
cd ..
33+
2934
# Perform various checks for lint registration
3035
./util/dev update_lints --check
3136
cargo +nightly fmt --all -- --check

src/driver.rs

+38-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use self::rustc_driver::{driver::CompileController, Compilation};
2323

2424
use std::convert::TryInto;
2525
use std::path::Path;
26-
use std::process::exit;
26+
use std::process::{exit, Command};
2727

2828
fn show_version() {
2929
println!(env!("CARGO_PKG_VERSION"));
@@ -40,22 +40,54 @@ pub fn main() {
4040
exit(0);
4141
}
4242

43+
let sys_root = option_env!("SYSROOT")
44+
.map(String::from)
45+
.or_else(|| std::env::var("SYSROOT").ok())
46+
.or_else(|| {
47+
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
48+
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
49+
home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
50+
})
51+
.or_else(|| {
52+
Command::new("rustc")
53+
.arg("--print")
54+
.arg("sysroot")
55+
.output()
56+
.ok()
57+
.and_then(|out| String::from_utf8(out.stdout).ok())
58+
.map(|s| s.trim().to_owned())
59+
})
60+
.expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
61+
4362
// Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
4463
// We're invoking the compiler programmatically, so we ignore this/
45-
let mut args: Vec<String> = env::args().collect();
46-
if args.len() <= 1 {
64+
let mut orig_args: Vec<String> = env::args().collect();
65+
if orig_args.len() <= 1 {
4766
std::process::exit(1);
4867
}
49-
if Path::new(&args[1]).file_stem() == Some("rustc".as_ref()) {
68+
if Path::new(&orig_args[1]).file_stem() == Some("rustc".as_ref()) {
5069
// we still want to be able to invoke it normally though
51-
args.remove(1);
70+
orig_args.remove(1);
5271
}
72+
// this conditional check for the --sysroot flag is there so users can call
73+
// `clippy_driver` directly
74+
// without having to pass --sysroot or anything
75+
let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
76+
orig_args.clone()
77+
} else {
78+
orig_args
79+
.clone()
80+
.into_iter()
81+
.chain(Some("--sysroot".to_owned()))
82+
.chain(Some(sys_root))
83+
.collect()
84+
};
5385

5486
// this check ensures that dependencies are built but not linted and the final
5587
// crate is
5688
// linted but not built
5789
let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
58-
|| args.iter().any(|s| s == "--emit=dep-info,metadata");
90+
|| orig_args.iter().any(|s| s == "--emit=dep-info,metadata");
5991

6092
if clippy_enabled {
6193
args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);

tests/compile-test.rs

+1-28
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use std::ffi::OsStr;
1717
use std::fs;
1818
use std::io;
1919
use std::path::{Path, PathBuf};
20-
use std::process::Command;
2120

2221
fn clippy_driver_path() -> PathBuf {
2322
if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
@@ -43,28 +42,6 @@ fn rustc_lib_path() -> PathBuf {
4342
option_env!("RUSTC_LIB_PATH").unwrap().into()
4443
}
4544

46-
fn rustc_sysroot_path() -> PathBuf {
47-
option_env!("SYSROOT")
48-
.map(String::from)
49-
.or_else(|| std::env::var("SYSROOT").ok())
50-
.or_else(|| {
51-
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
52-
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
53-
home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
54-
})
55-
.or_else(|| {
56-
Command::new("rustc")
57-
.arg("--print")
58-
.arg("sysroot")
59-
.output()
60-
.ok()
61-
.and_then(|out| String::from_utf8(out.stdout).ok())
62-
.map(|s| s.trim().to_owned())
63-
})
64-
.expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust")
65-
.into()
66-
}
67-
6845
fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
6946
let mut config = compiletest::Config::default();
7047

@@ -78,11 +55,7 @@ fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
7855
config.run_lib_path = rustc_lib_path();
7956
config.compile_lib_path = rustc_lib_path();
8057
}
81-
config.target_rustcflags = Some(format!(
82-
"-L {0} -L {0}/deps -Dwarnings --sysroot {1}",
83-
host_libs().display(),
84-
rustc_sysroot_path().display()
85-
));
58+
config.target_rustcflags = Some(format!("-L {0} -L {0}/deps -Dwarnings", host_libs().display()));
8659

8760
config.mode = cfg_mode;
8861
config.build_base = if rustc_test_suite().is_some() {

tests/dogfood.rs

-27
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,6 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
use std::path::PathBuf;
11-
use std::process::Command;
12-
13-
fn rustc_sysroot_path() -> PathBuf {
14-
option_env!("SYSROOT")
15-
.map(String::from)
16-
.or_else(|| std::env::var("SYSROOT").ok())
17-
.or_else(|| {
18-
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
19-
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
20-
home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
21-
})
22-
.or_else(|| {
23-
Command::new("rustc")
24-
.arg("--print")
25-
.arg("sysroot")
26-
.output()
27-
.ok()
28-
.and_then(|out| String::from_utf8(out.stdout).ok())
29-
.map(|s| s.trim().to_owned())
30-
})
31-
.expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust")
32-
.into()
33-
}
34-
3510
#[test]
3611
fn dogfood() {
3712
if option_env!("RUSTC_TEST_SUITE").is_some() || cfg!(windows) {
@@ -46,7 +21,6 @@ fn dogfood() {
4621
let output = std::process::Command::new(clippy_cmd)
4722
.current_dir(root_dir)
4823
.env("CLIPPY_DOGFOOD", "1")
49-
.env("RUSTFLAGS", format!("--sysroot {}", rustc_sysroot_path().display()))
5024
.arg("clippy")
5125
.arg("--all-targets")
5226
.arg("--all-features")
@@ -85,7 +59,6 @@ fn dogfood_tests() {
8559
let output = std::process::Command::new(&clippy_cmd)
8660
.current_dir(root_dir.join(d))
8761
.env("CLIPPY_DOGFOOD", "1")
88-
.env("RUSTFLAGS", format!("--sysroot {}", rustc_sysroot_path().display()))
8962
.arg("clippy")
9063
.arg("--")
9164
.args(&["-D", "clippy::all"])

0 commit comments

Comments
 (0)