Skip to content

cargo check --all-targets gives bad error details when tests are disabled but benches aren't #14571

Open
@Nemo157

Description

@Nemo157

Problem

When running cargo check --all-targets on a crate that can't compile tests (e.g. for an embedded target) where you have explicitly disabled tests via test = false, the build failure does not mention that it's trying to build a bench target, making it appear like --all-targets is just ignoring the test = false. cargo metadata also doesn't mention anything about whether a bench target is enabled or not.

Steps

> cargo new foo && cd foo
    Creating binary (application) `foo` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

> cat >> src/main.rs <<<'#[cfg(test)] compile_error!("no tests");'
> cat >> Cargo.toml <<<$'[[bin]]\nname = "foo"\ntest = false'

> cargo metadata --format-version=1 | jq '.packages[] | select(.name == "foo") | .targets'
[
  {
    "kind": [
      "bin"
    ],
    "crate_types": [
      "bin"
    ],
    "name": "foo",
    "src_path": "/tmp/scratch.rust-unwrapped.2024-09-20T14-32.Jn27OQ/foo/src/main.rs",
    "edition": "2021",
    "doc": true,
    "doctest": false,
    "test": false
  }
]

> cargo check --all-targets
    Checking foo v0.1.0 (/tmp/scratch.rust-unwrapped.2024-09-20T14-32.Jn27OQ/foo)
error: no tests
 --> src/main.rs:4:14
  |
4 | #[cfg(test)] compile_error!("no tests");
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: could not compile `foo` (bin "foo" test) due to 1 previous error

> cat >> Cargo.toml <<<'bench = false'

> cargo check --all-targets
    Checking foo v0.1.0 (/tmp/scratch.rust-unwrapped.2024-09-20T14-32.Jn27OQ/foo)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.82.0-nightly (8f40fc5 2024-08-21)
release: 1.82.0-nightly
commit-hash: 8f40fc5
commit-date: 2024-08-21
host: x86_64-unknown-linux-gnu
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w 11 Sep 2023
os: NixOS 24.5.0 [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-build-executionArea: anything dealing with executing the compilerA-cargo-targetsArea: selection and definition of targets (lib, bins, examples, tests, benches)C-bugCategory: bugS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions