mod report;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use rayon::prelude::*;
pub use report::{document, render_human, render_json};
use crate::delete::{Guard, Outcome, Removal};
use crate::error::Result;
pub const SCHEMA_VERSION: u32 = 1;
pub const DEFAULT_MAX_AGE: Duration = Duration::from_secs(7 * 24 * 60 * 60);
const OWNER_MARKER: &str = "DO_NOT_BUILD_HERE";
const ACTIVITY_MARKERS: &[&str] = &[
"command.log",
"lock",
"action_cache",
"execroot",
"external",
"server",
"java.log",
];
const CONTROL_FILE_MAX_BYTES: u64 = 4096;
pub const ROOTS_ENV: &str = "VOOM_BAZEL_ROOTS";
const SERVER_PID_FILE: &str = "server/server.pid.txt";
#[cfg(unix)]
const SERVER_STOP_TIMEOUT: Duration = Duration::from_secs(10);
#[derive(Debug, Clone)]
#[expect(
clippy::struct_excessive_bools,
reason = "these mirror command-line flags, which are bools by nature"
)]
pub struct BazelPruneOptions {
pub roots: Vec<PathBuf>,
pub dry_run: bool,
pub force: bool,
pub one_file_system: bool,
pub max_age: Duration,
pub scope: Option<Vec<PathBuf>>,
pub clear_all: bool,
pub jobs: Option<usize>,
}
impl BazelPruneOptions {
#[must_use]
pub fn conventional(dry_run: bool, one_file_system: bool) -> Self {
Self {
roots: conventional_roots(),
dry_run,
force: false,
one_file_system,
max_age: DEFAULT_MAX_AGE,
clear_all: false,
jobs: None,
scope: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OutputBaseState {
Unproven,
Owned {
owner: PathBuf,
},
Running {
owner: Option<PathBuf>,
},
Orphaned {
owner: PathBuf,
},
Abandoned {
owner: PathBuf,
},
Stale {
owner: Option<PathBuf>,
idle: Duration,
},
Cleared {
owner: Option<PathBuf>,
},
InstallBase {
in_use: bool,
},
Cache {
files: usize,
},
CacheCleared,
}
impl OutputBaseState {
#[must_use]
pub fn is_removable(&self) -> bool {
match self {
Self::Orphaned { .. }
| Self::Abandoned { .. }
| Self::Stale { .. }
| Self::Cleared { .. }
| Self::CacheCleared
| Self::InstallBase { in_use: false } => true,
Self::Cache { files } => *files > 0,
Self::Unproven | Self::Owned { .. } | Self::Running { .. } | Self::InstallBase { in_use: true } => false,
}
}
#[must_use]
pub fn owner(&self) -> Option<&Path> {
match self {
Self::Owned { owner } | Self::Orphaned { owner } | Self::Abandoned { owner } => Some(owner),
Self::Running { owner } | Self::Cleared { owner } | Self::Stale { owner, .. } => owner.as_deref(),
Self::Unproven | Self::InstallBase { .. } | Self::Cache { .. } | Self::CacheCleared => None,
}
}
}
#[derive(Debug, Clone)]
pub struct OutputBase {
pub path: PathBuf,
pub state: OutputBaseState,
pub bytes: Option<u64>,
pub outcome: Option<Outcome>,
}
impl OutputBase {
#[must_use]
pub fn is_removable(&self) -> bool {
self.state.is_removable()
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Totals {
pub found: usize,
pub removed: usize,
pub refused: usize,
pub kept: usize,
pub bytes: u64,
}
#[derive(Debug)]
pub struct BazelPruneResult {
pub roots: Vec<PathBuf>,
pub output_bases: Vec<OutputBase>,
pub dry_run: bool,
pub elapsed: Duration,
}
impl BazelPruneResult {
#[must_use]
pub fn totals(&self) -> Totals {
let mut totals = Totals {
found: self.output_bases.len(),
..Totals::default()
};
for base in &self.output_bases {
match (base.is_removable(), &base.outcome) {
(true, Some(Outcome::Removed | Outcome::Detached | Outcome::WouldRemove)) => {
totals.removed += 1;
totals.bytes += base.bytes.unwrap_or(0);
}
(true, Some(Outcome::Refused(_))) => totals.refused += 1,
_ => totals.kept += 1,
}
}
totals
}
pub fn absorb(&mut self, later: Self) {
for base in later.output_bases {
if base.outcome.is_none() {
continue;
}
match self.output_bases.iter_mut().find(|known| known.path == base.path) {
Some(known) if known.outcome.is_none() => *known = base,
Some(_) => {}
None => self.output_bases.push(base),
}
}
self.output_bases.sort_by(|left, right| left.path.cmp(&right.path));
self.elapsed += later.elapsed;
}
#[must_use]
pub fn exit_code(&self) -> i32 {
let failed = self.output_bases.iter().any(|base| {
matches!(
base.outcome,
Some(Outcome::Failed(_) | Outcome::PartiallyRemoved { .. })
)
});
if failed {
crate::report::exit::REMOVAL_FAILED
} else {
crate::report::exit::SUCCESS
}
}
}
#[must_use]
pub fn conventional_roots() -> Vec<PathBuf> {
if let Some(roots) = std::env::var_os(ROOTS_ENV) {
return std::env::split_paths(&roots)
.filter(|root| !root.as_os_str().is_empty())
.collect();
}
let Some(user) = current_user() else {
return Vec::new();
};
let name = format!("_bazel_{user}");
let mut roots = vec![PathBuf::from("/tmp").join(&name), PathBuf::from("/var/tmp").join(&name)];
if let Some(home) = dirs::home_dir() {
roots.push(home.join(".cache/bazel").join(&name));
roots.push(home.join("Library/Caches/bazel").join(&name));
}
roots
}
fn current_user() -> Option<String> {
std::env::var("USER").or_else(|_| std::env::var("USERNAME")).ok()
}
enum Candidate {
OutputBase(PathBuf),
InstallBase(PathBuf),
Cache(PathBuf),
}
pub fn prune(options: &BazelPruneOptions) -> Result<BazelPruneResult> {
let started = Instant::now();
let mut roots = Vec::new();
let mut candidates = Vec::new();
for root in &options.roots {
let Ok(canonical) = root.canonicalize() else {
continue;
};
candidates.extend(candidates_in(&canonical));
roots.push(canonical);
}
let guard = Guard::new(&roots, options.one_file_system)?;
let removal = Removal {
dry_run: options.dry_run,
force: options.force,
};
let (output_bases, install_bases, caches) = partition_candidates(candidates, &Scope::new(options));
let pool = crate::run::build_pool(options.jobs)?;
crate::delete::limit_removal_workers(options.jobs);
let (mut found, cache_found) = pool.install(|| {
rayon::join(
|| prune_bases(output_bases, install_bases, options, &guard, removal),
|| {
caches
.into_par_iter()
.map(|path| handle_cache(path, options, &guard, removal))
.collect::<Vec<_>>()
},
)
});
found.extend(cache_found);
found.sort_by(|left, right| left.path.cmp(&right.path));
Ok(BazelPruneResult {
roots,
output_bases: found,
dry_run: options.dry_run,
elapsed: started.elapsed(),
})
}
fn partition_candidates(candidates: Vec<Candidate>, scope: &Scope) -> (Vec<PathBuf>, Vec<PathBuf>, Vec<PathBuf>) {
let mut output_bases = Vec::new();
let mut install_bases = Vec::new();
let mut caches = Vec::new();
for candidate in candidates {
match candidate {
Candidate::OutputBase(path) if scope.holds_base(&path) => output_bases.push(path),
Candidate::InstallBase(path) if scope.machine => install_bases.push(path),
Candidate::Cache(path) if scope.machine => caches.push(path),
_ => {}
}
}
(output_bases, install_bases, caches)
}
fn prune_bases(
output_bases: Vec<PathBuf>,
install_bases: Vec<PathBuf>,
options: &BazelPruneOptions,
guard: &Guard,
removal: Removal,
) -> Vec<OutputBase> {
let mut found: Vec<OutputBase> = output_bases
.into_par_iter()
.map(|path| handle_output_base(path, options, guard, removal))
.collect();
let referenced: Vec<PathBuf> = found
.iter()
.filter(|base| !base.is_removable() || base.outcome.as_ref().is_some_and(|outcome| !outcome.is_reclaimed()))
.filter_map(|base| std::fs::canonicalize(base.path.join("install")).ok())
.collect();
found.par_extend(
install_bases
.into_par_iter()
.map(|path| handle_install_base(path, &referenced, options, guard, removal)),
);
found
}
struct Scope {
roots: Option<Vec<PathBuf>>,
machine: bool,
}
impl Scope {
fn new(options: &BazelPruneOptions) -> Self {
let roots = (!options.clear_all).then(|| options.scope.clone()).flatten();
let machine = roots.as_ref().is_none_or(|roots| {
dirs::home_dir()
.and_then(|home| home.canonicalize().ok())
.is_some_and(|home| roots.iter().any(|root| home.starts_with(root)))
});
Self { roots, machine }
}
fn holds_base(&self, output_base: &Path) -> bool {
let Some(roots) = &self.roots else {
return true;
};
let Some(owner) = read_owner(output_base) else {
return self.machine;
};
let owner = owner.canonicalize().unwrap_or(owner);
roots.iter().any(|root| owner.starts_with(root))
}
}
fn candidates_in(root: &Path) -> Vec<Candidate> {
let mut candidates = Vec::new();
let bazel_root = is_bazel_user_root(root);
for path in immediate_subdirectories(root) {
match path.file_name().and_then(|name| name.to_str()) {
Some("install") if bazel_root => {
candidates.extend(immediate_subdirectories(&path).into_iter().map(Candidate::InstallBase));
}
Some("cache") if bazel_root => candidates.push(Candidate::Cache(path)),
_ => candidates.push(Candidate::OutputBase(path)),
}
}
candidates
}
fn is_bazel_user_root(root: &Path) -> bool {
root.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| name.starts_with("_bazel_"))
}
fn immediate_subdirectories(root: &Path) -> Vec<PathBuf> {
let Ok(entries) = std::fs::read_dir(root) else {
return Vec::new();
};
entries
.flatten()
.filter(|entry| entry.file_type().is_ok_and(|file_type| file_type.is_dir()))
.map(|entry| entry.path())
.collect()
}
fn handle_output_base(path: PathBuf, options: &BazelPruneOptions, guard: &Guard, removal: Removal) -> OutputBase {
let mut state = state_of(&path, options);
if let OutputBaseState::Cleared { owner } = &state
&& !options.dry_run
&& !stop_server(&path)
{
state = OutputBaseState::Running { owner: owner.clone() };
}
finish_removal(path, state, guard, removal)
}
fn finish_removal(path: PathBuf, state: OutputBaseState, guard: &Guard, removal: Removal) -> OutputBase {
if !state.is_removable() {
return OutputBase {
path,
state,
bytes: None,
outcome: None,
};
}
let removal = Removal { force: true, ..removal };
let (outcome, bytes) = guard.reclaim(&path, removal);
let gone = matches!(outcome, Outcome::Removed | Outcome::Detached);
if gone && let Some(owner) = state.owner() {
unlink_convenience_symlinks(owner, &path);
}
OutputBase {
path,
state,
bytes: (outcome != Outcome::Detached).then_some(bytes),
outcome: Some(outcome),
}
}
fn state_of(output_base: &Path, options: &BazelPruneOptions) -> OutputBaseState {
let owner = read_owner(output_base);
if !options.clear_all && running_server(output_base) {
return OutputBaseState::Running { owner };
}
if options.clear_all && (owner.is_some() || is_a_bazel_base(output_base)) {
return OutputBaseState::Cleared { owner };
}
let Some(owner) = owner else {
return match idle_time(output_base) {
Some(idle) if is_a_bazel_base(output_base) && idle > options.max_age => {
OutputBaseState::Stale { owner: None, idle }
}
_ => OutputBaseState::Unproven,
};
};
if !owner.exists() {
return OutputBaseState::Orphaned { owner };
}
if is_dead_worktree(&owner) {
return OutputBaseState::Abandoned { owner };
}
match idle_time(output_base) {
Some(idle) if idle > options.max_age => OutputBaseState::Stale {
owner: Some(owner),
idle,
},
_ => OutputBaseState::Owned { owner },
}
}
fn is_bazel_root(output_base: &Path) -> bool {
output_base
.parent()
.and_then(Path::file_name)
.and_then(|name| name.to_str())
.is_some_and(|name| name.starts_with("_bazel_"))
}
fn is_a_bazel_base(output_base: &Path) -> bool {
is_bazel_root(output_base) && output_base.join("execroot").is_dir()
}
fn read_owner(output_base: &Path) -> Option<PathBuf> {
for candidate in [
output_base.join(OWNER_MARKER),
output_base.join("execroot").join(OWNER_MARKER),
] {
if let Some(contents) = crate::io::read_capped_text(&candidate, CONTROL_FILE_MAX_BYTES) {
let trimmed = contents.trim();
if !trimmed.is_empty() {
return Some(PathBuf::from(trimmed));
}
}
}
None
}
fn is_dead_worktree(owner: &Path) -> bool {
let Some(contents) = crate::io::read_capped_text(&owner.join(".git"), CONTROL_FILE_MAX_BYTES) else {
return false;
};
let Some(target) = contents.trim().strip_prefix("gitdir:") else {
return false;
};
let target = Path::new(target.trim());
let resolved = if target.is_absolute() {
target.to_path_buf()
} else {
owner.join(target)
};
!resolved.exists()
}
fn idle_time(output_base: &Path) -> Option<Duration> {
let newest = std::iter::once(output_base.to_path_buf())
.chain(ACTIVITY_MARKERS.iter().map(|name| output_base.join(name)))
.filter_map(|path| std::fs::metadata(path).and_then(|metadata| metadata.modified()).ok())
.max()?;
std::time::SystemTime::now().duration_since(newest).ok()
}
#[cfg(unix)]
fn server_pid(output_base: &Path) -> Option<u32> {
let pid: u32 = crate::io::read_capped_text(&output_base.join(SERVER_PID_FILE), CONTROL_FILE_MAX_BYTES)?
.trim()
.parse()
.ok()?;
let output = std::process::Command::new("ps")
.args(["-p", &pid.to_string(), "-o", "command="])
.output()
.ok()?;
if !output.status.success() {
return None;
}
let command = String::from_utf8_lossy(&output.stdout);
command.contains(&*output_base.to_string_lossy()).then_some(pid)
}
#[cfg(not(unix))]
fn server_pid(output_base: &Path) -> Option<u32> {
crate::io::read_capped_text(&output_base.join(SERVER_PID_FILE), CONTROL_FILE_MAX_BYTES)?
.trim()
.parse()
.ok()
}
fn running_server(output_base: &Path) -> bool {
server_pid(output_base).is_some()
}
#[cfg(unix)]
fn stop_server(output_base: &Path) -> bool {
let Some(pid) = server_pid(output_base) else {
return true;
};
let _ = std::process::Command::new("kill")
.args(["-TERM", &pid.to_string()])
.status();
let deadline = Instant::now() + SERVER_STOP_TIMEOUT;
while Instant::now() < deadline {
if server_pid(output_base).is_none() {
return true;
}
std::thread::sleep(Duration::from_millis(200));
}
false
}
#[cfg(not(unix))]
fn stop_server(_output_base: &Path) -> bool {
false
}
fn unlink_convenience_symlinks(owner: &Path, removed_base: &Path) {
let Ok(entries) = std::fs::read_dir(owner) else {
return;
};
for entry in entries.flatten() {
if !entry.file_name().to_string_lossy().starts_with("bazel-") {
continue;
}
let path = entry.path();
let points_into_base = std::fs::read_link(&path).is_ok_and(|target| {
target.is_absolute()
&& !target
.components()
.any(|component| matches!(component, std::path::Component::ParentDir))
&& target.starts_with(removed_base)
});
if points_into_base {
let _ = std::fs::remove_file(&path);
}
}
}
fn handle_install_base(
path: PathBuf,
referenced: &[PathBuf],
options: &BazelPruneOptions,
guard: &Guard,
removal: Removal,
) -> OutputBase {
let in_use = !options.clear_all
&& path
.canonicalize()
.is_ok_and(|canonical| referenced.contains(&canonical));
finish_removal(path, OutputBaseState::InstallBase { in_use }, guard, removal)
}
fn handle_cache(path: PathBuf, options: &BazelPruneOptions, guard: &Guard, removal: Removal) -> OutputBase {
if options.clear_all {
return finish_removal(path, OutputBaseState::CacheCleared, guard, removal);
}
let now = std::time::SystemTime::now();
let aged = collect_aged_files(&path, now, options.max_age);
let results: Vec<(u64, Outcome)> = aged
.par_iter()
.map(|(file, size)| (*size, guard.remove(file, *size, removal)))
.collect();
let mut bytes = 0;
let mut outcome = None;
for (size, result) in results {
if result.is_reclaimed() {
bytes += size;
}
if outcome.is_none() || !result.is_reclaimed() {
outcome = Some(result);
}
}
if !removal.dry_run {
remove_empty_directories(&path);
}
OutputBase {
path,
state: OutputBaseState::Cache { files: aged.len() },
bytes: (!aged.is_empty()).then_some(bytes),
outcome,
}
}
fn children(dir: &Path) -> Vec<(PathBuf, Option<std::fs::FileType>)> {
let Ok(entries) = std::fs::read_dir(dir) else {
return Vec::new();
};
entries
.flatten()
.map(|entry| (entry.path(), entry.file_type().ok()))
.collect()
}
fn collect_aged_files(dir: &Path, now: std::time::SystemTime, max_age: Duration) -> Vec<(PathBuf, u64)> {
children(dir)
.into_par_iter()
.flat_map_iter(|(path, file_type)| {
let Some(file_type) = file_type else {
return Vec::new();
};
if file_type.is_dir() {
return collect_aged_files(&path, now, max_age);
}
if !file_type.is_file() {
return Vec::new();
}
let Ok(metadata) = std::fs::symlink_metadata(&path) else {
return Vec::new();
};
let aged = metadata
.modified()
.ok()
.and_then(|modified| now.duration_since(modified).ok())
.is_some_and(|age| age > max_age);
if aged { vec![(path, metadata.len())] } else { Vec::new() }
})
.collect()
}
fn remove_empty_directories(root: &Path) {
children(root).into_par_iter().for_each(|(path, file_type)| {
if file_type.is_some_and(|file_type| file_type.is_dir()) {
remove_empty_directories(&path);
let _ = std::fs::remove_dir(&path);
}
});
}
#[cfg(test)]
mod tests;