Support different lintcheck CARGO_TARGET_DIR env variables (#14859)

Make lintcheck support different `CARGO_TARGET_DIR`, do not hardcode
`target` (useful for perf)

changelog:none
This commit is contained in:
dswij 2025-05-24 16:57:47 +00:00 committed by GitHub
commit 24a2a6629d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 15 deletions

View file

@ -8,7 +8,7 @@ use std::time::Duration;
use serde::Deserialize;
use walkdir::{DirEntry, WalkDir};
use crate::{Crate, LINTCHECK_DOWNLOADS, LINTCHECK_SOURCES};
use crate::{Crate, lintcheck_sources, target_dir};
const DEFAULT_DOCS_LINK: &str = "https://docs.rs/{krate}/{version}/src/{krate_}/{file}.html#{line}";
const DEFAULT_GITHUB_LINK: &str = "{url}/blob/{hash}/src/{file}#L{line}";
@ -201,8 +201,10 @@ impl CrateWithSource {
let file_link = &self.file_link;
match &self.source {
CrateSource::CratesIo { version } => {
let extract_dir = PathBuf::from(LINTCHECK_SOURCES);
let krate_download_dir = PathBuf::from(LINTCHECK_DOWNLOADS);
let extract_dir = PathBuf::from(lintcheck_sources());
// Keep constant downloads path to avoid repeating work and
// filling up disk space unnecessarily.
let krate_download_dir = PathBuf::from("target/lintcheck/downloads/");
// url to download the crate from crates.io
let url = format!("https://crates.io/api/v1/crates/{name}/{version}/download");
@ -211,7 +213,7 @@ impl CrateWithSource {
let krate_file_path = krate_download_dir.join(format!("{name}-{version}.crate.tar.gz"));
// don't download/extract if we already have done so
if !krate_file_path.is_file() {
if !krate_file_path.is_file() || !extract_dir.join(format!("{name}-{version}")).exists() {
// create a file path to download and write the crate data into
let mut krate_dest = fs::File::create(&krate_file_path).unwrap();
let mut krate_req = get(&url).unwrap().into_reader();
@ -236,7 +238,7 @@ impl CrateWithSource {
},
CrateSource::Git { url, commit } => {
let repo_path = {
let mut repo_path = PathBuf::from(LINTCHECK_SOURCES);
let mut repo_path = PathBuf::from(lintcheck_sources());
// add a -git suffix in case we have the same crate from crates.io and a git repo
repo_path.push(format!("{name}-git"));
repo_path
@ -286,7 +288,7 @@ impl CrateWithSource {
// copy path into the dest_crate_root but skip directories that contain a CACHEDIR.TAG file.
// The target/ directory contains a CACHEDIR.TAG file so it is the most commonly skipped directory
// as a result of this filter.
let dest_crate_root = PathBuf::from(LINTCHECK_SOURCES).join(name);
let dest_crate_root = PathBuf::from(lintcheck_sources()).join(name);
if dest_crate_root.exists() {
println!("Deleting existing directory at `{}`", dest_crate_root.display());
fs::remove_dir_all(&dest_crate_root).unwrap();
@ -326,15 +328,16 @@ impl CrateWithSource {
///
/// This function panics if creating one of the dirs fails.
fn create_dirs(krate_download_dir: &Path, extract_dir: &Path) {
fs::create_dir("target/lintcheck/").unwrap_or_else(|err| {
fs::create_dir(format!("{}/lintcheck/", target_dir())).unwrap_or_else(|err| {
assert_eq!(
err.kind(),
ErrorKind::AlreadyExists,
"cannot create lintcheck target dir"
);
});
fs::create_dir(krate_download_dir).unwrap_or_else(|err| {
assert_eq!(err.kind(), ErrorKind::AlreadyExists, "cannot create crate download dir");
fs::create_dir_all(krate_download_dir).unwrap_or_else(|err| {
// We are allowed to reuse download dirs
assert_ne!(err.kind(), ErrorKind::AlreadyExists);
});
fs::create_dir(extract_dir).unwrap_or_else(|err| {
assert_eq!(

View file

@ -43,8 +43,14 @@ use input::read_crates;
use output::{ClippyCheckOutput, ClippyWarning, RustcIce};
use rayon::prelude::*;
const LINTCHECK_DOWNLOADS: &str = "target/lintcheck/downloads";
const LINTCHECK_SOURCES: &str = "target/lintcheck/sources";
#[must_use]
pub fn target_dir() -> String {
env::var("CARGO_TARGET_DIR").unwrap_or("target".to_owned())
}
fn lintcheck_sources() -> String {
format!("{}/lintcheck/sources", target_dir())
}
/// Represents the actual source code of a crate that we ran "cargo clippy" on
#[derive(Debug)]
@ -307,7 +313,8 @@ fn main() {
fn lintcheck(config: LintcheckConfig) {
let clippy_ver = build_clippy(config.perf);
let clippy_driver_path = fs::canonicalize(format!(
"target/{}/clippy-driver{EXE_SUFFIX}",
"{}/{}/clippy-driver{EXE_SUFFIX}",
target_dir(),
if config.perf { "release" } else { "debug" }
))
.unwrap();
@ -315,7 +322,8 @@ fn lintcheck(config: LintcheckConfig) {
// assert that clippy is found
assert!(
clippy_driver_path.is_file(),
"target/{}/clippy-driver binary not found! {}",
"{}/{}/clippy-driver binary not found! {}",
target_dir(),
if config.perf { "release" } else { "debug" },
clippy_driver_path.display()
);
@ -386,7 +394,7 @@ fn lintcheck(config: LintcheckConfig) {
.unwrap();
let server = config.recursive.then(|| {
let _: io::Result<()> = fs::remove_dir_all("target/lintcheck/shared_target_dir/recursive");
let _: io::Result<()> = fs::remove_dir_all(format!("{}/lintcheck/shared_target_dir/recursive", target_dir()));
LintcheckServer::spawn(recursive_options)
});
@ -488,7 +496,7 @@ fn clippy_project_root() -> &'static Path {
#[must_use]
fn shared_target_dir(qualifier: &str) -> PathBuf {
clippy_project_root()
.join("target/lintcheck/shared_target_dir")
.join(format!("{}/lintcheck/shared_target_dir", target_dir()))
.join(qualifier)
}