1//! This module manages how the incremental compilation cache is represented in
2//! the file system.
3//!
4//! Incremental compilation caches are managed according to a copy-on-write
5//! strategy: Once a complete, consistent cache version is finalized, it is
6//! never modified. Instead, when a subsequent compilation session is started,
7//! the compiler will allocate a new version of the cache that starts out as
8//! a copy of the previous version. Then only this new copy is modified and it
9//! will not be visible to other processes until it is finalized. This ensures
10//! that multiple compiler processes can be executed concurrently for the same
11//! crate without interfering with each other or blocking each other.
12//!
13//! More concretely this is implemented via the following protocol:
14//!
15//! 1. For a newly started compilation session, the compiler allocates a
16//! new `session` directory within the incremental compilation directory.
17//! This session directory will have a unique name that ends with the suffix
18//! "-working" and that contains a creation timestamp.
19//! 2. Next, the compiler looks for the newest finalized session directory,
20//! that is, a session directory from a previous compilation session that
21//! has been marked as valid and consistent. A session directory is
22//! considered finalized if the "-working" suffix in the directory name has
23//! been replaced by the SVH of the crate.
24//! 3. Once the compiler has found a valid, finalized session directory, it will
25//! hard-link/copy its contents into the new "-working" directory. If all
26//! goes well, it will have its own, private copy of the source directory and
27//! subsequently not have to worry about synchronizing with other compiler
28//! processes.
29//! 4. Now the compiler can do its normal compilation process, which involves
30//! reading and updating its private session directory.
31//! 5. When compilation finishes without errors, the private session directory
32//! will be in a state where it can be used as input for other compilation
33//! sessions. That is, it will contain a dependency graph and cache artifacts
34//! that are consistent with the state of the source code it was compiled
35//! from, with no need to change them ever again. At this point, the compiler
36//! finalizes and "publishes" its private session directory by renaming it
37//! from "s-{timestamp}-{random}-working" to "s-{timestamp}-{SVH}".
38//! 6. At this point the "old" session directory that we copied our data from
39//! at the beginning of the session has become obsolete because we have just
40//! published a more current version. Thus the compiler will delete it.
41//!
42//! ## Garbage Collection
43//!
44//! Naively following the above protocol might lead to old session directories
45//! piling up if a compiler instance crashes for some reason before its able to
46//! remove its private session directory. In order to avoid wasting disk space,
47//! the compiler also does some garbage collection each time it is started in
48//! incremental compilation mode. Specifically, it will scan the incremental
49//! compilation directory for private session directories that are not in use
50//! any more and will delete those. It will also delete any finalized session
51//! directories for a given crate except for the most recent one.
52//!
53//! ## Synchronization
54//!
55//! There is some synchronization needed in order for the compiler to be able to
56//! determine whether a given private session directory is not in use any more.
57//! This is done by creating a lock file for each session directory and
58//! locking it while the directory is still being used. Since file locks have
59//! operating system support, we can rely on the lock being released if the
60//! compiler process dies for some unexpected reason. Thus, when garbage
61//! collecting private session directories, the collecting process can determine
62//! whether the directory is still in use by trying to acquire a lock on the
63//! file. If locking the file fails, the original process must still be alive.
64//! If locking the file succeeds, we know that the owning process is not alive
65//! any more and we can safely delete the directory.
66//! There is still a small time window between the original process creating the
67//! lock file and actually locking it. In order to minimize the chance that
68//! another process tries to acquire the lock in just that instance, only
69//! session directories that are older than a few seconds are considered for
70//! garbage collection.
71//!
72//! Another case that has to be considered is what happens if one process
73//! deletes a finalized session directory that another process is currently
74//! trying to copy from. This case is also handled via the lock file. Before
75//! a process starts copying a finalized session directory, it will acquire a
76//! shared lock on the directory's lock file. Any garbage collecting process,
77//! on the other hand, will acquire an exclusive lock on the lock file.
78//! Thus, if a directory is being collected, any reader process will fail
79//! acquiring the shared lock and will leave the directory alone. Conversely,
80//! if a collecting process can't acquire the exclusive lock because the
81//! directory is currently being read from, it will leave collecting that
82//! directory to another process at a later point in time.
83//! The exact same scheme is also used when reading the metadata hashes file
84//! from an extern crate. When a crate is compiled, the hash values of its
85//! metadata are stored in a file in its session directory. When the
86//! compilation session of another crate imports the first crate's metadata,
87//! it also has to read in the accompanying metadata hashes. It thus will access
88//! the finalized session directory of all crates it links to and while doing
89//! so, it will also place a read lock on that the respective session directory
90//! so that it won't be deleted while the metadata hashes are loaded.
91//!
92//! ## Preconditions
93//!
94//! This system relies on two features being available in the file system in
95//! order to work really well: file locking and hard linking.
96//! If hard linking is not available (like on FAT) the data in the cache
97//! actually has to be copied at the beginning of each session.
98//! If file locking does not work reliably (like on NFS), some of the
99//! synchronization will go haywire.
100//! In both cases we recommend to locate the incremental compilation directory
101//! on a file system that supports these things.
102//! It might be a good idea though to try and detect whether we are on an
103//! unsupported file system and emit a warning in that case. This is not yet
104//! implemented.
105106use std::fsas std_fs;
107use std::io::{self, ErrorKind};
108use std::path::{Path, PathBuf};
109use std::time::{Duration, SystemTime, UNIX_EPOCH};
110111use rand::{RngCore, rng};
112use rustc_data_structures::base_n::{BaseNString, CASE_INSENSITIVE, ToBaseN};
113use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
114use rustc_data_structures::svh::Svh;
115use rustc_data_structures::unord::{UnordMap, UnordSet};
116use rustc_data_structures::{base_n, flock};
117use rustc_fs_util::{LinkOrCopy, link_or_copy, try_canonicalize};
118use rustc_middle::bug;
119use rustc_session::{Session, StableCrateId};
120use rustc_span::Symbol;
121use tracing::debug;
122123use crate::diagnostics;
124125#[cfg(test)]
126mod tests;
127128const LOCK_FILE_EXT: &str = ".lock";
129const DEP_GRAPH_FILENAME: &str = "dep-graph.bin";
130const STAGING_DEP_GRAPH_FILENAME: &str = "dep-graph.part.bin";
131const WORK_PRODUCTS_FILENAME: &str = "work-products.bin";
132const QUERY_CACHE_FILENAME: &str = "query-cache.bin";
133134// We encode integers using the following base, so they are shorter than decimal
135// or hexadecimal numbers (we want short file and directory names). Since these
136// numbers will be used in file names, we choose an encoding that is not
137// case-sensitive (as opposed to base64, for example).
138const INT_ENCODE_BASE: usize = base_n::CASE_INSENSITIVE;
139140/// Returns the path to a session's dependency graph.
141pub(crate) fn dep_graph_path(sess: &Session) -> PathBuf {
142in_incr_comp_dir_sess(sess, DEP_GRAPH_FILENAME)
143}
144145/// Returns the path to a session's staging dependency graph.
146///
147/// On the difference between dep-graph and staging dep-graph,
148/// see `build_dep_graph`.
149pub(crate) fn staging_dep_graph_path(sess: &Session) -> PathBuf {
150in_incr_comp_dir_sess(sess, STAGING_DEP_GRAPH_FILENAME)
151}
152153pub(crate) fn work_products_path(sess: &Session) -> PathBuf {
154in_incr_comp_dir_sess(sess, WORK_PRODUCTS_FILENAME)
155}
156157/// Returns the path to a session's query cache.
158pub(crate) fn query_cache_path(sess: &Session) -> PathBuf {
159in_incr_comp_dir_sess(sess, QUERY_CACHE_FILENAME)
160}
161162/// Locks a given session directory.
163fn lock_file_path(session_dir: &Path) -> PathBuf {
164let crate_dir = session_dir.parent().unwrap();
165166let directory_name = session_dir167 .file_name()
168 .unwrap()
169 .to_str()
170 .expect("malformed session dir name: contains non-Unicode characters");
171172let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect();
173if dash_indices.len() != 3 {
174::rustc_middle::util::bug::bug_fmt(format_args!("Encountered incremental compilation session directory with malformed name: {0}",
session_dir.display()))bug!(
175"Encountered incremental compilation session directory with \
176 malformed name: {}",
177 session_dir.display()
178 )179 }
180181crate_dir.join(&directory_name[0..dash_indices[2]]).with_extension(&LOCK_FILE_EXT[1..])
182}
183184/// Returns the path for a given filename within the incremental compilation directory
185/// in the current session.
186pub fn in_incr_comp_dir_sess(sess: &Session, file_name: &str) -> PathBuf {
187sess.incr_comp_session_dir().join(file_name)
188}
189190/// Allocates the private session directory.
191///
192/// If the result of this function is `Ok`, we have a valid incremental
193/// compilation session directory. A valid session
194/// directory is one that contains a locked lock file. It may or may not contain
195/// a dep-graph and work products from a previous session.
196///
197/// This always attempts to load a dep-graph from the directory.
198/// If loading fails for some reason, we fallback to a disabled `DepGraph`.
199/// See [`rustc_interface::queries::dep_graph`].
200///
201/// If this function returns an error, it may leave behind an invalid session directory.
202/// The garbage collection will take care of it.
203///
204/// [`rustc_interface::queries::dep_graph`]: ../../rustc_interface/struct.Queries.html#structfield.dep_graph
205pub(crate) fn prepare_session_directory(
206 sess: &Session,
207 crate_name: Symbol,
208 stable_crate_id: StableCrateId,
209) {
210if !sess.opts.incremental.is_some() {
::core::panicking::panic("assertion failed: sess.opts.incremental.is_some()")
};assert!(sess.opts.incremental.is_some());
211212let _timer = sess.timer("incr_comp_prepare_session_directory");
213214{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:214",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(214u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("prepare_session_directory")
as &dyn Value))])
});
} else { ; }
};debug!("prepare_session_directory");
215216// {incr-comp-dir}/{crate-name-and-disambiguator}
217let crate_dir = crate_path(sess, crate_name, stable_crate_id);
218{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:218",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(218u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("crate-dir: {0}",
crate_dir.display()) as &dyn Value))])
});
} else { ; }
};debug!("crate-dir: {}", crate_dir.display());
219create_dir(sess, &crate_dir, "crate");
220221// Hack: canonicalize the path *after creating the directory*
222 // because, on windows, long paths can cause problems;
223 // canonicalization inserts this weird prefix that makes windows
224 // tolerate long paths.
225let crate_dir = match try_canonicalize(&crate_dir) {
226Ok(v) => v,
227Err(err) => {
228sess.dcx().emit_fatal(diagnostics::CanonicalizePath { path: crate_dir, err });
229 }
230 };
231232let mut source_directories_already_tried = FxHashSet::default();
233234loop {
235// Generate a session directory of the form:
236 //
237 // {incr-comp-dir}/{crate-name-and-disambiguator}/s-{timestamp}-{random}-working
238let session_dir = generate_session_dir_path(&crate_dir);
239{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:239",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(239u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("session-dir: {0}",
session_dir.display()) as &dyn Value))])
});
} else { ; }
};debug!("session-dir: {}", session_dir.display());
240241// Lock the new session directory. If this fails, return an
242 // error without retrying
243let (directory_lock, lock_file_path) = lock_directory(sess, &session_dir);
244245// Now that we have the lock, we can actually create the session
246 // directory
247create_dir(sess, &session_dir, "session");
248249// Find a suitable source directory to copy from. Ignore those that we
250 // have already tried before.
251let source_directory = find_source_directory(&crate_dir, &source_directories_already_tried);
252253let Some(source_directory) = source_directoryelse {
254// There's nowhere to copy from, we're done
255{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:255",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(255u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("no source directory found. Continuing with empty session directory.")
as &dyn Value))])
});
} else { ; }
};debug!(
256"no source directory found. Continuing with empty session \
257 directory."
258);
259260sess.init_incr_comp_session(session_dir, directory_lock);
261return;
262 };
263264{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:264",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(264u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("attempting to copy data from source: {0}",
source_directory.display()) as &dyn Value))])
});
} else { ; }
};debug!("attempting to copy data from source: {}", source_directory.display());
265266// Try copying over all files from the source directory
267if let Ok(allows_links) = copy_files(sess, &session_dir, &source_directory) {
268{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:268",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(268u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("successfully copied data from: {0}",
source_directory.display()) as &dyn Value))])
});
} else { ; }
};debug!("successfully copied data from: {}", source_directory.display());
269270if !allows_links {
271sess.dcx().emit_warn(diagnostics::HardLinkFailed { path: &session_dir });
272 }
273274sess.init_incr_comp_session(session_dir, directory_lock);
275return;
276 } else {
277{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:277",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(277u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("copying failed - trying next directory")
as &dyn Value))])
});
} else { ; }
};debug!("copying failed - trying next directory");
278279// Something went wrong while trying to copy/link files from the
280 // source directory. Try again with a different one.
281source_directories_already_tried.insert(source_directory);
282283// Try to remove the session directory we just allocated. We don't
284 // know if there's any garbage in it from the failed copy action.
285if let Err(err) = std_fs::remove_dir_all(&session_dir) {
286sess.dcx().emit_warn(diagnostics::DeletePartial { path: &session_dir, err });
287 }
288289delete_session_dir_lock_file(sess, &lock_file_path);
290drop(directory_lock);
291 }
292 }
293}
294295/// This function finalizes and thus 'publishes' the session directory by
296/// renaming it to `s-{timestamp}-{svh}` and releasing the file lock.
297/// If there have been compilation errors, however, this function will just
298/// delete the presumably invalid session directory.
299pub fn finalize_session_directory(sess: &Session, svh: Option<Svh>) {
300if sess.opts.incremental.is_none() {
301return;
302 }
303// The svh is always produced when incr. comp. is enabled.
304let svh = svh.unwrap();
305306let _timer = sess.timer("incr_comp_finalize_session_directory");
307308let incr_comp_session_dir: PathBuf = sess.incr_comp_session_dir().clone();
309310if sess.dcx().has_errors_or_delayed_bugs().is_some() {
311// If there have been any errors during compilation, we don't want to
312 // publish this session directory. Rather, we'll just delete it.
313314{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:314",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(314u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("finalize_session_directory() - invalidating session directory: {0}",
incr_comp_session_dir.display()) as &dyn Value))])
});
} else { ; }
};debug!(
315"finalize_session_directory() - invalidating session directory: {}",
316 incr_comp_session_dir.display()
317 );
318319if let Err(err) = std_fs::remove_dir_all(&*incr_comp_session_dir) {
320sess.dcx().emit_warn(diagnostics::DeleteFull { path: &incr_comp_session_dir, err });
321 }
322323let lock_file_path = lock_file_path(&*incr_comp_session_dir);
324delete_session_dir_lock_file(sess, &lock_file_path);
325sess.mark_incr_comp_session_as_invalid();
326 }
327328{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:328",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(328u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("finalize_session_directory() - session directory: {0}",
incr_comp_session_dir.display()) as &dyn Value))])
});
} else { ; }
};debug!("finalize_session_directory() - session directory: {}", incr_comp_session_dir.display());
329330let mut sub_dir_name = incr_comp_session_dir331 .file_name()
332 .unwrap()
333 .to_str()
334 .expect("malformed session dir name: contains non-Unicode characters")
335 .to_string();
336337// Keep the 's-{timestamp}-{random-number}' prefix, but replace "working" with the SVH of the crate
338sub_dir_name.truncate(sub_dir_name.len() - "working".len());
339// Double-check that we kept this: "s-{timestamp}-{random-number}-"
340if !sub_dir_name.ends_with('-') {
{ ::core::panicking::panic_fmt(format_args!("{0:?}", sub_dir_name)); }
};assert!(sub_dir_name.ends_with('-'), "{:?}", sub_dir_name);
341if !(sub_dir_name.as_bytes().iter().filter(|b| **b == b'-').count() == 3) {
::core::panicking::panic("assertion failed: sub_dir_name.as_bytes().iter().filter(|b| **b == b\'-\').count() == 3")
};assert!(sub_dir_name.as_bytes().iter().filter(|b| **b == b'-').count() == 3);
342343// Append the SVH
344sub_dir_name.push_str(&svh.as_u128().to_base_fixed_len(CASE_INSENSITIVE));
345346// Create the full path
347let new_path = incr_comp_session_dir.parent().unwrap().join(&*sub_dir_name);
348{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:348",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(348u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("finalize_session_directory() - new path: {0}",
new_path.display()) as &dyn Value))])
});
} else { ; }
};debug!("finalize_session_directory() - new path: {}", new_path.display());
349350match rename_path_with_retry(&*incr_comp_session_dir, &new_path, 3) {
351Ok(_) => {
352{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:352",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(352u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("finalize_session_directory() - directory renamed successfully")
as &dyn Value))])
});
} else { ; }
};debug!("finalize_session_directory() - directory renamed successfully");
353354// This unlocks the directory
355sess.finalize_incr_comp_session(new_path);
356 }
357Err(e) => {
358// Warn about the error. However, no need to abort compilation now.
359sess.dcx().emit_note(diagnostics::Finalize { path: &incr_comp_session_dir, err: e });
360361{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:361",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(361u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("finalize_session_directory() - error, marking as invalid")
as &dyn Value))])
});
} else { ; }
};debug!("finalize_session_directory() - error, marking as invalid");
362// Drop the file lock, so we can garage collect
363sess.mark_incr_comp_session_as_invalid();
364 }
365 }
366367let _ = garbage_collect_session_directories(sess);
368}
369370pub(crate) fn delete_all_session_dir_contents(sess: &Session) -> io::Result<()> {
371let sess_dir_iterator = sess.incr_comp_session_dir().read_dir()?;
372for entry in sess_dir_iterator {
373let entry = entry?;
374 safe_remove_file(&entry.path())?
375}
376Ok(())
377}
378379fn copy_files(sess: &Session, target_dir: &Path, source_dir: &Path) -> Result<bool, ()> {
380// We acquire a shared lock on the lock file of the directory, so that
381 // nobody deletes it out from under us while we are reading from it.
382let lock_file_path = lock_file_path(source_dir);
383384// not exclusive
385let Ok(_lock) = flock::Lock::new(
386&lock_file_path,
387false, // don't wait,
388false, // don't create
389false,
390 ) else {
391// Could not acquire the lock, don't try to copy from here
392return Err(());
393 };
394395let Ok(source_dir_iterator) = source_dir.read_dir() else {
396return Err(());
397 };
398399let mut files_linked = 0;
400let mut files_copied = 0;
401402for entry in source_dir_iterator {
403match entry {
404Ok(entry) => {
405let file_name = entry.file_name();
406407let target_file_path = target_dir.join(file_name);
408let source_path = entry.path();
409410{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:410",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(410u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("copying into session dir: {0}",
source_path.display()) as &dyn Value))])
});
} else { ; }
};debug!("copying into session dir: {}", source_path.display());
411match link_or_copy(source_path, target_file_path) {
412Ok(LinkOrCopy::Link) => files_linked += 1,
413Ok(LinkOrCopy::Copy) => files_copied += 1,
414Err(_) => return Err(()),
415 }
416 }
417Err(_) => return Err(()),
418 }
419 }
420421if sess.opts.unstable_opts.incremental_info {
422{
::std::io::_eprint(format_args!("[incremental] session directory: {0} files hard-linked\n",
files_linked));
};eprintln!(
423"[incremental] session directory: \
424 {files_linked} files hard-linked"
425);
426{
::std::io::_eprint(format_args!("[incremental] session directory: {0} files copied\n",
files_copied));
};eprintln!(
427"[incremental] session directory: \
428 {files_copied} files copied"
429);
430 }
431432Ok(files_linked > 0 || files_copied == 0)
433}
434435/// Generates unique directory path of the form:
436/// {crate_dir}/s-{timestamp}-{random-number}-working
437fn generate_session_dir_path(crate_dir: &Path) -> PathBuf {
438let timestamp = timestamp_to_string(SystemTime::now());
439{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:439",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(439u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("generate_session_dir_path: timestamp = {0}",
timestamp) as &dyn Value))])
});
} else { ; }
};debug!("generate_session_dir_path: timestamp = {}", timestamp);
440let random_number = rng().next_u32();
441{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:441",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(441u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("generate_session_dir_path: random_number = {0}",
random_number) as &dyn Value))])
});
} else { ; }
};debug!("generate_session_dir_path: random_number = {}", random_number);
442443// Chop the first 3 characters off the timestamp. Those 3 bytes will be zero for a while.
444let (zeroes, timestamp) = timestamp.split_at(3);
445{
match (&zeroes, &"000") {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};assert_eq!(zeroes, "000");
446let directory_name =
447::alloc::__export::must_use({
::alloc::fmt::format(format_args!("s-{0}-{1}-working", timestamp,
random_number.to_base_fixed_len(CASE_INSENSITIVE)))
})format!("s-{}-{}-working", timestamp, random_number.to_base_fixed_len(CASE_INSENSITIVE));
448{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:448",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(448u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("generate_session_dir_path: directory_name = {0}",
directory_name) as &dyn Value))])
});
} else { ; }
};debug!("generate_session_dir_path: directory_name = {}", directory_name);
449let directory_path = crate_dir.join(directory_name);
450{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:450",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(450u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("generate_session_dir_path: directory_path = {0}",
directory_path.display()) as &dyn Value))])
});
} else { ; }
};debug!("generate_session_dir_path: directory_path = {}", directory_path.display());
451directory_path452}
453454fn create_dir(sess: &Session, path: &Path, dir_tag: &str) {
455match std_fs::create_dir_all(path) {
456Ok(()) => {
457{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:457",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(457u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("{0} directory created successfully",
dir_tag) as &dyn Value))])
});
} else { ; }
};debug!("{} directory created successfully", dir_tag);
458 }
459Err(err) => {
460sess.dcx().emit_fatal(diagnostics::CreateIncrCompDir { tag: dir_tag, path, err })
461 }
462 }
463}
464465/// Allocate the lock-file and lock it.
466fn lock_directory(sess: &Session, session_dir: &Path) -> (flock::Lock, PathBuf) {
467let lock_file_path = lock_file_path(session_dir);
468{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:468",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(468u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("lock_directory() - lock_file: {0}",
lock_file_path.display()) as &dyn Value))])
});
} else { ; }
};debug!("lock_directory() - lock_file: {}", lock_file_path.display());
469470match flock::Lock::new(
471&lock_file_path,
472false, // don't wait
473true, // create the lock file
474true,
475 ) {
476// the lock should be exclusive
477Ok(lock) => (lock, lock_file_path),
478Err(lock_err) => {
479let is_unsupported_lock = flock::Lock::error_unsupported(&lock_err);
480sess.dcx().emit_fatal(diagnostics::CreateLock {
481lock_err,
482session_dir,
483is_unsupported_lock,
484 is_cargo: rustc_session::utils::was_invoked_from_cargo(),
485 });
486 }
487 }
488}
489490fn delete_session_dir_lock_file(sess: &Session, lock_file_path: &Path) {
491if let Err(err) = safe_remove_file(lock_file_path) {
492sess.dcx().emit_warn(diagnostics::DeleteLock { path: lock_file_path, err });
493 }
494}
495496/// Finds the most recent published session directory that is not in the
497/// ignore-list.
498fn find_source_directory(
499 crate_dir: &Path,
500 source_directories_already_tried: &FxHashSet<PathBuf>,
501) -> Option<PathBuf> {
502let iter = crate_dir503 .read_dir()
504 .unwrap() // FIXME
505.filter_map(|e| e.ok().map(|e| e.path()));
506507find_source_directory_in_iter(iter, source_directories_already_tried)
508}
509510fn find_source_directory_in_iter<I>(
511 iter: I,
512 source_directories_already_tried: &FxHashSet<PathBuf>,
513) -> Option<PathBuf>
514where
515I: Iterator<Item = PathBuf>,
516{
517let mut best_candidate = (UNIX_EPOCH, None);
518519for session_dir in iter {
520{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:520",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(520u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("find_source_directory_in_iter - inspecting `{0}`",
session_dir.display()) as &dyn Value))])
});
} else { ; }
};debug!("find_source_directory_in_iter - inspecting `{}`", session_dir.display());
521522let Some(directory_name) = session_dir.file_name().unwrap().to_str() else {
523{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:523",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(523u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("find_source_directory_in_iter - ignoring")
as &dyn Value))])
});
} else { ; }
};debug!("find_source_directory_in_iter - ignoring");
524continue;
525 };
526527if source_directories_already_tried.contains(&session_dir)
528 || !is_session_directory(&directory_name)
529 || !is_finalized(&directory_name)
530 {
531{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:531",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(531u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("find_source_directory_in_iter - ignoring")
as &dyn Value))])
});
} else { ; }
};debug!("find_source_directory_in_iter - ignoring");
532continue;
533 }
534535let timestamp = match extract_timestamp_from_session_dir(&directory_name) {
536Ok(timestamp) => timestamp,
537Err(e) => {
538{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:538",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(538u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("unexpected incr-comp session dir: {0}: {1}",
session_dir.display(), e) as &dyn Value))])
});
} else { ; }
};debug!("unexpected incr-comp session dir: {}: {}", session_dir.display(), e);
539continue;
540 }
541 };
542543if timestamp > best_candidate.0 {
544 best_candidate = (timestamp, Some(session_dir.clone()));
545 }
546 }
547548best_candidate.1
549}
550551fn is_finalized(directory_name: &str) -> bool {
552 !directory_name.ends_with("-working")
553}
554555fn is_session_directory(directory_name: &str) -> bool {
556directory_name.starts_with("s-") && !directory_name.ends_with(LOCK_FILE_EXT)
557}
558559fn is_session_directory_lock_file(file_name: &str) -> bool {
560file_name.starts_with("s-") && file_name.ends_with(LOCK_FILE_EXT)
561}
562563fn extract_timestamp_from_session_dir(directory_name: &str) -> Result<SystemTime, &'static str> {
564if !is_session_directory(directory_name) {
565return Err("not a directory");
566 }
567568let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect();
569if dash_indices.len() != 3 {
570return Err("not three dashes in name");
571 }
572573string_to_timestamp(&directory_name[dash_indices[0] + 1..dash_indices[1]])
574}
575576fn timestamp_to_string(timestamp: SystemTime) -> BaseNString {
577let duration = timestamp.duration_since(UNIX_EPOCH).unwrap();
578let micros: u64 = duration.as_micros().try_into().unwrap();
579micros.to_base_fixed_len(CASE_INSENSITIVE)
580}
581582fn string_to_timestamp(s: &str) -> Result<SystemTime, &'static str> {
583let micros_since_unix_epoch = match u64::from_str_radix(s, INT_ENCODE_BASEas u32) {
584Ok(micros) => micros,
585Err(_) => return Err("timestamp not an int"),
586 };
587588let duration = Duration::from_micros(micros_since_unix_epoch);
589Ok(UNIX_EPOCH + duration)
590}
591592fn crate_path(sess: &Session, crate_name: Symbol, stable_crate_id: StableCrateId) -> PathBuf {
593let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();
594595let crate_name =
596::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1}-{0}",
stable_crate_id.as_u64().to_base_fixed_len(CASE_INSENSITIVE),
crate_name))
})format!("{crate_name}-{}", stable_crate_id.as_u64().to_base_fixed_len(CASE_INSENSITIVE));
597incr_dir.join(crate_name)
598}
599600fn is_old_enough_to_be_collected(timestamp: SystemTime) -> bool {
601timestamp < SystemTime::now() - Duration::from_secs(10)
602}
603604/// Runs garbage collection for the current session.
605pub(crate) fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
606{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:606",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(606u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - begin")
as &dyn Value))])
});
} else { ; }
};debug!("garbage_collect_session_directories() - begin");
607608let session_directory = sess.incr_comp_session_dir();
609{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:609",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(609u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - session directory: {0}",
session_directory.display()) as &dyn Value))])
});
} else { ; }
};debug!(
610"garbage_collect_session_directories() - session directory: {}",
611 session_directory.display()
612 );
613614let crate_directory = session_directory.parent().unwrap();
615{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:615",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(615u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - crate directory: {0}",
crate_directory.display()) as &dyn Value))])
});
} else { ; }
};debug!(
616"garbage_collect_session_directories() - crate directory: {}",
617 crate_directory.display()
618 );
619620// First do a pass over the crate directory, collecting lock files and
621 // session directories
622let mut session_directories = FxIndexSet::default();
623let mut lock_files = UnordSet::default();
624625for dir_entry in crate_directory.read_dir()? {
626let Ok(dir_entry) = dir_entry else {
627// Ignore any errors
628continue;
629 };
630631let entry_name = dir_entry.file_name();
632let Some(entry_name) = entry_name.to_str() else {
633continue;
634 };
635636if is_session_directory_lock_file(&entry_name) {
637 lock_files.insert(entry_name.to_string());
638 } else if is_session_directory(&entry_name) {
639 session_directories.insert(entry_name.to_string());
640 } else {
641// This is something we don't know, leave it alone
642}
643 }
644session_directories.sort();
645646// Now map from lock files to session directories
647let lock_file_to_session_dir: UnordMap<String, Option<String>> = lock_files648 .into_items()
649 .map(|lock_file_name| {
650if !lock_file_name.ends_with(LOCK_FILE_EXT) {
::core::panicking::panic("assertion failed: lock_file_name.ends_with(LOCK_FILE_EXT)")
};assert!(lock_file_name.ends_with(LOCK_FILE_EXT));
651let dir_prefix_end = lock_file_name.len() - LOCK_FILE_EXT.len();
652let session_dir = {
653let dir_prefix = &lock_file_name[0..dir_prefix_end];
654session_directories.iter().find(|dir_name| dir_name.starts_with(dir_prefix))
655 };
656 (lock_file_name, session_dir.map(String::clone))
657 })
658 .into();
659660// Delete all lock files, that don't have an associated directory. They must
661 // be some kind of leftover
662for (lock_file_name, directory_name) in
663lock_file_to_session_dir.items().into_sorted_stable_ord()
664 {
665if directory_name.is_none() {
666let Ok(timestamp) = extract_timestamp_from_session_dir(lock_file_name) else {
667{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:667",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(667u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("found lock-file with malformed timestamp: {0}",
crate_directory.join(&lock_file_name).display()) as
&dyn Value))])
});
} else { ; }
};debug!(
668"found lock-file with malformed timestamp: {}",
669 crate_directory.join(&lock_file_name).display()
670 );
671// Ignore it
672continue;
673 };
674675let lock_file_path = crate_directory.join(&*lock_file_name);
676677if is_old_enough_to_be_collected(timestamp) {
678{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:678",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(678u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - deleting garbage lock file: {0}",
lock_file_path.display()) as &dyn Value))])
});
} else { ; }
};debug!(
679"garbage_collect_session_directories() - deleting \
680 garbage lock file: {}",
681 lock_file_path.display()
682 );
683 delete_session_dir_lock_file(sess, &lock_file_path);
684 } else {
685{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:685",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(685u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - lock file with no session dir not old enough to be collected: {0}",
lock_file_path.display()) as &dyn Value))])
});
} else { ; }
};debug!(
686"garbage_collect_session_directories() - lock file with \
687 no session dir not old enough to be collected: {}",
688 lock_file_path.display()
689 );
690 }
691 }
692 }
693694// Filter out `None` directories
695let lock_file_to_session_dir: UnordMap<String, String> = lock_file_to_session_dir696 .into_items()
697 .filter_map(|(lock_file_name, directory_name)| directory_name.map(|n| (lock_file_name, n)))
698 .into();
699700// Delete all session directories that don't have a lock file.
701for directory_name in session_directories {
702if !lock_file_to_session_dir.items().any(|(_, dir)| *dir == directory_name) {
703let path = crate_directory.join(directory_name);
704if let Err(err) = std_fs::remove_dir_all(&path) {
705 sess.dcx().emit_warn(diagnostics::InvalidGcFailed { path: &path, err });
706 }
707 }
708 }
709710let current_session_directory_name =
711session_directory.file_name().expect("session directory is not `..`");
712713// Now garbage collect the valid session directories.
714let deletion_candidates =
715lock_file_to_session_dir.items().filter_map(|(lock_file_name, directory_name)| {
716{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:716",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(716u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - inspecting: {0}",
directory_name) as &dyn Value))])
});
} else { ; }
};debug!("garbage_collect_session_directories() - inspecting: {}", directory_name);
717718if directory_name.as_str() == current_session_directory_name {
719// Skipping our own directory is, unfortunately, important for correctness.
720 //
721 // To summarize #147821: we will try to lock directories before deciding they can be
722 // garbage collected, but the ability of `flock::Lock` to detect a lock held *by the
723 // same process* varies across file locking APIs. Then, if our own session directory
724 // has become old enough to be eligible for GC, we are beholden to platform-specific
725 // details about detecting the our own lock on the session directory.
726 //
727 // POSIX `fcntl(F_SETLK)`-style file locks are maintained across a process. On
728 // systems where this is the mechanism for `flock::Lock`, there is no way to
729 // discover if an `flock::Lock` has been created in the same process on the same
730 // file. Attempting to set a lock on the lockfile again will succeed, even if the
731 // lock was set by another thread, on another file descriptor. Then we would
732 // garbage collect our own live directory, unable to tell it was locked perhaps by
733 // this same thread.
734 //
735 // It's not clear that `flock::Lock` can be fixed for this in general, and our own
736 // incremental session directory is the only one which this process may own, so skip
737 // it here and avoid the problem. We know it's not garbage anyway: we're using it.
738return None;
739 }
740741let Ok(timestamp) = extract_timestamp_from_session_dir(directory_name) else {
742{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:742",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(742u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("found session-dir with malformed timestamp: {0}",
crate_directory.join(directory_name).display()) as
&dyn Value))])
});
} else { ; }
};debug!(
743"found session-dir with malformed timestamp: {}",
744 crate_directory.join(directory_name).display()
745 );
746// Ignore it
747return None;
748 };
749750if is_finalized(directory_name) {
751let lock_file_path = crate_directory.join(lock_file_name);
752match flock::Lock::new(
753&lock_file_path,
754false, // don't wait
755false, // don't create the lock-file
756true,
757 ) {
758// get an exclusive lock
759Ok(lock) => {
760{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:760",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(760u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - successfully acquired lock")
as &dyn Value))])
});
} else { ; }
};debug!(
761"garbage_collect_session_directories() - \
762 successfully acquired lock"
763);
764{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:764",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(764u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - adding deletion candidate: {0}",
directory_name) as &dyn Value))])
});
} else { ; }
};debug!(
765"garbage_collect_session_directories() - adding \
766 deletion candidate: {}",
767 directory_name
768 );
769770// Note that we are holding on to the lock
771return Some((
772 (timestamp, crate_directory.join(directory_name)),
773Some(lock),
774 ));
775 }
776Err(_) => {
777{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:777",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(777u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - not collecting, still in use")
as &dyn Value))])
});
} else { ; }
};debug!(
778"garbage_collect_session_directories() - \
779 not collecting, still in use"
780);
781 }
782 }
783 } else if is_old_enough_to_be_collected(timestamp) {
784// When cleaning out "-working" session directories, i.e.
785 // session directories that might still be in use by another
786 // compiler instance, we only look a directories that are
787 // at least ten seconds old. This is supposed to reduce the
788 // chance of deleting a directory in the time window where
789 // the process has allocated the directory but has not yet
790 // acquired the file-lock on it.
791792 // Try to acquire the directory lock. If we can't, it
793 // means that the owning process is still alive and we
794 // leave this directory alone.
795let lock_file_path = crate_directory.join(lock_file_name);
796match flock::Lock::new(
797&lock_file_path,
798false, // don't wait
799false, // don't create the lock-file
800true,
801 ) {
802// get an exclusive lock
803Ok(lock) => {
804{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:804",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(804u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - successfully acquired lock")
as &dyn Value))])
});
} else { ; }
};debug!(
805"garbage_collect_session_directories() - \
806 successfully acquired lock"
807);
808809delete_old(sess, &crate_directory.join(directory_name));
810811// Let's make it explicit that the file lock is released at this point,
812 // or rather, that we held on to it until here
813drop(lock);
814 }
815Err(_) => {
816{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:816",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(816u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - not collecting, still in use")
as &dyn Value))])
});
} else { ; }
};debug!(
817"garbage_collect_session_directories() - \
818 not collecting, still in use"
819);
820 }
821 }
822 } else {
823{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:823",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(823u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - not finalized, not old enough")
as &dyn Value))])
});
} else { ; }
};debug!(
824"garbage_collect_session_directories() - not finalized, not \
825 old enough"
826);
827 }
828None829 });
830let deletion_candidates = deletion_candidates.into();
831832// Delete all but the most recent of the candidates
833all_except_most_recent(deletion_candidates).into_items().all(|(path, lock)| {
834{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:834",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(834u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - deleting `{0}`",
path.display()) as &dyn Value))])
});
} else { ; }
};debug!("garbage_collect_session_directories() - deleting `{}`", path.display());
835836if let Err(err) = std_fs::remove_dir_all(&path) {
837sess.dcx().emit_warn(diagnostics::FinalizedGcFailed { path: &path, err });
838 } else {
839delete_session_dir_lock_file(sess, &lock_file_path(&path));
840 }
841842// Let's make it explicit that the file lock is released at this point,
843 // or rather, that we held on to it until here
844drop(lock);
845true
846});
847848Ok(())
849}
850851fn delete_old(sess: &Session, path: &Path) {
852{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/fs.rs:852",
"rustc_incremental::persist::fs", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/fs.rs"),
::tracing_core::__macro_support::Option::Some(852u32),
::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::fs"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("garbage_collect_session_directories() - deleting `{0}`",
path.display()) as &dyn Value))])
});
} else { ; }
};debug!("garbage_collect_session_directories() - deleting `{}`", path.display());
853854if let Err(err) = std_fs::remove_dir_all(path) {
855sess.dcx().emit_warn(diagnostics::SessionGcFailed { path, err });
856 } else {
857delete_session_dir_lock_file(sess, &lock_file_path(path));
858 }
859}
860861fn all_except_most_recent(
862 deletion_candidates: UnordMap<(SystemTime, PathBuf), Option<flock::Lock>>,
863) -> UnordMap<PathBuf, Option<flock::Lock>> {
864let most_recent = deletion_candidates.items().map(|(&(timestamp, _), _)| timestamp).max();
865866if let Some(most_recent) = most_recent {
867deletion_candidates868 .into_items()
869 .filter(|&((timestamp, _), _)| timestamp != most_recent)
870 .map(|((_, path), lock)| (path, lock))
871 .collect()
872 } else {
873UnordMap::default()
874 }
875}
876877fn safe_remove_file(p: &Path) -> io::Result<()> {
878match std_fs::remove_file(p) {
879Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
880 result => result,
881 }
882}
883884// On Windows the compiler would sometimes fail to rename the session directory because
885// the OS thought something was still being accessed in it. So we retry a few times to give
886// the OS time to catch up.
887// See https://github.com/rust-lang/rust/issues/86929.
888fn rename_path_with_retry(from: &Path, to: &Path, mut retries_left: usize) -> std::io::Result<()> {
889loop {
890match std_fs::rename(from, to) {
891Ok(()) => return Ok(()),
892Err(e) => {
893if retries_left > 0 && e.kind() == ErrorKind::PermissionDenied {
894// Try again after a short waiting period.
895std::thread::sleep(Duration::from_millis(50));
896retries_left -= 1;
897 } else {
898return Err(e);
899 }
900 }
901 }
902 }
903}