From bc21abbd79ba45a2a770dde28f26479ccc5a2e4e Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 3 Sep 2026 07:43:04 -0400 Subject: [PATCH] Garbage-collect old incremental compilation sessions --- compiler/rustc_incremental/src/persist/fs.rs | 8 +++-- .../run-make/incremental-session-gc/empty.rs | 2 ++ .../run-make/incremental-session-gc/rmake.rs | 33 +++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/run-make/incremental-session-gc/empty.rs create mode 100644 tests/run-make/incremental-session-gc/rmake.rs diff --git a/compiler/rustc_incremental/src/persist/fs.rs b/compiler/rustc_incremental/src/persist/fs.rs index de543ef0c53..6dcccccf0d3 100644 --- a/compiler/rustc_incremental/src/persist/fs.rs +++ b/compiler/rustc_incremental/src/persist/fs.rs @@ -702,8 +702,10 @@ pub(crate) fn garbage_collect_session_directories( lock_file_to_session_dir.items().filter_map(|(lock_file_name, directory_name)| { debug!("garbage_collect_session_directories() - inspecting: {}", directory_name); - if directory_name.as_str() == current_session_directory_name { - // Skipping our own directory is, unfortunately, important for correctness. + if directory_name.as_str() == current_session_directory_name + && !is_finalized(directory_name) + { + // Skipping our own active directory is important for correctness. // // To summarize #147821: we will try to lock directories before deciding they can be // garbage collected, but the ability of `flock::Lock` to detect a lock held *by the @@ -722,6 +724,8 @@ pub(crate) fn garbage_collect_session_directories( // It's not clear that `flock::Lock` can be fixed for this in general, and our own // incremental session directory is the only one which this process may own, so skip // it here and avoid the problem. We know it's not garbage anyway: we're using it. + // Once finalized, its lock is released. Include it in collection so we keep only + // the newest completed session. return None; } diff --git a/tests/run-make/incremental-session-gc/empty.rs b/tests/run-make/incremental-session-gc/empty.rs new file mode 100644 index 00000000000..da27b7f3463 --- /dev/null +++ b/tests/run-make/incremental-session-gc/empty.rs @@ -0,0 +1,2 @@ +#![feature(no_core)] +#![no_core] diff --git a/tests/run-make/incremental-session-gc/rmake.rs b/tests/run-make/incremental-session-gc/rmake.rs new file mode 100644 index 00000000000..a8ffe15b26d --- /dev/null +++ b/tests/run-make/incremental-session-gc/rmake.rs @@ -0,0 +1,33 @@ +//! Successful sequential builds should retain only the newest incremental session. +//! The current session must participate in garbage collection once it is finalized. + +use std::path::PathBuf; + +use run_make_support::{rfs, rustc, shallow_find_directories}; + +fn main() { + let compile = || { + rustc().input("empty.rs").crate_type("rlib").emit("metadata").incremental("incr").run(); + }; + + compile(); + let mut previous = session_dir(); + rfs::write(previous.join("sentinel"), "previous session"); + + for _ in 0..2 { + compile(); + let current = session_dir(); + assert_ne!(previous, current); + assert!(!previous.exists(), "superseded session was not collected: {previous:?}"); + assert_eq!(rfs::read_to_string(current.join("sentinel")), "previous session"); + previous = current; + } +} + +fn session_dir() -> PathBuf { + let crate_dirs = shallow_find_directories("incr", |_| true); + assert_eq!(crate_dirs.len(), 1); + let sessions = shallow_find_directories(&crate_dirs[0], |_| true); + assert_eq!(sessions.len(), 1, "expected only the newest completed session: {sessions:?}"); + sessions.into_iter().next().unwrap() +}