Skip to content
Latchkey

Rust "cannot find macro in this scope" in CI

You invoked a macro the compiler cannot find. The macro is not in scope -- its crate or feature is missing, it needs an explicit use, or it lives behind an edition or #[macro_use] you have not set.

What this error means

cargo build fails with error: cannot find macro X in this scope, often with a help: suggesting an import. The macro invocation does not resolve and the build stops.

cargo
error: cannot find macro `json` in this scope
 --> src/main.rs:4:13
  |
4 |     let v = json!({ "ok": true });
  |             ^^^^
  = help: consider importing this macro: use serde_json::json;

Common causes

Macro crate or feature missing

The macro's crate is not a dependency, or it lives behind a feature (some crates gate macros) that is not enabled, so it is undefined.

Macro not imported

In edition 2018+, most macros need an explicit use crate::macro_name;. Without it (or the older #[macro_use]), the macro is out of scope.

How to fix it

Import the macro

Bring it into scope with a use, as the help line suggests.

Rust
use serde_json::json;
// then:
let v = json!({ "ok": true });

Add the crate / feature that provides it

Cargo.toml
[dependencies]
serde_json = "1"

How to prevent it

  • Import macros explicitly with use in edition 2018+.
  • Enable any feature that gates the macro you need.
  • Run cargo check locally to catch missing macros before CI.

Related guides

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