Skip to content
Latchkey

Rust "cannot find derive macro in this scope" in CI

A #[derive(...)] names a derive macro that is not in scope. The macro crate is not imported, or the feature that exports the derive (for example serde's derive) is not enabled.

What this error means

The compiler reports error: cannot find derive macro Serialize in this scope. It reproduces deterministically for the same Cargo features.

cargo
error: cannot find derive macro `Serialize` in this scope
 --> src/model.rs:3:10
  |
3 | #[derive(Serialize)]
  |          ^^^^^^^^^
  |
  = note: consider importing this derive macro: use serde::Serialize;

Common causes

Derive macro not imported

The derive name is used without a use serde::Serialize;, so name resolution cannot find the macro.

Deriving feature not enabled

serde's derive macros live behind the derive feature; without serde = { version = "1", features = ["derive"] } the macro is absent.

How to fix it

Enable the derive feature and import

  1. Add the derive feature to the dependency in Cargo.toml.
  2. Bring the macro into scope with a use.
  3. Rebuild with the same features CI uses.
Cargo.toml
# Cargo.toml
serde = { version = "1", features = ["derive"] }

Import the derive in the module

src/model.rs
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Model { id: u32 }

How to prevent it

  • Enable derive features explicitly in Cargo.toml.
  • Keep derive imports at the top of the module.
  • Build CI with the full feature set that derives need.

Related guides

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