Skip to content

Commit f1ad1cd

Browse files
committed
Support different lintcheck CARGO_TARGET_DIR
1 parent d4e7e5b commit f1ad1cd

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

lintcheck/src/input.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::time::Duration;
88
use serde::Deserialize;
99
use walkdir::{DirEntry, WalkDir};
1010

11-
use crate::{Crate, LINTCHECK_DOWNLOADS, LINTCHECK_SOURCES};
11+
use crate::{Crate, lintcheck_sources, target_dir};
1212

1313
const DEFAULT_DOCS_LINK: &str = "https://docs.rs/{krate}/{version}/src/{krate_}/{file}.html#{line}";
1414
const DEFAULT_GITHUB_LINK: &str = "{url}/blob/{hash}/src/{file}#L{line}";
@@ -201,8 +201,10 @@ impl CrateWithSource {
201201
let file_link = &self.file_link;
202202
match &self.source {
203203
CrateSource::CratesIo { version } => {
204-
let extract_dir = PathBuf::from(LINTCHECK_SOURCES);
205-
let krate_download_dir = PathBuf::from(LINTCHECK_DOWNLOADS);
204+
let extract_dir = PathBuf::from(lintcheck_sources());
205+
// Keep constant downloads path to avoid repeating work and
206+
// filling up disk space unnecessarily.
207+
let krate_download_dir = PathBuf::from("target/lintcheck/downloads/");
206208

207209
// url to download the crate from crates.io
208210
let url = format!("https://crates.io/api/v1/crates/{name}/{version}/download");
@@ -211,7 +213,7 @@ impl CrateWithSource {
211213

212214
let krate_file_path = krate_download_dir.join(format!("{name}-{version}.crate.tar.gz"));
213215
// don't download/extract if we already have done so
214-
if !krate_file_path.is_file() {
216+
if !krate_file_path.is_file() || !extract_dir.join(format!("{name}-{version}")).exists() {
215217
// create a file path to download and write the crate data into
216218
let mut krate_dest = fs::File::create(&krate_file_path).unwrap();
217219
let mut krate_req = get(&url).unwrap().into_reader();
@@ -236,7 +238,7 @@ impl CrateWithSource {
236238
},
237239
CrateSource::Git { url, commit } => {
238240
let repo_path = {
239-
let mut repo_path = PathBuf::from(LINTCHECK_SOURCES);
241+
let mut repo_path = PathBuf::from(lintcheck_sources());
240242
// add a -git suffix in case we have the same crate from crates.io and a git repo
241243
repo_path.push(format!("{name}-git"));
242244
repo_path
@@ -286,7 +288,7 @@ impl CrateWithSource {
286288
// copy path into the dest_crate_root but skip directories that contain a CACHEDIR.TAG file.
287289
// The target/ directory contains a CACHEDIR.TAG file so it is the most commonly skipped directory
288290
// as a result of this filter.
289-
let dest_crate_root = PathBuf::from(LINTCHECK_SOURCES).join(name);
291+
let dest_crate_root = PathBuf::from(lintcheck_sources()).join(name);
290292
if dest_crate_root.exists() {
291293
println!("Deleting existing directory at `{}`", dest_crate_root.display());
292294
fs::remove_dir_all(&dest_crate_root).unwrap();
@@ -326,15 +328,16 @@ impl CrateWithSource {
326328
///
327329
/// This function panics if creating one of the dirs fails.
328330
fn create_dirs(krate_download_dir: &Path, extract_dir: &Path) {
329-
fs::create_dir("target/lintcheck/").unwrap_or_else(|err| {
331+
fs::create_dir(format!("{}/lintcheck/", target_dir())).unwrap_or_else(|err| {
330332
assert_eq!(
331333
err.kind(),
332334
ErrorKind::AlreadyExists,
333335
"cannot create lintcheck target dir"
334336
);
335337
});
336-
fs::create_dir(krate_download_dir).unwrap_or_else(|err| {
337-
assert_eq!(err.kind(), ErrorKind::AlreadyExists, "cannot create crate download dir");
338+
fs::create_dir_all(krate_download_dir).unwrap_or_else(|err| {
339+
// We are allowed to reuse download dirs
340+
assert_ne!(err.kind(), ErrorKind::AlreadyExists);
338341
});
339342
fs::create_dir(extract_dir).unwrap_or_else(|err| {
340343
assert_eq!(

lintcheck/src/main.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ use input::read_crates;
4343
use output::{ClippyCheckOutput, ClippyWarning, RustcIce};
4444
use rayon::prelude::*;
4545

46-
const LINTCHECK_DOWNLOADS: &str = "target/lintcheck/downloads";
47-
const LINTCHECK_SOURCES: &str = "target/lintcheck/sources";
46+
#[must_use]
47+
pub fn target_dir() -> String {
48+
env::var("CARGO_TARGET_DIR").unwrap_or("target".to_owned())
49+
}
50+
51+
fn lintcheck_sources() -> String {
52+
format!("{}/lintcheck/sources", target_dir())
53+
}
4854

4955
/// Represents the actual source code of a crate that we ran "cargo clippy" on
5056
#[derive(Debug)]
@@ -307,15 +313,17 @@ fn main() {
307313
fn lintcheck(config: LintcheckConfig) {
308314
let clippy_ver = build_clippy(config.perf);
309315
let clippy_driver_path = fs::canonicalize(format!(
310-
"target/{}/clippy-driver{EXE_SUFFIX}",
316+
"{}/{}/clippy-driver{EXE_SUFFIX}",
317+
target_dir(),
311318
if config.perf { "release" } else { "debug" }
312319
))
313320
.unwrap();
314321

315322
// assert that clippy is found
316323
assert!(
317324
clippy_driver_path.is_file(),
318-
"target/{}/clippy-driver binary not found! {}",
325+
"{}/{}/clippy-driver binary not found! {}",
326+
target_dir(),
319327
if config.perf { "release" } else { "debug" },
320328
clippy_driver_path.display()
321329
);
@@ -386,7 +394,7 @@ fn lintcheck(config: LintcheckConfig) {
386394
.unwrap();
387395

388396
let server = config.recursive.then(|| {
389-
let _: io::Result<()> = fs::remove_dir_all("target/lintcheck/shared_target_dir/recursive");
397+
let _: io::Result<()> = fs::remove_dir_all(format!("{}/lintcheck/shared_target_dir/recursive", target_dir()));
390398

391399
LintcheckServer::spawn(recursive_options)
392400
});
@@ -488,7 +496,7 @@ fn clippy_project_root() -> &'static Path {
488496
#[must_use]
489497
fn shared_target_dir(qualifier: &str) -> PathBuf {
490498
clippy_project_root()
491-
.join("target/lintcheck/shared_target_dir")
499+
.join(format!("{}/lintcheck/shared_target_dir", target_dir()))
492500
.join(qualifier)
493501
}
494502

0 commit comments

Comments
 (0)