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::errors;
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 {
187in_incr_comp_dir(&sess.incr_comp_session_dir(), file_name)
188}
189190/// Returns the path for a given filename within the incremental compilation directory,
191/// not necessarily from the current session.
192///
193/// To ensure the file is part of the current session, use [`in_incr_comp_dir_sess`].
194pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBuf {
195incr_comp_session_dir.join(file_name)
196}
197198/// Allocates the private session directory.
199///
200/// If the result of this function is `Ok`, we have a valid incremental
201/// compilation session directory. A valid session
202/// directory is one that contains a locked lock file. It may or may not contain
203/// a dep-graph and work products from a previous session.
204///
205/// This always attempts to load a dep-graph from the directory.
206/// If loading fails for some reason, we fallback to a disabled `DepGraph`.
207/// See [`rustc_interface::queries::dep_graph`].
208///
209/// If this function returns an error, it may leave behind an invalid session directory.
210/// The garbage collection will take care of it.
211///
212/// [`rustc_interface::queries::dep_graph`]: ../../rustc_interface/struct.Queries.html#structfield.dep_graph
213pub(crate) fn prepare_session_directory(
214 sess: &Session,
215 crate_name: Symbol,
216 stable_crate_id: StableCrateId,
217) {
218if sess.opts.incremental.is_none() {
219return;
220 }
221222let _timer = sess.timer("incr_comp_prepare_session_directory");
223224{
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:224",
"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(224u32),
::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");
225226// {incr-comp-dir}/{crate-name-and-disambiguator}
227let crate_dir = crate_path(sess, crate_name, stable_crate_id);
228{
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:228",
"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(228u32),
::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());
229create_dir(sess, &crate_dir, "crate");
230231// Hack: canonicalize the path *after creating the directory*
232 // because, on windows, long paths can cause problems;
233 // canonicalization inserts this weird prefix that makes windows
234 // tolerate long paths.
235let crate_dir = match try_canonicalize(&crate_dir) {
236Ok(v) => v,
237Err(err) => {
238sess.dcx().emit_fatal(errors::CanonicalizePath { path: crate_dir, err });
239 }
240 };
241242let mut source_directories_already_tried = FxHashSet::default();
243244loop {
245// Generate a session directory of the form:
246 //
247 // {incr-comp-dir}/{crate-name-and-disambiguator}/s-{timestamp}-{random}-working
248let session_dir = generate_session_dir_path(&crate_dir);
249{
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:249",
"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(249u32),
::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());
250251// Lock the new session directory. If this fails, return an
252 // error without retrying
253let (directory_lock, lock_file_path) = lock_directory(sess, &session_dir);
254255// Now that we have the lock, we can actually create the session
256 // directory
257create_dir(sess, &session_dir, "session");
258259// Find a suitable source directory to copy from. Ignore those that we
260 // have already tried before.
261let source_directory = find_source_directory(&crate_dir, &source_directories_already_tried);
262263let Some(source_directory) = source_directoryelse {
264// There's nowhere to copy from, we're done
265{
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:265",
"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(265u32),
::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!(
266"no source directory found. Continuing with empty session \
267 directory."
268);
269270sess.init_incr_comp_session(session_dir, directory_lock);
271return;
272 };
273274{
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:274",
"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(274u32),
::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());
275276// Try copying over all files from the source directory
277if let Ok(allows_links) = copy_files(sess, &session_dir, &source_directory) {
278{
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:278",
"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(278u32),
::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());
279280if !allows_links {
281sess.dcx().emit_warn(errors::HardLinkFailed { path: &session_dir });
282 }
283284sess.init_incr_comp_session(session_dir, directory_lock);
285return;
286 } else {
287{
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:287",
"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(287u32),
::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");
288289// Something went wrong while trying to copy/link files from the
290 // source directory. Try again with a different one.
291source_directories_already_tried.insert(source_directory);
292293// Try to remove the session directory we just allocated. We don't
294 // know if there's any garbage in it from the failed copy action.
295if let Err(err) = std_fs::remove_dir_all(&session_dir) {
296sess.dcx().emit_warn(errors::DeletePartial { path: &session_dir, err });
297 }
298299delete_session_dir_lock_file(sess, &lock_file_path);
300drop(directory_lock);
301 }
302 }
303}
304305/// This function finalizes and thus 'publishes' the session directory by
306/// renaming it to `s-{timestamp}-{svh}` and releasing the file lock.
307/// If there have been compilation errors, however, this function will just
308/// delete the presumably invalid session directory.
309pub fn finalize_session_directory(sess: &Session, svh: Option<Svh>) {
310if sess.opts.incremental.is_none() {
311return;
312 }
313// The svh is always produced when incr. comp. is enabled.
314let svh = svh.unwrap();
315316let _timer = sess.timer("incr_comp_finalize_session_directory");
317318let incr_comp_session_dir: PathBuf = sess.incr_comp_session_dir().clone();
319320if sess.dcx().has_errors_or_delayed_bugs().is_some() {
321// If there have been any errors during compilation, we don't want to
322 // publish this session directory. Rather, we'll just delete it.
323324{
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:324",
"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(324u32),
::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!(
325"finalize_session_directory() - invalidating session directory: {}",
326 incr_comp_session_dir.display()
327 );
328329if let Err(err) = std_fs::remove_dir_all(&*incr_comp_session_dir) {
330sess.dcx().emit_warn(errors::DeleteFull { path: &incr_comp_session_dir, err });
331 }
332333let lock_file_path = lock_file_path(&*incr_comp_session_dir);
334delete_session_dir_lock_file(sess, &lock_file_path);
335sess.mark_incr_comp_session_as_invalid();
336 }
337338{
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:338",
"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(338u32),
::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());
339340let mut sub_dir_name = incr_comp_session_dir341 .file_name()
342 .unwrap()
343 .to_str()
344 .expect("malformed session dir name: contains non-Unicode characters")
345 .to_string();
346347// Keep the 's-{timestamp}-{random-number}' prefix, but replace "working" with the SVH of the crate
348sub_dir_name.truncate(sub_dir_name.len() - "working".len());
349// Double-check that we kept this: "s-{timestamp}-{random-number}-"
350if !sub_dir_name.ends_with('-') {
{ ::core::panicking::panic_fmt(format_args!("{0:?}", sub_dir_name)); }
};assert!(sub_dir_name.ends_with('-'), "{:?}", sub_dir_name);
351if !(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);
352353// Append the SVH
354sub_dir_name.push_str(&svh.as_u128().to_base_fixed_len(CASE_INSENSITIVE));
355356// Create the full path
357let new_path = incr_comp_session_dir.parent().unwrap().join(&*sub_dir_name);
358{
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:358",
"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(358u32),
::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());
359360match rename_path_with_retry(&*incr_comp_session_dir, &new_path, 3) {
361Ok(_) => {
362{
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:362",
"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(362u32),
::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");
363364// This unlocks the directory
365sess.finalize_incr_comp_session(new_path);
366 }
367Err(e) => {
368// Warn about the error. However, no need to abort compilation now.
369sess.dcx().emit_warn(errors::Finalize { path: &incr_comp_session_dir, err: e });
370371{
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:371",
"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(371u32),
::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");
372// Drop the file lock, so we can garage collect
373sess.mark_incr_comp_session_as_invalid();
374 }
375 }
376377let _ = garbage_collect_session_directories(sess);
378}
379380pub(crate) fn delete_all_session_dir_contents(sess: &Session) -> io::Result<()> {
381let sess_dir_iterator = sess.incr_comp_session_dir().read_dir()?;
382for entry in sess_dir_iterator {
383let entry = entry?;
384 safe_remove_file(&entry.path())?
385}
386Ok(())
387}
388389fn copy_files(sess: &Session, target_dir: &Path, source_dir: &Path) -> Result<bool, ()> {
390// We acquire a shared lock on the lock file of the directory, so that
391 // nobody deletes it out from under us while we are reading from it.
392let lock_file_path = lock_file_path(source_dir);
393394// not exclusive
395let Ok(_lock) = flock::Lock::new(
396&lock_file_path,
397false, // don't wait,
398false, // don't create
399false,
400 ) else {
401// Could not acquire the lock, don't try to copy from here
402return Err(());
403 };
404405let Ok(source_dir_iterator) = source_dir.read_dir() else {
406return Err(());
407 };
408409let mut files_linked = 0;
410let mut files_copied = 0;
411412for entry in source_dir_iterator {
413match entry {
414Ok(entry) => {
415let file_name = entry.file_name();
416417let target_file_path = target_dir.join(file_name);
418let source_path = entry.path();
419420{
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:420",
"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(420u32),
::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());
421match link_or_copy(source_path, target_file_path) {
422Ok(LinkOrCopy::Link) => files_linked += 1,
423Ok(LinkOrCopy::Copy) => files_copied += 1,
424Err(_) => return Err(()),
425 }
426 }
427Err(_) => return Err(()),
428 }
429 }
430431if sess.opts.unstable_opts.incremental_info {
432{
::std::io::_eprint(format_args!("[incremental] session directory: {0} files hard-linked\n",
files_linked));
};eprintln!(
433"[incremental] session directory: \
434 {files_linked} files hard-linked"
435);
436{
::std::io::_eprint(format_args!("[incremental] session directory: {0} files copied\n",
files_copied));
};eprintln!(
437"[incremental] session directory: \
438 {files_copied} files copied"
439);
440 }
441442Ok(files_linked > 0 || files_copied == 0)
443}
444445/// Generates unique directory path of the form:
446/// {crate_dir}/s-{timestamp}-{random-number}-working
447fn generate_session_dir_path(crate_dir: &Path) -> PathBuf {
448let timestamp = timestamp_to_string(SystemTime::now());
449{
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:449",
"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(449u32),
::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);
450let random_number = rng().next_u32();
451{
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:451",
"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(451u32),
::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);
452453// Chop the first 3 characters off the timestamp. Those 3 bytes will be zero for a while.
454let (zeroes, timestamp) = timestamp.split_at(3);
455match (&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");
456let directory_name =
457::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));
458{
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:458",
"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(458u32),
::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);
459let directory_path = crate_dir.join(directory_name);
460{
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:460",
"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(460u32),
::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());
461directory_path462}
463464fn create_dir(sess: &Session, path: &Path, dir_tag: &str) {
465match std_fs::create_dir_all(path) {
466Ok(()) => {
467{
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:467",
"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(467u32),
::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);
468 }
469Err(err) => sess.dcx().emit_fatal(errors::CreateIncrCompDir { tag: dir_tag, path, err }),
470 }
471}
472473/// Allocate the lock-file and lock it.
474fn lock_directory(sess: &Session, session_dir: &Path) -> (flock::Lock, PathBuf) {
475let lock_file_path = lock_file_path(session_dir);
476{
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:476",
"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(476u32),
::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());
477478match flock::Lock::new(
479&lock_file_path,
480false, // don't wait
481true, // create the lock file
482true,
483 ) {
484// the lock should be exclusive
485Ok(lock) => (lock, lock_file_path),
486Err(lock_err) => {
487let is_unsupported_lock = flock::Lock::error_unsupported(&lock_err);
488sess.dcx().emit_fatal(errors::CreateLock {
489lock_err,
490session_dir,
491is_unsupported_lock,
492 is_cargo: rustc_session::utils::was_invoked_from_cargo(),
493 });
494 }
495 }
496}
497498fn delete_session_dir_lock_file(sess: &Session, lock_file_path: &Path) {
499if let Err(err) = safe_remove_file(lock_file_path) {
500sess.dcx().emit_warn(errors::DeleteLock { path: lock_file_path, err });
501 }
502}
503504/// Finds the most recent published session directory that is not in the
505/// ignore-list.
506fn find_source_directory(
507 crate_dir: &Path,
508 source_directories_already_tried: &FxHashSet<PathBuf>,
509) -> Option<PathBuf> {
510let iter = crate_dir511 .read_dir()
512 .unwrap() // FIXME
513.filter_map(|e| e.ok().map(|e| e.path()));
514515find_source_directory_in_iter(iter, source_directories_already_tried)
516}
517518fn find_source_directory_in_iter<I>(
519 iter: I,
520 source_directories_already_tried: &FxHashSet<PathBuf>,
521) -> Option<PathBuf>
522where
523I: Iterator<Item = PathBuf>,
524{
525let mut best_candidate = (UNIX_EPOCH, None);
526527for session_dir in iter {
528{
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:528",
"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(528u32),
::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());
529530let Some(directory_name) = session_dir.file_name().unwrap().to_str() else {
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 };
534535if source_directories_already_tried.contains(&session_dir)
536 || !is_session_directory(&directory_name)
537 || !is_finalized(&directory_name)
538 {
539{
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:539",
"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(539u32),
::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");
540continue;
541 }
542543let timestamp = match extract_timestamp_from_session_dir(&directory_name) {
544Ok(timestamp) => timestamp,
545Err(e) => {
546{
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:546",
"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(546u32),
::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);
547continue;
548 }
549 };
550551if timestamp > best_candidate.0 {
552 best_candidate = (timestamp, Some(session_dir.clone()));
553 }
554 }
555556best_candidate.1
557}
558559fn is_finalized(directory_name: &str) -> bool {
560 !directory_name.ends_with("-working")
561}
562563fn is_session_directory(directory_name: &str) -> bool {
564directory_name.starts_with("s-") && !directory_name.ends_with(LOCK_FILE_EXT)
565}
566567fn is_session_directory_lock_file(file_name: &str) -> bool {
568file_name.starts_with("s-") && file_name.ends_with(LOCK_FILE_EXT)
569}
570571fn extract_timestamp_from_session_dir(directory_name: &str) -> Result<SystemTime, &'static str> {
572if !is_session_directory(directory_name) {
573return Err("not a directory");
574 }
575576let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect();
577if dash_indices.len() != 3 {
578return Err("not three dashes in name");
579 }
580581string_to_timestamp(&directory_name[dash_indices[0] + 1..dash_indices[1]])
582}
583584fn timestamp_to_string(timestamp: SystemTime) -> BaseNString {
585let duration = timestamp.duration_since(UNIX_EPOCH).unwrap();
586let micros: u64 = duration.as_micros().try_into().unwrap();
587micros.to_base_fixed_len(CASE_INSENSITIVE)
588}
589590fn string_to_timestamp(s: &str) -> Result<SystemTime, &'static str> {
591let micros_since_unix_epoch = match u64::from_str_radix(s, INT_ENCODE_BASEas u32) {
592Ok(micros) => micros,
593Err(_) => return Err("timestamp not an int"),
594 };
595596let duration = Duration::from_micros(micros_since_unix_epoch);
597Ok(UNIX_EPOCH + duration)
598}
599600fn crate_path(sess: &Session, crate_name: Symbol, stable_crate_id: StableCrateId) -> PathBuf {
601let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();
602603let crate_name =
604::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));
605incr_dir.join(crate_name)
606}
607608fn is_old_enough_to_be_collected(timestamp: SystemTime) -> bool {
609timestamp < SystemTime::now() - Duration::from_secs(10)
610}
611612/// Runs garbage collection for the current session.
613pub(crate) fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
614{
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:614",
"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(614u32),
::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");
615616let session_directory = sess.incr_comp_session_dir();
617{
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:617",
"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(617u32),
::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!(
618"garbage_collect_session_directories() - session directory: {}",
619 session_directory.display()
620 );
621622let crate_directory = session_directory.parent().unwrap();
623{
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:623",
"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(623u32),
::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!(
624"garbage_collect_session_directories() - crate directory: {}",
625 crate_directory.display()
626 );
627628// First do a pass over the crate directory, collecting lock files and
629 // session directories
630let mut session_directories = FxIndexSet::default();
631let mut lock_files = UnordSet::default();
632633for dir_entry in crate_directory.read_dir()? {
634let Ok(dir_entry) = dir_entry else {
635// Ignore any errors
636continue;
637 };
638639let entry_name = dir_entry.file_name();
640let Some(entry_name) = entry_name.to_str() else {
641continue;
642 };
643644if is_session_directory_lock_file(&entry_name) {
645 lock_files.insert(entry_name.to_string());
646 } else if is_session_directory(&entry_name) {
647 session_directories.insert(entry_name.to_string());
648 } else {
649// This is something we don't know, leave it alone
650}
651 }
652session_directories.sort();
653654// Now map from lock files to session directories
655let lock_file_to_session_dir: UnordMap<String, Option<String>> = lock_files656 .into_items()
657 .map(|lock_file_name| {
658if !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));
659let dir_prefix_end = lock_file_name.len() - LOCK_FILE_EXT.len();
660let session_dir = {
661let dir_prefix = &lock_file_name[0..dir_prefix_end];
662session_directories.iter().find(|dir_name| dir_name.starts_with(dir_prefix))
663 };
664 (lock_file_name, session_dir.map(String::clone))
665 })
666 .into();
667668// Delete all lock files, that don't have an associated directory. They must
669 // be some kind of leftover
670for (lock_file_name, directory_name) in
671lock_file_to_session_dir.items().into_sorted_stable_ord()
672 {
673if directory_name.is_none() {
674let Ok(timestamp) = extract_timestamp_from_session_dir(lock_file_name) else {
675{
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:675",
"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(675u32),
::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!(
676"found lock-file with malformed timestamp: {}",
677 crate_directory.join(&lock_file_name).display()
678 );
679// Ignore it
680continue;
681 };
682683let lock_file_path = crate_directory.join(&*lock_file_name);
684685if is_old_enough_to_be_collected(timestamp) {
686{
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:686",
"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(686u32),
::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!(
687"garbage_collect_session_directories() - deleting \
688 garbage lock file: {}",
689 lock_file_path.display()
690 );
691 delete_session_dir_lock_file(sess, &lock_file_path);
692 } else {
693{
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:693",
"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(693u32),
::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!(
694"garbage_collect_session_directories() - lock file with \
695 no session dir not old enough to be collected: {}",
696 lock_file_path.display()
697 );
698 }
699 }
700 }
701702// Filter out `None` directories
703let lock_file_to_session_dir: UnordMap<String, String> = lock_file_to_session_dir704 .into_items()
705 .filter_map(|(lock_file_name, directory_name)| directory_name.map(|n| (lock_file_name, n)))
706 .into();
707708// Delete all session directories that don't have a lock file.
709for directory_name in session_directories {
710if !lock_file_to_session_dir.items().any(|(_, dir)| *dir == directory_name) {
711let path = crate_directory.join(directory_name);
712if let Err(err) = std_fs::remove_dir_all(&path) {
713 sess.dcx().emit_warn(errors::InvalidGcFailed { path: &path, err });
714 }
715 }
716 }
717718let current_session_directory_name =
719session_directory.file_name().expect("session directory is not `..`");
720721// Now garbage collect the valid session directories.
722let deletion_candidates =
723lock_file_to_session_dir.items().filter_map(|(lock_file_name, directory_name)| {
724{
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:724",
"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(724u32),
::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);
725726if directory_name.as_str() == current_session_directory_name {
727// Skipping our own directory is, unfortunately, important for correctness.
728 //
729 // To summarize #147821: we will try to lock directories before deciding they can be
730 // garbage collected, but the ability of `flock::Lock` to detect a lock held *by the
731 // same process* varies across file locking APIs. Then, if our own session directory
732 // has become old enough to be eligible for GC, we are beholden to platform-specific
733 // details about detecting the our own lock on the session directory.
734 //
735 // POSIX `fcntl(F_SETLK)`-style file locks are maintained across a process. On
736 // systems where this is the mechanism for `flock::Lock`, there is no way to
737 // discover if an `flock::Lock` has been created in the same process on the same
738 // file. Attempting to set a lock on the lockfile again will succeed, even if the
739 // lock was set by another thread, on another file descriptor. Then we would
740 // garbage collect our own live directory, unable to tell it was locked perhaps by
741 // this same thread.
742 //
743 // It's not clear that `flock::Lock` can be fixed for this in general, and our own
744 // incremental session directory is the only one which this process may own, so skip
745 // it here and avoid the problem. We know it's not garbage anyway: we're using it.
746return None;
747 }
748749let Ok(timestamp) = extract_timestamp_from_session_dir(directory_name) else {
750{
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:750",
"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(750u32),
::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!(
751"found session-dir with malformed timestamp: {}",
752 crate_directory.join(directory_name).display()
753 );
754// Ignore it
755return None;
756 };
757758if is_finalized(directory_name) {
759let lock_file_path = crate_directory.join(lock_file_name);
760match flock::Lock::new(
761&lock_file_path,
762false, // don't wait
763false, // don't create the lock-file
764true,
765 ) {
766// get an exclusive lock
767Ok(lock) => {
768{
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:768",
"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(768u32),
::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!(
769"garbage_collect_session_directories() - \
770 successfully acquired lock"
771);
772{
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:772",
"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(772u32),
::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!(
773"garbage_collect_session_directories() - adding \
774 deletion candidate: {}",
775 directory_name
776 );
777778// Note that we are holding on to the lock
779return Some((
780 (timestamp, crate_directory.join(directory_name)),
781Some(lock),
782 ));
783 }
784Err(_) => {
785{
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:785",
"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(785u32),
::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!(
786"garbage_collect_session_directories() - \
787 not collecting, still in use"
788);
789 }
790 }
791 } else if is_old_enough_to_be_collected(timestamp) {
792// When cleaning out "-working" session directories, i.e.
793 // session directories that might still be in use by another
794 // compiler instance, we only look a directories that are
795 // at least ten seconds old. This is supposed to reduce the
796 // chance of deleting a directory in the time window where
797 // the process has allocated the directory but has not yet
798 // acquired the file-lock on it.
799800 // Try to acquire the directory lock. If we can't, it
801 // means that the owning process is still alive and we
802 // leave this directory alone.
803let lock_file_path = crate_directory.join(lock_file_name);
804match flock::Lock::new(
805&lock_file_path,
806false, // don't wait
807false, // don't create the lock-file
808true,
809 ) {
810// get an exclusive lock
811Ok(lock) => {
812{
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:812",
"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(812u32),
::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!(
813"garbage_collect_session_directories() - \
814 successfully acquired lock"
815);
816817delete_old(sess, &crate_directory.join(directory_name));
818819// Let's make it explicit that the file lock is released at this point,
820 // or rather, that we held on to it until here
821drop(lock);
822 }
823Err(_) => {
824{
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:824",
"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(824u32),
::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!(
825"garbage_collect_session_directories() - \
826 not collecting, still in use"
827);
828 }
829 }
830 } else {
831{
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:831",
"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(831u32),
::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!(
832"garbage_collect_session_directories() - not finalized, not \
833 old enough"
834);
835 }
836None837 });
838let deletion_candidates = deletion_candidates.into();
839840// Delete all but the most recent of the candidates
841all_except_most_recent(deletion_candidates).into_items().all(|(path, lock)| {
842{
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:842",
"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(842u32),
::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());
843844if let Err(err) = std_fs::remove_dir_all(&path) {
845sess.dcx().emit_warn(errors::FinalizedGcFailed { path: &path, err });
846 } else {
847delete_session_dir_lock_file(sess, &lock_file_path(&path));
848 }
849850// Let's make it explicit that the file lock is released at this point,
851 // or rather, that we held on to it until here
852drop(lock);
853true
854});
855856Ok(())
857}
858859fn delete_old(sess: &Session, path: &Path) {
860{
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:860",
"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(860u32),
::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());
861862if let Err(err) = std_fs::remove_dir_all(path) {
863sess.dcx().emit_warn(errors::SessionGcFailed { path, err });
864 } else {
865delete_session_dir_lock_file(sess, &lock_file_path(path));
866 }
867}
868869fn all_except_most_recent(
870 deletion_candidates: UnordMap<(SystemTime, PathBuf), Option<flock::Lock>>,
871) -> UnordMap<PathBuf, Option<flock::Lock>> {
872let most_recent = deletion_candidates.items().map(|(&(timestamp, _), _)| timestamp).max();
873874if let Some(most_recent) = most_recent {
875deletion_candidates876 .into_items()
877 .filter(|&((timestamp, _), _)| timestamp != most_recent)
878 .map(|((_, path), lock)| (path, lock))
879 .collect()
880 } else {
881UnordMap::default()
882 }
883}
884885fn safe_remove_file(p: &Path) -> io::Result<()> {
886match std_fs::remove_file(p) {
887Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
888 result => result,
889 }
890}
891892// On Windows the compiler would sometimes fail to rename the session directory because
893// the OS thought something was still being accessed in it. So we retry a few times to give
894// the OS time to catch up.
895// See https://github.com/rust-lang/rust/issues/86929.
896fn rename_path_with_retry(from: &Path, to: &Path, mut retries_left: usize) -> std::io::Result<()> {
897loop {
898match std_fs::rename(from, to) {
899Ok(()) => return Ok(()),
900Err(e) => {
901if retries_left > 0 && e.kind() == ErrorKind::PermissionDenied {
902// Try again after a short waiting period.
903std::thread::sleep(Duration::from_millis(50));
904retries_left -= 1;
905 } else {
906return Err(e);
907 }
908 }
909 }
910 }
911}