rustc_incremental/persist/
fs.rs

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.
105
106use std::fs as std_fs;
107use std::io::{self, ErrorKind};
108use std::path::{Path, PathBuf};
109use std::time::{Duration, SystemTime, UNIX_EPOCH};
110
111use rand::{RngCore, thread_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::config::CrateType;
120use rustc_session::output::{collect_crate_types, find_crate_name};
121use rustc_session::{Session, StableCrateId};
122use tracing::debug;
123
124use crate::errors;
125
126#[cfg(test)]
127mod tests;
128
129const LOCK_FILE_EXT: &str = ".lock";
130const DEP_GRAPH_FILENAME: &str = "dep-graph.bin";
131const STAGING_DEP_GRAPH_FILENAME: &str = "dep-graph.part.bin";
132const WORK_PRODUCTS_FILENAME: &str = "work-products.bin";
133const QUERY_CACHE_FILENAME: &str = "query-cache.bin";
134
135// We encode integers using the following base, so they are shorter than decimal
136// or hexadecimal numbers (we want short file and directory names). Since these
137// numbers will be used in file names, we choose an encoding that is not
138// case-sensitive (as opposed to base64, for example).
139const INT_ENCODE_BASE: usize = base_n::CASE_INSENSITIVE;
140
141/// Returns the path to a session's dependency graph.
142pub(crate) fn dep_graph_path(sess: &Session) -> PathBuf {
143    in_incr_comp_dir_sess(sess, DEP_GRAPH_FILENAME)
144}
145
146/// Returns the path to a session's staging dependency graph.
147///
148/// On the difference between dep-graph and staging dep-graph,
149/// see `build_dep_graph`.
150pub(crate) fn staging_dep_graph_path(sess: &Session) -> PathBuf {
151    in_incr_comp_dir_sess(sess, STAGING_DEP_GRAPH_FILENAME)
152}
153
154pub(crate) fn work_products_path(sess: &Session) -> PathBuf {
155    in_incr_comp_dir_sess(sess, WORK_PRODUCTS_FILENAME)
156}
157
158/// Returns the path to a session's query cache.
159pub(crate) fn query_cache_path(sess: &Session) -> PathBuf {
160    in_incr_comp_dir_sess(sess, QUERY_CACHE_FILENAME)
161}
162
163/// Locks a given session directory.
164fn lock_file_path(session_dir: &Path) -> PathBuf {
165    let crate_dir = session_dir.parent().unwrap();
166
167    let directory_name = session_dir
168        .file_name()
169        .unwrap()
170        .to_str()
171        .expect("malformed session dir name: contains non-Unicode characters");
172
173    let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect();
174    if dash_indices.len() != 3 {
175        bug!(
176            "Encountered incremental compilation session directory with \
177              malformed name: {}",
178            session_dir.display()
179        )
180    }
181
182    crate_dir.join(&directory_name[0..dash_indices[2]]).with_extension(&LOCK_FILE_EXT[1..])
183}
184
185/// Returns the path for a given filename within the incremental compilation directory
186/// in the current session.
187pub fn in_incr_comp_dir_sess(sess: &Session, file_name: &str) -> PathBuf {
188    in_incr_comp_dir(&sess.incr_comp_session_dir(), file_name)
189}
190
191/// Returns the path for a given filename within the incremental compilation directory,
192/// not necessarily from the current session.
193///
194/// To ensure the file is part of the current session, use [`in_incr_comp_dir_sess`].
195pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBuf {
196    incr_comp_session_dir.join(file_name)
197}
198
199/// Allocates the private session directory.
200///
201/// If the result of this function is `Ok`, we have a valid incremental
202/// compilation session directory. A valid session
203/// directory is one that contains a locked lock file. It may or may not contain
204/// a dep-graph and work products from a previous session.
205///
206/// This always attempts to load a dep-graph from the directory.
207/// If loading fails for some reason, we fallback to a disabled `DepGraph`.
208/// See [`rustc_interface::queries::dep_graph`].
209///
210/// If this function returns an error, it may leave behind an invalid session directory.
211/// The garbage collection will take care of it.
212///
213/// [`rustc_interface::queries::dep_graph`]: ../../rustc_interface/struct.Queries.html#structfield.dep_graph
214pub(crate) fn prepare_session_directory(sess: &Session) {
215    if sess.opts.incremental.is_none() {
216        return;
217    }
218
219    let _timer = sess.timer("incr_comp_prepare_session_directory");
220
221    debug!("prepare_session_directory");
222
223    // {incr-comp-dir}/{crate-name-and-disambiguator}
224    let crate_dir = crate_path(sess);
225    debug!("crate-dir: {}", crate_dir.display());
226    create_dir(sess, &crate_dir, "crate");
227
228    // Hack: canonicalize the path *after creating the directory*
229    // because, on windows, long paths can cause problems;
230    // canonicalization inserts this weird prefix that makes windows
231    // tolerate long paths.
232    let crate_dir = match try_canonicalize(&crate_dir) {
233        Ok(v) => v,
234        Err(err) => {
235            sess.dcx().emit_fatal(errors::CanonicalizePath { path: crate_dir, err });
236        }
237    };
238
239    let mut source_directories_already_tried = FxHashSet::default();
240
241    loop {
242        // Generate a session directory of the form:
243        //
244        // {incr-comp-dir}/{crate-name-and-disambiguator}/s-{timestamp}-{random}-working
245        let session_dir = generate_session_dir_path(&crate_dir);
246        debug!("session-dir: {}", session_dir.display());
247
248        // Lock the new session directory. If this fails, return an
249        // error without retrying
250        let (directory_lock, lock_file_path) = lock_directory(sess, &session_dir);
251
252        // Now that we have the lock, we can actually create the session
253        // directory
254        create_dir(sess, &session_dir, "session");
255
256        // Find a suitable source directory to copy from. Ignore those that we
257        // have already tried before.
258        let source_directory = find_source_directory(&crate_dir, &source_directories_already_tried);
259
260        let Some(source_directory) = source_directory else {
261            // There's nowhere to copy from, we're done
262            debug!(
263                "no source directory found. Continuing with empty session \
264                    directory."
265            );
266
267            sess.init_incr_comp_session(session_dir, directory_lock);
268            return;
269        };
270
271        debug!("attempting to copy data from source: {}", source_directory.display());
272
273        // Try copying over all files from the source directory
274        if let Ok(allows_links) = copy_files(sess, &session_dir, &source_directory) {
275            debug!("successfully copied data from: {}", source_directory.display());
276
277            if !allows_links {
278                sess.dcx().emit_warn(errors::HardLinkFailed { path: &session_dir });
279            }
280
281            sess.init_incr_comp_session(session_dir, directory_lock);
282            return;
283        } else {
284            debug!("copying failed - trying next directory");
285
286            // Something went wrong while trying to copy/link files from the
287            // source directory. Try again with a different one.
288            source_directories_already_tried.insert(source_directory);
289
290            // Try to remove the session directory we just allocated. We don't
291            // know if there's any garbage in it from the failed copy action.
292            if let Err(err) = safe_remove_dir_all(&session_dir) {
293                sess.dcx().emit_warn(errors::DeletePartial { path: &session_dir, err });
294            }
295
296            delete_session_dir_lock_file(sess, &lock_file_path);
297            drop(directory_lock);
298        }
299    }
300}
301
302/// This function finalizes and thus 'publishes' the session directory by
303/// renaming it to `s-{timestamp}-{svh}` and releasing the file lock.
304/// If there have been compilation errors, however, this function will just
305/// delete the presumably invalid session directory.
306pub fn finalize_session_directory(sess: &Session, svh: Option<Svh>) {
307    if sess.opts.incremental.is_none() {
308        return;
309    }
310    // The svh is always produced when incr. comp. is enabled.
311    let svh = svh.unwrap();
312
313    let _timer = sess.timer("incr_comp_finalize_session_directory");
314
315    let incr_comp_session_dir: PathBuf = sess.incr_comp_session_dir().clone();
316
317    if sess.dcx().has_errors_or_delayed_bugs().is_some() {
318        // If there have been any errors during compilation, we don't want to
319        // publish this session directory. Rather, we'll just delete it.
320
321        debug!(
322            "finalize_session_directory() - invalidating session directory: {}",
323            incr_comp_session_dir.display()
324        );
325
326        if let Err(err) = safe_remove_dir_all(&*incr_comp_session_dir) {
327            sess.dcx().emit_warn(errors::DeleteFull { path: &incr_comp_session_dir, err });
328        }
329
330        let lock_file_path = lock_file_path(&*incr_comp_session_dir);
331        delete_session_dir_lock_file(sess, &lock_file_path);
332        sess.mark_incr_comp_session_as_invalid();
333    }
334
335    debug!("finalize_session_directory() - session directory: {}", incr_comp_session_dir.display());
336
337    let mut sub_dir_name = incr_comp_session_dir
338        .file_name()
339        .unwrap()
340        .to_str()
341        .expect("malformed session dir name: contains non-Unicode characters")
342        .to_string();
343
344    // Keep the 's-{timestamp}-{random-number}' prefix, but replace "working" with the SVH of the crate
345    sub_dir_name.truncate(sub_dir_name.len() - "working".len());
346    // Double-check that we kept this: "s-{timestamp}-{random-number}-"
347    assert!(sub_dir_name.ends_with('-'), "{:?}", sub_dir_name);
348    assert!(sub_dir_name.as_bytes().iter().filter(|b| **b == b'-').count() == 3);
349
350    // Append the SVH
351    sub_dir_name.push_str(&svh.as_u128().to_base_fixed_len(CASE_INSENSITIVE));
352
353    // Create the full path
354    let new_path = incr_comp_session_dir.parent().unwrap().join(&*sub_dir_name);
355    debug!("finalize_session_directory() - new path: {}", new_path.display());
356
357    match rename_path_with_retry(&*incr_comp_session_dir, &new_path, 3) {
358        Ok(_) => {
359            debug!("finalize_session_directory() - directory renamed successfully");
360
361            // This unlocks the directory
362            sess.finalize_incr_comp_session(new_path);
363        }
364        Err(e) => {
365            // Warn about the error. However, no need to abort compilation now.
366            sess.dcx().emit_warn(errors::Finalize { path: &incr_comp_session_dir, err: e });
367
368            debug!("finalize_session_directory() - error, marking as invalid");
369            // Drop the file lock, so we can garage collect
370            sess.mark_incr_comp_session_as_invalid();
371        }
372    }
373
374    let _ = garbage_collect_session_directories(sess);
375}
376
377pub(crate) fn delete_all_session_dir_contents(sess: &Session) -> io::Result<()> {
378    let sess_dir_iterator = sess.incr_comp_session_dir().read_dir()?;
379    for entry in sess_dir_iterator {
380        let entry = entry?;
381        safe_remove_file(&entry.path())?
382    }
383    Ok(())
384}
385
386fn copy_files(sess: &Session, target_dir: &Path, source_dir: &Path) -> Result<bool, ()> {
387    // We acquire a shared lock on the lock file of the directory, so that
388    // nobody deletes it out from under us while we are reading from it.
389    let lock_file_path = lock_file_path(source_dir);
390
391    // not exclusive
392    let Ok(_lock) = flock::Lock::new(
393        &lock_file_path,
394        false, // don't wait,
395        false, // don't create
396        false,
397    ) else {
398        // Could not acquire the lock, don't try to copy from here
399        return Err(());
400    };
401
402    let Ok(source_dir_iterator) = source_dir.read_dir() else {
403        return Err(());
404    };
405
406    let mut files_linked = 0;
407    let mut files_copied = 0;
408
409    for entry in source_dir_iterator {
410        match entry {
411            Ok(entry) => {
412                let file_name = entry.file_name();
413
414                let target_file_path = target_dir.join(file_name);
415                let source_path = entry.path();
416
417                debug!("copying into session dir: {}", source_path.display());
418                match link_or_copy(source_path, target_file_path) {
419                    Ok(LinkOrCopy::Link) => files_linked += 1,
420                    Ok(LinkOrCopy::Copy) => files_copied += 1,
421                    Err(_) => return Err(()),
422                }
423            }
424            Err(_) => return Err(()),
425        }
426    }
427
428    if sess.opts.unstable_opts.incremental_info {
429        eprintln!(
430            "[incremental] session directory: \
431                  {files_linked} files hard-linked"
432        );
433        eprintln!(
434            "[incremental] session directory: \
435                 {files_copied} files copied"
436        );
437    }
438
439    Ok(files_linked > 0 || files_copied == 0)
440}
441
442/// Generates unique directory path of the form:
443/// {crate_dir}/s-{timestamp}-{random-number}-working
444fn generate_session_dir_path(crate_dir: &Path) -> PathBuf {
445    let timestamp = timestamp_to_string(SystemTime::now());
446    debug!("generate_session_dir_path: timestamp = {}", timestamp);
447    let random_number = thread_rng().next_u32();
448    debug!("generate_session_dir_path: random_number = {}", random_number);
449
450    // Chop the first 3 characters off the timestamp. Those 3 bytes will be zero for a while.
451    let (zeroes, timestamp) = timestamp.split_at(3);
452    assert_eq!(zeroes, "000");
453    let directory_name =
454        format!("s-{}-{}-working", timestamp, random_number.to_base_fixed_len(CASE_INSENSITIVE));
455    debug!("generate_session_dir_path: directory_name = {}", directory_name);
456    let directory_path = crate_dir.join(directory_name);
457    debug!("generate_session_dir_path: directory_path = {}", directory_path.display());
458    directory_path
459}
460
461fn create_dir(sess: &Session, path: &Path, dir_tag: &str) {
462    match std_fs::create_dir_all(path) {
463        Ok(()) => {
464            debug!("{} directory created successfully", dir_tag);
465        }
466        Err(err) => sess.dcx().emit_fatal(errors::CreateIncrCompDir { tag: dir_tag, path, err }),
467    }
468}
469
470/// Allocate the lock-file and lock it.
471fn lock_directory(sess: &Session, session_dir: &Path) -> (flock::Lock, PathBuf) {
472    let lock_file_path = lock_file_path(session_dir);
473    debug!("lock_directory() - lock_file: {}", lock_file_path.display());
474
475    match flock::Lock::new(
476        &lock_file_path,
477        false, // don't wait
478        true,  // create the lock file
479        true,
480    ) {
481        // the lock should be exclusive
482        Ok(lock) => (lock, lock_file_path),
483        Err(lock_err) => {
484            let is_unsupported_lock = flock::Lock::error_unsupported(&lock_err);
485            sess.dcx().emit_fatal(errors::CreateLock {
486                lock_err,
487                session_dir,
488                is_unsupported_lock,
489                is_cargo: rustc_session::utils::was_invoked_from_cargo(),
490            });
491        }
492    }
493}
494
495fn delete_session_dir_lock_file(sess: &Session, lock_file_path: &Path) {
496    if let Err(err) = safe_remove_file(lock_file_path) {
497        sess.dcx().emit_warn(errors::DeleteLock { path: lock_file_path, err });
498    }
499}
500
501/// Finds the most recent published session directory that is not in the
502/// ignore-list.
503fn find_source_directory(
504    crate_dir: &Path,
505    source_directories_already_tried: &FxHashSet<PathBuf>,
506) -> Option<PathBuf> {
507    let iter = crate_dir
508        .read_dir()
509        .unwrap() // FIXME
510        .filter_map(|e| e.ok().map(|e| e.path()));
511
512    find_source_directory_in_iter(iter, source_directories_already_tried)
513}
514
515fn find_source_directory_in_iter<I>(
516    iter: I,
517    source_directories_already_tried: &FxHashSet<PathBuf>,
518) -> Option<PathBuf>
519where
520    I: Iterator<Item = PathBuf>,
521{
522    let mut best_candidate = (UNIX_EPOCH, None);
523
524    for session_dir in iter {
525        debug!("find_source_directory_in_iter - inspecting `{}`", session_dir.display());
526
527        let Some(directory_name) = session_dir.file_name().unwrap().to_str() else {
528            debug!("find_source_directory_in_iter - ignoring");
529            continue;
530        };
531
532        if source_directories_already_tried.contains(&session_dir)
533            || !is_session_directory(&directory_name)
534            || !is_finalized(&directory_name)
535        {
536            debug!("find_source_directory_in_iter - ignoring");
537            continue;
538        }
539
540        let timestamp = match extract_timestamp_from_session_dir(&directory_name) {
541            Ok(timestamp) => timestamp,
542            Err(e) => {
543                debug!("unexpected incr-comp session dir: {}: {}", session_dir.display(), e);
544                continue;
545            }
546        };
547
548        if timestamp > best_candidate.0 {
549            best_candidate = (timestamp, Some(session_dir.clone()));
550        }
551    }
552
553    best_candidate.1
554}
555
556fn is_finalized(directory_name: &str) -> bool {
557    !directory_name.ends_with("-working")
558}
559
560fn is_session_directory(directory_name: &str) -> bool {
561    directory_name.starts_with("s-") && !directory_name.ends_with(LOCK_FILE_EXT)
562}
563
564fn is_session_directory_lock_file(file_name: &str) -> bool {
565    file_name.starts_with("s-") && file_name.ends_with(LOCK_FILE_EXT)
566}
567
568fn extract_timestamp_from_session_dir(directory_name: &str) -> Result<SystemTime, &'static str> {
569    if !is_session_directory(directory_name) {
570        return Err("not a directory");
571    }
572
573    let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect();
574    if dash_indices.len() != 3 {
575        return Err("not three dashes in name");
576    }
577
578    string_to_timestamp(&directory_name[dash_indices[0] + 1..dash_indices[1]])
579}
580
581fn timestamp_to_string(timestamp: SystemTime) -> BaseNString {
582    let duration = timestamp.duration_since(UNIX_EPOCH).unwrap();
583    let micros: u64 = duration.as_micros().try_into().unwrap();
584    micros.to_base_fixed_len(CASE_INSENSITIVE)
585}
586
587fn string_to_timestamp(s: &str) -> Result<SystemTime, &'static str> {
588    let micros_since_unix_epoch = match u64::from_str_radix(s, INT_ENCODE_BASE as u32) {
589        Ok(micros) => micros,
590        Err(_) => return Err("timestamp not an int"),
591    };
592
593    let duration = Duration::from_micros(micros_since_unix_epoch);
594    Ok(UNIX_EPOCH + duration)
595}
596
597fn crate_path(sess: &Session) -> PathBuf {
598    let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();
599
600    let crate_name = find_crate_name(sess, &[]);
601    let crate_types = collect_crate_types(sess, &[]);
602    let stable_crate_id = StableCrateId::new(
603        crate_name,
604        crate_types.contains(&CrateType::Executable),
605        sess.opts.cg.metadata.clone(),
606        sess.cfg_version,
607    );
608
609    let crate_name =
610        format!("{crate_name}-{}", stable_crate_id.as_u64().to_base_fixed_len(CASE_INSENSITIVE));
611    incr_dir.join(crate_name)
612}
613
614fn is_old_enough_to_be_collected(timestamp: SystemTime) -> bool {
615    timestamp < SystemTime::now() - Duration::from_secs(10)
616}
617
618/// Runs garbage collection for the current session.
619pub(crate) fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
620    debug!("garbage_collect_session_directories() - begin");
621
622    let session_directory = sess.incr_comp_session_dir();
623    debug!(
624        "garbage_collect_session_directories() - session directory: {}",
625        session_directory.display()
626    );
627
628    let crate_directory = session_directory.parent().unwrap();
629    debug!(
630        "garbage_collect_session_directories() - crate directory: {}",
631        crate_directory.display()
632    );
633
634    // First do a pass over the crate directory, collecting lock files and
635    // session directories
636    let mut session_directories = FxIndexSet::default();
637    let mut lock_files = UnordSet::default();
638
639    for dir_entry in crate_directory.read_dir()? {
640        let Ok(dir_entry) = dir_entry else {
641            // Ignore any errors
642            continue;
643        };
644
645        let entry_name = dir_entry.file_name();
646        let Some(entry_name) = entry_name.to_str() else {
647            continue;
648        };
649
650        if is_session_directory_lock_file(&entry_name) {
651            lock_files.insert(entry_name.to_string());
652        } else if is_session_directory(&entry_name) {
653            session_directories.insert(entry_name.to_string());
654        } else {
655            // This is something we don't know, leave it alone
656        }
657    }
658    session_directories.sort();
659
660    // Now map from lock files to session directories
661    let lock_file_to_session_dir: UnordMap<String, Option<String>> = lock_files
662        .into_items()
663        .map(|lock_file_name| {
664            assert!(lock_file_name.ends_with(LOCK_FILE_EXT));
665            let dir_prefix_end = lock_file_name.len() - LOCK_FILE_EXT.len();
666            let session_dir = {
667                let dir_prefix = &lock_file_name[0..dir_prefix_end];
668                session_directories.iter().find(|dir_name| dir_name.starts_with(dir_prefix))
669            };
670            (lock_file_name, session_dir.map(String::clone))
671        })
672        .into();
673
674    // Delete all lock files, that don't have an associated directory. They must
675    // be some kind of leftover
676    for (lock_file_name, directory_name) in
677        lock_file_to_session_dir.items().into_sorted_stable_ord()
678    {
679        if directory_name.is_none() {
680            let Ok(timestamp) = extract_timestamp_from_session_dir(lock_file_name) else {
681                debug!(
682                    "found lock-file with malformed timestamp: {}",
683                    crate_directory.join(&lock_file_name).display()
684                );
685                // Ignore it
686                continue;
687            };
688
689            let lock_file_path = crate_directory.join(&*lock_file_name);
690
691            if is_old_enough_to_be_collected(timestamp) {
692                debug!(
693                    "garbage_collect_session_directories() - deleting \
694                    garbage lock file: {}",
695                    lock_file_path.display()
696                );
697                delete_session_dir_lock_file(sess, &lock_file_path);
698            } else {
699                debug!(
700                    "garbage_collect_session_directories() - lock file with \
701                    no session dir not old enough to be collected: {}",
702                    lock_file_path.display()
703                );
704            }
705        }
706    }
707
708    // Filter out `None` directories
709    let lock_file_to_session_dir: UnordMap<String, String> = lock_file_to_session_dir
710        .into_items()
711        .filter_map(|(lock_file_name, directory_name)| directory_name.map(|n| (lock_file_name, n)))
712        .into();
713
714    // Delete all session directories that don't have a lock file.
715    for directory_name in session_directories {
716        if !lock_file_to_session_dir.items().any(|(_, dir)| *dir == directory_name) {
717            let path = crate_directory.join(directory_name);
718            if let Err(err) = safe_remove_dir_all(&path) {
719                sess.dcx().emit_warn(errors::InvalidGcFailed { path: &path, err });
720            }
721        }
722    }
723
724    // Now garbage collect the valid session directories.
725    let deletion_candidates =
726        lock_file_to_session_dir.items().filter_map(|(lock_file_name, directory_name)| {
727            debug!("garbage_collect_session_directories() - inspecting: {}", directory_name);
728
729            let Ok(timestamp) = extract_timestamp_from_session_dir(directory_name) else {
730                debug!(
731                    "found session-dir with malformed timestamp: {}",
732                    crate_directory.join(directory_name).display()
733                );
734                // Ignore it
735                return None;
736            };
737
738            if is_finalized(directory_name) {
739                let lock_file_path = crate_directory.join(lock_file_name);
740                match flock::Lock::new(
741                    &lock_file_path,
742                    false, // don't wait
743                    false, // don't create the lock-file
744                    true,
745                ) {
746                    // get an exclusive lock
747                    Ok(lock) => {
748                        debug!(
749                            "garbage_collect_session_directories() - \
750                            successfully acquired lock"
751                        );
752                        debug!(
753                            "garbage_collect_session_directories() - adding \
754                            deletion candidate: {}",
755                            directory_name
756                        );
757
758                        // Note that we are holding on to the lock
759                        return Some((
760                            (timestamp, crate_directory.join(directory_name)),
761                            Some(lock),
762                        ));
763                    }
764                    Err(_) => {
765                        debug!(
766                            "garbage_collect_session_directories() - \
767                            not collecting, still in use"
768                        );
769                    }
770                }
771            } else if is_old_enough_to_be_collected(timestamp) {
772                // When cleaning out "-working" session directories, i.e.
773                // session directories that might still be in use by another
774                // compiler instance, we only look a directories that are
775                // at least ten seconds old. This is supposed to reduce the
776                // chance of deleting a directory in the time window where
777                // the process has allocated the directory but has not yet
778                // acquired the file-lock on it.
779
780                // Try to acquire the directory lock. If we can't, it
781                // means that the owning process is still alive and we
782                // leave this directory alone.
783                let lock_file_path = crate_directory.join(lock_file_name);
784                match flock::Lock::new(
785                    &lock_file_path,
786                    false, // don't wait
787                    false, // don't create the lock-file
788                    true,
789                ) {
790                    // get an exclusive lock
791                    Ok(lock) => {
792                        debug!(
793                            "garbage_collect_session_directories() - \
794                            successfully acquired lock"
795                        );
796
797                        delete_old(sess, &crate_directory.join(directory_name));
798
799                        // Let's make it explicit that the file lock is released at this point,
800                        // or rather, that we held on to it until here
801                        drop(lock);
802                    }
803                    Err(_) => {
804                        debug!(
805                            "garbage_collect_session_directories() - \
806                            not collecting, still in use"
807                        );
808                    }
809                }
810            } else {
811                debug!(
812                    "garbage_collect_session_directories() - not finalized, not \
813                    old enough"
814                );
815            }
816            None
817        });
818    let deletion_candidates = deletion_candidates.into();
819
820    // Delete all but the most recent of the candidates
821    all_except_most_recent(deletion_candidates).into_items().all(|(path, lock)| {
822        debug!("garbage_collect_session_directories() - deleting `{}`", path.display());
823
824        if let Err(err) = safe_remove_dir_all(&path) {
825            sess.dcx().emit_warn(errors::FinalizedGcFailed { path: &path, err });
826        } else {
827            delete_session_dir_lock_file(sess, &lock_file_path(&path));
828        }
829
830        // Let's make it explicit that the file lock is released at this point,
831        // or rather, that we held on to it until here
832        drop(lock);
833        true
834    });
835
836    Ok(())
837}
838
839fn delete_old(sess: &Session, path: &Path) {
840    debug!("garbage_collect_session_directories() - deleting `{}`", path.display());
841
842    if let Err(err) = safe_remove_dir_all(path) {
843        sess.dcx().emit_warn(errors::SessionGcFailed { path, err });
844    } else {
845        delete_session_dir_lock_file(sess, &lock_file_path(path));
846    }
847}
848
849fn all_except_most_recent(
850    deletion_candidates: UnordMap<(SystemTime, PathBuf), Option<flock::Lock>>,
851) -> UnordMap<PathBuf, Option<flock::Lock>> {
852    let most_recent = deletion_candidates.items().map(|(&(timestamp, _), _)| timestamp).max();
853
854    if let Some(most_recent) = most_recent {
855        deletion_candidates
856            .into_items()
857            .filter(|&((timestamp, _), _)| timestamp != most_recent)
858            .map(|((_, path), lock)| (path, lock))
859            .collect()
860    } else {
861        UnordMap::default()
862    }
863}
864
865/// Since paths of artifacts within session directories can get quite long, we
866/// need to support deleting files with very long paths. The regular
867/// WinApi functions only support paths up to 260 characters, however. In order
868/// to circumvent this limitation, we canonicalize the path of the directory
869/// before passing it to std::fs::remove_dir_all(). This will convert the path
870/// into the '\\?\' format, which supports much longer paths.
871fn safe_remove_dir_all(p: &Path) -> io::Result<()> {
872    let canonicalized = match try_canonicalize(p) {
873        Ok(canonicalized) => canonicalized,
874        Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
875        Err(err) => return Err(err),
876    };
877
878    std_fs::remove_dir_all(canonicalized)
879}
880
881fn safe_remove_file(p: &Path) -> io::Result<()> {
882    let canonicalized = match try_canonicalize(p) {
883        Ok(canonicalized) => canonicalized,
884        Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
885        Err(err) => return Err(err),
886    };
887
888    match std_fs::remove_file(canonicalized) {
889        Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
890        result => result,
891    }
892}
893
894// On Windows the compiler would sometimes fail to rename the session directory because
895// the OS thought something was still being accessed in it. So we retry a few times to give
896// the OS time to catch up.
897// See https://github.com/rust-lang/rust/issues/86929.
898fn rename_path_with_retry(from: &Path, to: &Path, mut retries_left: usize) -> std::io::Result<()> {
899    loop {
900        match std_fs::rename(from, to) {
901            Ok(()) => return Ok(()),
902            Err(e) => {
903                if retries_left > 0 && e.kind() == ErrorKind::PermissionDenied {
904                    // Try again after a short waiting period.
905                    std::thread::sleep(Duration::from_millis(50));
906                    retries_left -= 1;
907                } else {
908                    return Err(e);
909                }
910            }
911        }
912    }
913}