Skip to content

(Auto)DRY for your Rust dependencies

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

mainmatter/cargo-autoinherit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cargo-autoinherit

A Cargo subcommand to automatically DRY up your Cargo.toml manifests in a workspace.

Note

This project has been created by Mainmatter.
Check out our landing page if you're looking for Rust consulting or training!

The problem

When you have multiple packages in a Cargo workspace, you often end up depending on the same packages in multiple Cargo.toml files.
This duplication can become an issue:

  • When you want to update a dependency, you have to update it in multiple places.
  • When you need to add a new dependency, you first have to check if it's already used in another package of your workspace to keep versions in sync.

This process it's error-prone and tedious.
If you mess it up, you end up with different versions of the same dependency within your workspace. This can lead to hard-to-debug compilation errors or bloat your artifacts with unnecessary copies of the same package.

The solution

cargo-autoinherit is a Cargo subcommand that helps you to keep your dependencies DRY.

It takes advantage of dependency inheritance, a recent feature of Cargo: you can specify dependencies in the root Cargo.toml of your workspace, and all the members of the workspace will inherit them (dependency_name = { workspace = true}).

Converting an existing workspace to use dependency inheritance can be a tedious process—a non-trivial project can have tens of dependencies, and you have to move them all manually from the Cargo.toml of each member to the root Cargo.toml.

cargo-autoinherit automates this process for you.

# From the root of your workspace
cargo autoinherit

It collects all the dependencies in your workspace, determines which ones can be DRYed and moves them to the [workspace.dependencies] section of the root Cargo.toml. It also takes care of updating the members' Cargo.toml files, setting the correct features field for each package.

To exclude workspace members from the autoinherit process, you can either pass their packgage names as an option like so:

cargo autoinherit -e cargo-inherit-test-web

or you can define the exclusion in the workspace metadata:

# Cargo.toml
[workspace]
members = [
    "cli",
    "config",
    "db",
    "web",
    "macros"
]

[workspace.metadata.cargo-autoinherit]
# Skip cargo-autoinherit for these packages
exclude-members = [
    "cargo-autoinherit-test-web" # <= This member will be excluded
]

Installation

You can find prebuilt binaries on the Releases page.
Alternatively, you can build from source:

cargo install --locked cargo-autoinherit

Usage

# From the root of your workspace
cargo autoinherit

Limitations

  • cargo-autoinherit won't auto-inherit dependencies from private registries.
  • cargo-autoinherit will only merge version requirements that are obviously compatible (e.g. ^1.0.0 and ^1.1.5 will be merged to ^1.1.5, but ^1.0.0 and >=1,<2 won't be merged).

About

(Auto)DRY for your Rust dependencies

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages