Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target directory cleanup #7646

Merged
merged 8 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[alias]
uitest = "test --test compile-test"
dev = "run --target-dir clippy_dev/target --package clippy_dev --bin clippy_dev --manifest-path clippy_dev/Cargo.toml --"
lintcheck = "run --target-dir lintcheck/target --package lintcheck --bin lintcheck --manifest-path lintcheck/Cargo.toml -- "
dev = "run --package clippy_dev --bin clippy_dev --manifest-path clippy_dev/Cargo.toml --"
lintcheck = "run --package lintcheck --bin lintcheck --manifest-path lintcheck/Cargo.toml -- "
collect-metadata = "test --test dogfood --features metadata-collector-lint -- run_metadata_collection_lint --ignored"

[build]
# -Zbinary-dep-depinfo allows us to track which rlib files to use for compiling UI tests
rustflags = ["-Zunstable-options", "-Zbinary-dep-depinfo"]
target-dir = "target"
20 changes: 4 additions & 16 deletions clippy_dev/src/bless.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! `bless` updates the reference files in the repo with changed output files
//! from the last test run.

use std::env;
use std::ffi::OsStr;
use std::fs;
use std::lazy::SyncLazy;
Expand All @@ -10,17 +9,9 @@ use walkdir::WalkDir;

use crate::clippy_project_root;

// NOTE: this is duplicated with tests/cargo/mod.rs What to do?
pub static CARGO_TARGET_DIR: SyncLazy<PathBuf> = SyncLazy::new(|| match env::var_os("CARGO_TARGET_DIR") {
Some(v) => v.into(),
None => env::current_dir().unwrap().join("target"),
});

static CLIPPY_BUILD_TIME: SyncLazy<Option<std::time::SystemTime>> = SyncLazy::new(|| {
let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
let mut path = PathBuf::from(&**CARGO_TARGET_DIR);
path.push(profile);
path.push("cargo-clippy");
let mut path = std::env::current_exe().unwrap();
path.set_file_name("cargo-clippy");
fs::metadata(path).ok()?.modified().ok()
});

Expand Down Expand Up @@ -94,10 +85,7 @@ fn updated_since_clippy_build(path: &Path) -> Option<bool> {
}

fn build_dir() -> PathBuf {
let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
let mut path = PathBuf::new();
path.push(CARGO_TARGET_DIR.clone());
path.push(profile);
path.push("test_build_base");
let mut path = std::env::current_exe().unwrap();
path.set_file_name("test");
path
}
20 changes: 1 addition & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use rustc_tools_util::VersionInfo;
use std::env;
use std::ffi::OsString;
use std::path::PathBuf;
use std::process::{self, Command};

Expand All @@ -14,7 +13,7 @@ Usage:
cargo clippy [options] [--] [<opts>...]

Common options:
--no-deps Run Clippy only on the given crate, without linting the dependencies
--no-deps Run Clippy only on the given crate, without linting the dependencies
--fix Automatically apply lint suggestions. This flag implies `--no-deps`
-h, --help Print this message
-V, --version Print version info and exit
Expand Down Expand Up @@ -116,22 +115,6 @@ impl ClippyCmd {
path
}

fn target_dir() -> Option<(&'static str, OsString)> {
env::var_os("CLIPPY_DOGFOOD")
.map(|_| {
env::var_os("CARGO_MANIFEST_DIR").map_or_else(
|| std::ffi::OsString::from("clippy_dogfood"),
|d| {
std::path::PathBuf::from(d)
.join("target")
.join("dogfood")
.into_os_string()
},
)
})
.map(|p| ("CARGO_TARGET_DIR", p))
}

fn into_std_cmd(self) -> Command {
let mut cmd = Command::new("cargo");
let clippy_args: String = self
Expand All @@ -141,7 +124,6 @@ impl ClippyCmd {
.collect();

cmd.env("RUSTC_WORKSPACE_WRAPPER", Self::path())
.envs(ClippyCmd::target_dir())
.env("CLIPPY_ARGS", clippy_args)
.arg(self.cargo_subcommand)
.args(&self.args);
Expand Down
22 changes: 0 additions & 22 deletions tests/cargo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
use std::env;
use std::lazy::SyncLazy;
use std::path::PathBuf;

pub static CARGO_TARGET_DIR: SyncLazy<PathBuf> = SyncLazy::new(|| match env::var_os("CARGO_TARGET_DIR") {
Some(v) => v.into(),
None => env::current_dir().unwrap().join("target"),
});

pub static TARGET_LIB: SyncLazy<PathBuf> = SyncLazy::new(|| {
if let Some(path) = option_env!("TARGET_LIBS") {
path.into()
} else {
let mut dir = CARGO_TARGET_DIR.clone();
if let Some(target) = env::var_os("CARGO_BUILD_TARGET") {
dir.push(target);
}
dir.push(env!("PROFILE"));
dir
}
});

#[must_use]
pub fn is_rustc_test_suite() -> bool {
option_env!("RUSTC_TEST_SUITE").is_some()
Expand Down
31 changes: 16 additions & 15 deletions tests/compile-test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(test)] // compiletest_rs requires this attribute
#![feature(once_cell)]
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
#![warn(rust_2018_idioms, unused_lifetimes)]

Expand Down Expand Up @@ -46,14 +45,6 @@ extern crate quote;
#[allow(unused_extern_crates)]
extern crate syn;

fn host_lib() -> PathBuf {
option_env!("HOST_LIBS").map_or(cargo::CARGO_TARGET_DIR.join(env!("PROFILE")), PathBuf::from)
}

fn clippy_driver_path() -> PathBuf {
option_env!("CLIPPY_DRIVER_PATH").map_or(cargo::TARGET_LIB.join("clippy-driver"), PathBuf::from)
}

/// Produces a string with an `--extern` flag for all UI test crate
/// dependencies.
///
Expand Down Expand Up @@ -104,7 +95,7 @@ fn extern_flags() -> String {
}
crates
.into_iter()
.map(|(name, path)| format!("--extern {}={} ", name, path))
.map(|(name, path)| format!(" --extern {}={}", name, path))
.collect()
}

Expand All @@ -120,19 +111,29 @@ fn default_config() -> compiletest::Config {
config.run_lib_path = path.clone();
config.compile_lib_path = path;
}
let current_exe_path = std::env::current_exe().unwrap();
let deps_path = current_exe_path.parent().unwrap();
let profile_path = deps_path.parent().unwrap();

// Using `-L dependency={}` enforces that external dependencies are added with `--extern`.
// This is valuable because a) it allows us to monitor what external dependencies are used
// and b) it ensures that conflicting rlibs are resolved properly.
let host_libs = option_env!("HOST_LIBS")
.map(|p| format!(" -L dependency={}", Path::new(p).join("deps").display()))
.unwrap_or_default();
config.target_rustcflags = Some(format!(
"--emit=metadata -L dependency={} -L dependency={} -Dwarnings -Zui-testing {}",
host_lib().join("deps").display(),
cargo::TARGET_LIB.join("deps").display(),
"--emit=metadata -Dwarnings -Zui-testing -L dependency={}{}{}",
deps_path.display(),
host_libs,
extern_flags(),
));

config.build_base = host_lib().join("test_build_base");
config.rustc_path = clippy_driver_path();
config.build_base = profile_path.join("test");
config.rustc_path = profile_path.join(if cfg!(windows) {
"clippy-driver.exe"
} else {
"clippy-driver"
});
config
}

Expand Down
12 changes: 6 additions & 6 deletions tests/dogfood.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ use std::process::Command;

mod cargo;

static CLIPPY_PATH: SyncLazy<PathBuf> = SyncLazy::new(|| cargo::TARGET_LIB.join("cargo-clippy"));
static CLIPPY_PATH: SyncLazy<PathBuf> = SyncLazy::new(|| {
let mut path = std::env::current_exe().unwrap();
assert!(path.pop()); // deps
path.set_file_name("cargo-clippy");
path
});

#[test]
fn dogfood_clippy() {
Expand All @@ -28,7 +33,6 @@ fn dogfood_clippy() {
let mut command = Command::new(&*CLIPPY_PATH);
command
.current_dir(root_dir)
.env("CLIPPY_DOGFOOD", "1")
.env("CARGO_INCREMENTAL", "0")
.arg("clippy")
.arg("--all-targets")
Expand Down Expand Up @@ -74,7 +78,6 @@ fn test_no_deps_ignores_path_deps_in_workspaces() {
// Make sure that with the `--no-deps` argument Clippy does not run on `path_dep`.
let output = Command::new(&*CLIPPY_PATH)
.current_dir(&cwd)
.env("CLIPPY_DOGFOOD", "1")
.env("CARGO_INCREMENTAL", "0")
.arg("clippy")
.args(&["-p", "subcrate"])
Expand All @@ -94,7 +97,6 @@ fn test_no_deps_ignores_path_deps_in_workspaces() {
// Test that without the `--no-deps` argument, `path_dep` is linted.
let output = Command::new(&*CLIPPY_PATH)
.current_dir(&cwd)
.env("CLIPPY_DOGFOOD", "1")
.env("CARGO_INCREMENTAL", "0")
.arg("clippy")
.args(&["-p", "subcrate"])
Expand All @@ -121,7 +123,6 @@ fn test_no_deps_ignores_path_deps_in_workspaces() {
let successful_build = || {
let output = Command::new(&*CLIPPY_PATH)
.current_dir(&cwd)
.env("CLIPPY_DOGFOOD", "1")
.env("CARGO_INCREMENTAL", "0")
.arg("clippy")
.args(&["-p", "subcrate"])
Expand Down Expand Up @@ -223,7 +224,6 @@ fn run_clippy_for_project(project: &str) {

command
.current_dir(root_dir.join(project))
.env("CLIPPY_DOGFOOD", "1")
.env("CARGO_INCREMENTAL", "0")
.arg("clippy")
.arg("--all-targets")
Expand Down