Rust "cannot find macro `X` in this scope" in CI
The compiler found no macro by that name in scope. The macro’s crate isn’t imported, a feature that exports it isn’t enabled, or the macro needs an explicit use on the 2018+ edition.
What this error means
cargo build fails with error: cannot find macro X in this scope, often with a help: suggesting an import. Frequently hits json!, format_args!-style helpers, or derive-adjacent macros after a refactor or upgrade.
error: cannot find macro `json` in this scope
--> src/main.rs:6:16
|
6 | let body = json!({ "ok": true });
| ^^^^
= help: consider importing this macro:
serde_json::jsonCommon causes
Macro not imported
On the 2018+ edition, macros are imported like items. json! needs use serde_json::json; (or a path call) rather than the old #[macro_use] extern crate.
Feature or crate providing the macro is absent
The macro lives behind a feature (e.g. a macros feature) or in a crate not declared in Cargo.toml, so it’s never brought into scope.
How to fix it
Import the macro
Bring the macro into scope with a use, as the help line suggests.
use serde_json::json; // or call it by path: serde_json::json!(...)Enable the feature that exports it
When a feature gates the macro, enable it on the dependency.
[dependencies]
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }How to prevent it
- Import macros with
useon modern editions instead of#[macro_use]. - Enable the features that export the macros you call.
- Run
cargo checklocally to catch macro-resolution errors early.