Skip to content

cargo bench --bench x builds unrelated build targets #15702

@bdonlan

Description

@bdonlan

Problem

Cargo allows you to specify a single build target for cargo run and cargo bench as in:

  • cargo run --bin foo
  • cargo bench --bench bar

For cargo run, only the specified target is built. However, for cargo bench, all benchmark and bin targets are built before executing the benchmark. For applications with heavyweight bins, this can slow down the build quite a bit.

Steps

  1. Create a project with a benchmark target
  2. Run cargo bench --bench benchmark-target-name
  3. Observe that other bins are built as well

Possible Solution(s)

No response

Notes

No response

Version

% cargo version --verbose
cargo 1.87.0 (99624be96 2025-05-06)
release: 1.87.0
commit-hash: 99624be96e9d213b0e9b1e36451271f24e4a41d8
commit-date: 2025-05-06
host: x86_64-unknown-linux-gnu
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.12.1-DEV (sys:0.4.80+curl-8.12.1 vendored ssl:OpenSSL/3.4.1)
ssl: OpenSSL 3.4.1 11 Feb 2025
os: Amazon Linux AMI 2.0.0 [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-cargo-targetsArea: selection and definition of targets (lib, bins, examples, tests, benches)A-cliArea: Command-line interface, option parsing, etc.C-bugCategory: bugCommand-benchS-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions