use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::ffi::OsString;
use std::io::IsTerminal;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
const CHECK_INTERVAL: Duration = Duration::from_secs(24 * 60 * 60);
const DEFAULT_NUDGE_THRESHOLD_GB: u64 = 10;
const BYTES_PER_GIB: u64 = 1024 * 1024 * 1024;
#[derive(Debug, Serialize, Deserialize)]
struct Cache {
checked_at_unix: u64,
size_bytes: u64,
}
pub(crate) fn target_dir(app_root: &Path) -> PathBuf {
resolve_target_dir(app_root, std::env::var_os("CARGO_TARGET_DIR"))
}
fn resolve_target_dir(app_root: &Path, cargo_target_dir: Option<OsString>) -> PathBuf {
cargo_target_dir
.map(PathBuf::from)
.unwrap_or_else(|| app_root.join("target"))
}
fn cache_path(target: &Path) -> PathBuf {
target.join(".xr-build-cache-check.json")
}
fn nudge_threshold_bytes() -> u64 {
parse_threshold_gb(std::env::var("XR_CACHE_NUDGE_THRESHOLD_GB").ok()) * BYTES_PER_GIB
}
fn parse_threshold_gb(value: Option<String>) -> u64 {
value
.and_then(|v| v.parse::<u64>().ok())
.unwrap_or(DEFAULT_NUDGE_THRESHOLD_GB)
}
pub fn maybe_notify(app_root: &Path) {
if std::env::var_os("XR_SKIP_CACHE_CHECK").is_some() {
return;
}
let target = target_dir(app_root);
if !target.is_dir() {
return;
}
let cache_file = cache_path(&target);
let mut needs_refresh = true;
if let Some(cache) = read_cache(&cache_file) {
notify_if_large(&target, cache.size_bytes);
needs_refresh = is_stale(&cache);
}
if needs_refresh {
let background_target = target.clone();
std::thread::spawn(move || {
let size = directory_size(&background_target);
write_cache(&cache_path(&background_target), size);
});
}
}
fn is_stale(cache: &Cache) -> bool {
let checked_at = UNIX_EPOCH + Duration::from_secs(cache.checked_at_unix);
SystemTime::now()
.duration_since(checked_at)
.map(|age| age >= CHECK_INTERVAL)
.unwrap_or(true)
}
fn notify_if_large(target: &Path, size_bytes: u64) {
if size_bytes < nudge_threshold_bytes() {
return;
}
let gib = size_bytes as f64 / BYTES_PER_GIB as f64;
eprintln!(
"\nxr: your build cache at {} is {gib:.1} GiB. Run `xr clean` to reclaim the space - \
it's fully regenerated by your next build. (Set XR_SKIP_CACHE_CHECK=1 to stop this \
check, or XR_CACHE_NUDGE_THRESHOLD_GB to change when it fires.)\n",
target.display()
);
}
fn read_cache(path: &Path) -> Option<Cache> {
let bytes = std::fs::read(path).ok()?;
serde_json::from_slice(&bytes).ok()
}
fn write_cache(path: &Path, size_bytes: u64) {
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let cache = Cache {
checked_at_unix: SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0),
size_bytes,
};
if let Ok(json) = serde_json::to_vec(&cache) {
let _ = std::fs::write(path, json);
}
}
fn directory_size(root: &Path) -> u64 {
let mut total = 0u64;
let mut stack = vec![root.to_path_buf()];
while let Some(dir) = stack.pop() {
let Ok(entries) = std::fs::read_dir(&dir) else {
continue;
};
for entry in entries.flatten() {
let Ok(metadata) = entry.metadata() else {
continue;
};
if metadata.is_dir() {
stack.push(entry.path());
} else {
total += metadata.len();
}
}
}
total
}
pub fn run(app_root: &Path, skip_confirm: bool, release_only: bool) -> Result<()> {
anyhow::ensure!(
app_root.join("Cargo.toml").is_file(),
"no Cargo.toml in the current directory - run `xr clean` from inside a Larust app"
);
let target = target_dir(app_root);
if !target.exists() {
println!(
"xr clean: no build cache found at {} - nothing to do.",
target.display()
);
return Ok(());
}
if !skip_confirm {
anyhow::ensure!(
std::io::stdin().is_terminal(),
"xr clean: refusing to prompt in a non-interactive terminal - pass --yes to skip \
confirmation"
);
let scope = if release_only {
"release"
} else {
"debug and release"
};
let confirmed = dialoguer::Confirm::with_theme(&dialoguer::theme::ColorfulTheme::default())
.with_prompt(format!(
"Delete {} ({scope} build cache)? It's fully regenerated by your next build.",
target.display()
))
.default(false)
.interact()
.unwrap_or(false);
if !confirmed {
println!("xr clean: cancelled.");
return Ok(());
}
}
let command_label = if release_only {
"cargo clean --release"
} else {
"cargo clean"
};
println!("xr clean: running {command_label}...");
let mut command = Command::new("cargo");
command.arg("clean").current_dir(app_root);
if release_only {
command.arg("--release");
}
let status = command
.status()
.with_context(|| format!("failed to run `{command_label}`"))?;
anyhow::ensure!(
status.success(),
"`{command_label}` exited with a non-zero status"
);
println!("xr clean: done.");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolve_target_dir_prefers_cargo_target_dir_when_set() {
let dir = resolve_target_dir(Path::new("/app"), Some(OsString::from("/custom/target")));
assert_eq!(dir, PathBuf::from("/custom/target"));
}
#[test]
fn resolve_target_dir_falls_back_to_app_root_target() {
let dir = resolve_target_dir(Path::new("/app"), None);
assert_eq!(dir, PathBuf::from("/app/target"));
}
#[test]
fn parse_threshold_gb_uses_the_default_when_unset() {
assert_eq!(parse_threshold_gb(None), DEFAULT_NUDGE_THRESHOLD_GB);
}
#[test]
fn parse_threshold_gb_uses_the_default_for_unparseable_input() {
assert_eq!(
parse_threshold_gb(Some("not-a-number".to_string())),
DEFAULT_NUDGE_THRESHOLD_GB
);
}
#[test]
fn parse_threshold_gb_honors_a_valid_override() {
assert_eq!(parse_threshold_gb(Some("50".to_string())), 50);
}
#[test]
fn is_stale_is_true_for_a_long_expired_entry() {
let cache = Cache {
checked_at_unix: 0, size_bytes: 0,
};
assert!(is_stale(&cache));
}
#[test]
fn is_stale_is_false_for_a_just_written_entry() {
let cache = Cache {
checked_at_unix: SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs(),
size_bytes: 0,
};
assert!(!is_stale(&cache));
}
#[test]
fn read_cache_is_none_for_a_missing_file() {
assert!(read_cache(Path::new("definitely/does/not/exist.json")).is_none());
}
#[test]
fn write_then_read_cache_round_trips() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("nested").join(".xr-build-cache-check.json");
write_cache(&path, 12345);
let cache = read_cache(&path).unwrap();
assert_eq!(cache.size_bytes, 12345);
}
#[test]
fn directory_size_sums_nested_files() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("a.txt"), vec![0u8; 100]).unwrap();
let nested = dir.path().join("nested");
std::fs::create_dir_all(&nested).unwrap();
std::fs::write(nested.join("b.txt"), vec![0u8; 250]).unwrap();
assert_eq!(directory_size(dir.path()), 350);
}
#[test]
fn directory_size_is_zero_for_a_missing_directory() {
assert_eq!(directory_size(Path::new("definitely/does/not/exist")), 0);
}
}