holmgr / cargo-sweep

A cargo subcommand for cleaning up unused build files generated by Cargo
MIT License
693 stars 31 forks source link

Support shared target directory #71

Open inodentry opened 1 year ago

inodentry commented 1 year ago

I (and many other people) configue cargo to use a single target dir shared between all projects, by putting something like this in ~/.cargo/config:

[build]
target-dir = "/Users/iyes/.cargo/target"

That way all projects put their build artifacts in the same place, allowing dependencies to be reused, etc. shortening clean build times.

However, currently there is no nice way to remove only outdated build artifacts from it (say, when updating a nightly toolchain), without outright deleting everything. cargo sweep could be the perfect tool for this. However, it is currently unsupported, as the tool expects to be run on project dirs.

jyn514 commented 1 year ago

This is an interesting problem. sweep does support cleaning that global target dir, but only if you're already in a project:

(bash@pop-os) ~/rust-community/cargo-sweep [18:42:07, ci]
; CARGO_TARGET_DIR=$CARGO_HOME/target cargo sweep -i
[INFO] Using all installed toolchains: ["stable-x86_64-unknown-linux-gnu", "beta-2022-05-20-x86_64-unknown-linux-gnu", ...]
[INFO] Cleaned 0.00 bytes
; cd /tmp
; CARGO_TARGET_DIR=$CARGO_HOME/target cargo sweep -i
[ERROR] Failed to clean "/tmp" as it is not a cargo project.

The problem is that sweep is detecting the target directory with the Rust equivalent of cargo metadata | jq .target_directory, and Cargo of course only works when there's a Cargo.toml.

There are two possible ways I think we could solve this:

@inodentry can you talk more about why you want to sweep the global directory when you're not in a cargo project? Is this working for you in a project, and you just want the convenience of running it from /home or wherever?