Skip to content

Support different lintcheck CARGO_TARGET_DIR env variables #14859

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 12 additions & 9 deletions lintcheck/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Expand Down Expand Up @@ -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");
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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!(
Expand Down
20 changes: 14 additions & 6 deletions lintcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -307,15 +313,17 @@ 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();

// 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()
);
Expand Down Expand Up @@ -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)
});
Expand Down Expand Up @@ -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)
}

Expand Down