Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

cargo-hack

> 测试质量
Open source

Cargo subcommand to provide various options useful for testing and continuous integration.

852 stars0 likes1 views
WebsiteGitHub

About

Cargo subcommand to provide various options useful for testing and continuous integration.

cargo-hack

Cargo subcommand to provide various options useful for testing and continuous integration.

  • Usage
    • --each-feature
    • --feature-powerset
    • Options for adjusting the behavior of --each-feature and --feature-powerset
      • --optional-deps
      • --exclude-features, --skip
    • --rust-version
    • --version-range
    • Improvement of the behavior of existing cargo flags
  • Installation
  • Related Projects
  • License

Usage

Complete list of options (click to show)

…

cargo-hack is basically wrapper of cargo that propagates subcommand and most of the passed flags to cargo, but provides additional flags and changes the behavior of some existing flags.

--each-feature

Perform for each feature which includes default features and --no-default-features of the package.

This is useful to check that each feature is working properly. (When used for this purpose, it is recommended to use with --no-dev-deps to avoid cargo#4866.)

bash
cargo hack check --each-feature --no-dev-deps

See also Options for adjusting the behavior of --each-feature and --feature-powerset section.

--feature-powerset

Perform for the feature powerset which includes --no-default-features and default features of the package.

This is useful to check that every combination of features is working properly. (When used for this purpose, it is recommended to use with --no-dev-deps to avoid cargo#4866.)

bash
cargo hack check --feature-powerset --no-dev-deps

cargo-hack deduplicate any fully equivalent feature combinations based on how the cargo features work. Therefore, it may be more efficient than checking all feature combinations in other ways.

[!TIP] When using this flag results in a very large number of feature combinations, consider using --depth option.

See also Options for adjusting the behavior of --each-feature and --feature-powerset section.

Options for adjusting the behavior of --each-feature and --feature-powerset

The following flags can be used with --each-feature and --feature-powerset.

--optional-deps

Use optional dependencies as features.

This flag treats all option dependencies as features by default. To treat only specific dependencies as features, pass a space or comma separated list.

bash
cargo hack check --feature-powerset --optional-deps deps1,deps2

--exclude-features, --skip

Space or comma separated list of features to exclude.

bash
cargo hack check --feature-powerset --exclude-features feature1,feature2
cargo hack check --feature-powerset --skip feature1,feature2

--depth

Specify a max number of simultaneous feature flags of --feature-powerset.

If the number is set to 1, --feature-powerset is equivalent to --each-feature.

--group-features

Space or comma separated list of features to group.

This treats the specified features as if it were a single feature.

To specify multiple groups, use this option multiple times: --group-features a,b --group-features c,d

--rust-version

Perform commands on the Rust version of package.rust-version field in Cargo.toml

--version-range

Perform commands on a specified (inclusive) range of Rust versions.

bash
$ cargo hack check --version-range 1.46..=1.47
info: running `rustup run 1.46 cargo check` on cargo-hack (1/2)
...
info: running `rustup run 1.47 cargo check` on cargo-hack (2/2)
...

(We use rustup run cargo instead of cargo + to work around a rustup bug.)

This might be useful for catching issues like [termcolor#35], regex#685, [rust-clippy#6324].

If the upper bound of the range is omitted, the latest stable compiler is used as the upper bound.

If the lower bound of the range is omitted, the value of the rust-version field in Cargo.toml is used as the lower bound.

You can specify the version interval by using --version-step.

--no-dev-deps

Perform without dev-dependencies.

This is a workaround for an issue that dev-dependencies leaking into normal build (cargo#4866).

Also, this can be used as a workaround for an issue that cargo does not allow publishing a package with cyclic dev-dependencies. (cargo#4242)

bash
cargo hack publish --no-dev-deps --dry-run --allow-dirty

[!NOTE] Currently, using --no-dev-deps flag removes dev-dependencies from real manifest while cargo-hack is running and restores it when finished. Any changes you made to those files during running will not be preserved. See cargo#4242 for why this is necessary. Also, this behavior may change in the future on some subcommands. See also #15.

--remove-dev-deps

Equivalent to --no-dev-deps except for does not restore the original Cargo.toml after execution.

This is useful to know what Cargo.toml that cargo-hack is actually using with --no-dev-deps.

This flag also works without subcommands.

--ignore-private

Skip to perform on publish = false crates.

--no-private

Perform without publish = false crates. This is similar to --ignore-private, but is more powerful because this also prevents private crates from affecting lockfile and metadata.

[!NOTE] --no-private flag modifies Cargo.toml while cargo-hack is running and restores it when finished. Any changes you made to those files during running will not be preserved.

--ignore-unknown-features

Skip passing --features to cargo if that feature does not exist.

--clean-per-run

Remove artifacts for that package before running the command.

This also works as a workaround for rust-clippy#4612.

Improvement of the behavior of existing cargo flags

cargo-hack changes the behavior of the following existing flags.

--features, --no-default-features

Unlike cargo (cargo#3620, cargo#4106, cargo#4463, cargo#4753, cargo#5015, cargo#5364, cargo#6195), it can also be applied to sub-crates.

--all, --workspace

Perform command for all packages in the workspace.

Unlike cargo, it does not compile all members at once.

For example, running cargo hack check --all in a workspace with members foo and bar behaves almost the same as the following script:

bash
# If you use cargo-hack, you don't need to maintain this list manually.
members=("foo" "bar")

for member in "${members[@]}"; do
    cargo check --manifest-path "${member}/Cargo.toml"
done

Workspace members will be performed according to the order of the 'packages' fields of cargo metadata.

Installation

From source

bash
cargo +stable install cargo-hack --locked

Currently, installing cargo-hack requires rustc 1.85+.

cargo-hack is usually runnable with Cargo versions older than the Rust version required for installation (e.g., cargo +1.31 hack check). Currently, to run cargo-hack requires Cargo 1.26+.

From prebuilt binaries

You can download prebuilt binaries from the Release page. Prebuilt binaries are available for Linux (x86_64 gnu/musl, aarch64 gnu/musl, powerpc64le gnu/musl, riscv64gc gnu/musl, and s390x gnu, musl binaries are static executable), macOS (x86_64, aarch64, and universal), Windows (x86_64 and aarch64, static executable), FreeBSD (x86_64), and illumos (x86_64). All releases are immutable and releases since 0.6.44 publish artifact attestations.

Example of script to install from the Release page with verifications (click to show)

…

gh release -R .. verify-asset should output messages like the following (//`` contains the actual SHA-256 digest of the downloaded file / the actual SHA-1 digest of tag / the actual version number):

Calculated digest for cargo-hack.tar.gz: sha256:
Resolved tag v to sha1:
Loaded attestation from GitHub API
✓ Verification succeeded! cargo-hack.tar.gz is present in release v

gh attestation verify should output messages like the following (`` contains the actual SHA-256 digest of the downloaded file):

…

Example of script to install from the Release page without verification (click to show)

bash
# Get host target.
host=$(rustc -vV | grep '^host:' | cut -d' ' -f2)
# Download binary and install to $CARGO_HOME/bin (or $HOME/.cargo/bin if CARGO_HOME is unset).
curl --proto '=https' --tlsv1.2 -fsSL "https://github.com/taiki-e/cargo-hack/releases/latest/download/cargo-hack-$host.tar.gz" \
  | tar xzf - -C "${CARGO_HOME:-"$HOME/.cargo"}"/bin

On GitHub Actions

You can use taiki-e/install-action to install prebuilt binaries on Linux, macOS, and Windows. This makes the installation faster and may avoid the impact of problems caused by upstream changes.

yaml
- uses: taiki-e/install-action@cargo-hack

Via Homebrew

You can install cargo-hack from homebrew-core (x86_64/AArch64 macOS, x86_64/AArch64 Linux):

bash
brew install cargo-hack

Alternatively, you can also install from the Homebrew tap maintained by us (x86_64/AArch64 macOS, x86_64/AArch64 Linux):

bash
brew install taiki-e/tap/cargo-hack

Via Scoop (Windows)

You can install cargo-hack from the Scoop bucket maintained by us:

bash
scoop bucket add taiki-e https://github.com/taiki-e/scoop-bucket
scoop install cargo-hack

Via cargo-binstall

You can install cargo-hack using cargo-binstall:

bash
cargo binstall cargo-hack

Via pacman (Arch Linux)

You can install cargo-hack from the extra repository:

bash
pacman -S cargo-hack

Via ports (FreeBSD)

You can install cargo-hack from the official ports:

bash
pkg install cargo-hack

Via other package managers

Related Projects

  • cargo-llvm-cov: Cargo subcommand to easily use LLVM source-based code coverage.
  • cargo-minimal-versions: Cargo subcommand for proper use of -Z minimal-versions.
  • cargo-config2: Library to load and resolve Cargo configuration.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rustcargoclirusttesting

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category测试质量
PricingOpen source

> Related tools

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.