bnjbvr / cargo-machete

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

Doesn't work with workspace inheritance of package.edition #45

Closed Veetaha closed 1 year ago

Veetaha commented 2 years ago

When running cargo-machete with workspace inheritance the CLI outputs errors. This happens when the package.edition key is used in the workspace-level manifest and inherited in the package-level manifest. Inheriting other keys doesn't seem to break cargo machete.

Reproduction

You may use the reproduction git repository:

git clone git@github.com:Veetaha/cargo-machete-workspace-inheritance-bug-repro.git
cd cargo-machete-workspace-inheritance-bug-repro
cargo machete
cargo machete --with-metadata

You'll see that both cargo machete invocations fail

~/dev/cargo-machete-workspace-inheritance-bug-repro (master) $ cargo machete 
Analyzing dependencies of crates in this directory...
error when handling /home/veetaha/dev/cargo-machete-workspace-inheritance-bug-repro/foo/Cargo.toml: value from workspace hasn't been set
cargo-machete didn't find any unused dependencies in /home/veetaha/dev/cargo-machete-workspace-inheritance-bug-repro. Good job!
Done!
~/dev/cargo-machete-workspace-inheritance-bug-repro (master) $ cargo machete --with-metadata
Analyzing dependencies of crates in this directory...
error when handling /home/veetaha/dev/cargo-machete-workspace-inheritance-bug-repro/foo/Cargo.toml: value from workspace hasn't been set
cargo-machete didn't find any unused dependencies in /home/veetaha/dev/cargo-machete-workspace-inheritance-bug-repro. Good job!
Done!

Or you can create the cargo workspace manually the following way.

Workspace-level Cargo.toml manifest:

workspace.members = ["foo"]
workspace.package.edition = "2021"

Package-level Cargo.toml manifest:

[package]
name = "foo"
version = "0.1.0"
edition = { workspace = true }

Meta

CLI version is 0.4.0. Suprisingly, there is no --version command in the CLI.

bnjbvr commented 2 years ago

Yep, it's a bug in the cargo_toml library, unfortunately, as proves this test case adapted from yours: https://github.com/bnjbvr/cargo-toml-workspace-inheritance-bug-repro

running cargo run from the top-level directory will show the same error. I'll open an issue on cargo_toml's issue tracker.

Thanks for reporting!

bnjbvr commented 2 years ago

CLI version is 0.4.0. Suprisingly, there is no --version command in the CLI.

Feel free to implement this! I've pushed as far as I could adding a library to parse arguments, maybe the time has come :)

Veetaha commented 1 year ago

Looks like the 0.5.0 version fixes this. Confirmed by testing in my repo. Thanks!