bnjbvr / cargo-machete

Remove unused Rust dependencies with this one weird trick!
MIT License
804 stars 29 forks source link

Respect the `members` field of a workspace Cargo.toml #97

Open nipunn1313 opened 11 months ago

nipunn1313 commented 11 months ago

https://doc.rust-lang.org/cargo/reference/workspaces.html#the-members-and-exclude-fields

For example if the workspace Cargo.toml has

[workspace]
members = ["crates/*", "crates_private/*"]

Then ideally cargo-machete only searches the crates/ and crates_private/ directories. This is particularly useful in monorepos where there are a large number of files not included in the workspace.

Thanks for the tool - it's great!

bnjbvr commented 11 months ago

Thanks for opening an issue. I, for one, quite like that I could run the tool independently of workspaces in a directory that contained multiple Rust projects (unrelated to each other, i.e. no workspaces). And it'd be an API breaking change. So maybe we can introduce a CLI flag that respects the workspace by default.

Out of curiosity, you're saying it's useful in monorepos. Right now cargo-machete will look only for Cargo.toml file, so it only depends on the number of directories; do you see a large difference in runtime, when comparing:

de-vri-es commented 10 months ago

How about: find all Cargo.toml files at or below the current working directory, but to not crawl subdirectories if a Cargo.toml is found. Instead call cargo-metadata to find the workspace members and check those.

On problem I have now is that generated crates in the target directory also get checked. For example, trybuild generates a crate to run compile-fail tests. These may easily end up using less dependencies, but the generated crate copies all dependencies, just in case.

Alternatively, but more difficult I think: find all Cargo.toml files, but ignore target directories of workspaces. However, this still requires something like cargo-metadata to correctly identify the target directory.