Open jdno opened 8 months ago
In https://github.com/rust-lang/simpleinfra/pull/389#discussion_r1513232655, it was pointed out that the developer guide for rustc
suggests creating a build-rust-analyzer
directory. We might want to clean that up in the future, too.
Hi, I would like to tackle this issue, as a start in contributing to Rust-lang. I followed to this page from the eurorust implRoom issue. But want to mention that I will not to be able to attend eurorust in person or virtually : (
I have a few questions / ideas that may be of use here regardless:
build
dir, but target
of rust crates usually have a CACHEDIR.TAG
created by cargo. The presence of which would be a sign that the directory is indeed safe to clean out.cargo clean
in place of deleting the target
directory of crates directly?
This will take care of cachedir checking, and future changes to target directory. Also possibly relevant cleanup step: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#gc
I have a few questions / ideas that may be of use here regardless:
- Could we use the CACHEDIR.TAG to make the detection of the cache dirs more robust? I am not knowledgeable about the x.py
build
dir, buttarget
of rust crates usually have aCACHEDIR.TAG
created by cargo. The presence of which would be a sign that the directory is indeed safe to clean out.
This depends on the particular checkout.
build
directory which is not trivially managed by cargo and they can include other artifacts. For rust-lang/rust checkouts, it's actually easier to run ./x clean
which should handle trimming unneeded build artifacts. Note that via git worktree, some rustc/std/rustdoc devs have multiple checkouts.
build
, but you can customize it.build/
, that can be awkward.build/
directory does not have a CACHEDIR.TAG
you can look at.build-rust-analyzer
is a common alternative build dir name for r-a, but the contributor can feed r-a with any arbitrary directory name, i.e. it doesn't need to be exactly named build-rust-analyzer
.
- Should we use
cargo clean
in place of deleting thetarget
directory of crates directly? This will take care of cachedir checking, and future changes to target directory.
If you can implement a heuristic to detect if a directory is a cargo-managed project (this probably should not fire on cargo dirs inside rust-lang/rust checkouts because bootstrap has additional logic on top, as you don't want to delete bootstrap's self build via cargo, then you have to build bootstrap to run ./x clean
), then yes I think that can make sense. You might want to use nightly cargo to run cargo clean
though because nightly cargo might support newer variations of build structure compared to stable cargo versions.
Additional space-saving techniques:
cargo-bisect-rustc
and retaining some specific toolchains. But please do not delete stable-<target-triple>
or beta-<target-triple>
or nightly-<target-triple>
toolchains.As a first step, I'd like to rewrite the script that we have now and make sure that the new implementation is well-tested and well-documented. It'll be much easier afterwards to add more features or improve its heuristics. 🙂
In #389, we created a cronjob that runs a Bash script to periodically clean up unused cache directories on the dev-desktops. While the script itself is relatively simple, it uses some advanced arguments for
find
and a lot of loops to go through all directories inside/home
. Given that the script performs destructive actions on the machine, we should make it more robust and easier to maintain by rewriting it in Rust.We specifically want to get these benefits from rewriting it:
Prior Art
I've considered rewriting the script as part of #389, but the priority wasn't high enough to dedicate the required time to the task. But the code snippet below can be used as inspiration or a starting point.
ansible/roles/dev-desktop/files/free_disk_space/src/main.rs
```rust use std::path::{Path, PathBuf}; use anyhow::Error; use clap::Parser; use walkdir::WalkDir; /// Clean up unused projects /// /// This CLI finds all projects that users have checked out on the dev-desktops and deletes /// temporary files if the project has not been modified in a certain number of days. /// /// Specifically, the CLI will look for checkouts of `rust-lang/rust` and delete the `build` /// directory. And it will find unused crates and delete the `target` directory. #[derive(Parser)] struct Cli { /// The root directory to search for projects #[arg(short, long = "root-directory", default_value = "/home")] root_directory: PathBuf, /// The maximum age of a project in days /// /// The CLI will only clean projects that have not been updated in the last `max-age` days. #[arg(short, long = "max-age", default_value_t = 60)] max_age: u32, /// Perform a dry run without cleaning any files /// /// When this flag is set, the CLI will only print the directories that would be removed. #[arg(long = "dry-run", default_value_t = false)] dry_run: bool, } fn main() -> Result<(), Error> { let cli = Cli::parse(); let all_artifact_directories = find_artifact_directories(&cli.root_directory)?; Ok(()) } fn find_artifact_directories(root_directory: &Path) -> ResultResources