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
- Add the
derivefeature to the dependency in Cargo.toml. - Bring the macro into scope with a
use. - 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
Rust "cannot find macro `X` in this scope" in CIFix Rust "error: cannot find macro `X` in this scope" in CI - a macro not imported, a missing feature/derive,…
Rust "cannot find macro in this scope" in CIFix Rust "error: cannot find macro X in this scope" in CI -- a macro needs importing, a feature enabling, or…
Rust "proc-macro panicked" in CIFix rust "proc-macro derive panicked" / "custom attribute panicked" in CI -- a procedural macro hit a panic w…