Skip to content
Latchkey

Cargo "could not compile X (test profile)" in CI

The crate compiled fine for a normal build but failed when compiled with the test profile. Test-only code -- #[cfg(test)] modules, dev-dependencies, #[test] functions -- has a compile error, so cargo test aborts before running anything.

What this error means

The build reports error: could not compile X (lib test) (or "(bin test)") with the underlying compile error above it. cargo build passes while cargo test fails.

cargo
error[E0433]: failed to resolve: use of undeclared crate or module `proptest`
 --> src/lib.rs:40:9
   |
40 |     use proptest::prelude::*;
   |         ^^^^^^^^ use of undeclared crate or module `proptest`

error: could not compile `app` (lib test) due to previous error

Common causes

Test-only code does not compile

A #[cfg(test)] module references a dev-dependency that is missing, or has its own type error that only the test profile triggers.

Missing dev-dependency

A crate used only in tests is not listed under [dev-dependencies], so it resolves in your editor (via a normal dep) but not under the test profile.

How to fix it

Add the missing dev-dependency

Cargo.toml
# Cargo.toml
[dev-dependencies]
proptest = "1"

Compile the test profile locally

cargo test --no-run builds tests without executing them, surfacing the compile error fast.

Terminal
cargo test --no-run --locked

How to prevent it

  • Run cargo test --no-run in CI (or locally) to catch test-profile breakage.
  • List every test-only crate under [dev-dependencies].
  • Keep #[cfg(test)] modules building as part of your normal check loop.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →