tokio-rs/tokio · error · syn::Error

The #[tokio::test] macro requires rt or rt-multi-thread.

Error message

The #[tokio::test] macro requires rt or rt-multi-thread.

What it means

Compile-time error from the `test_fail` proc-macro attribute, which Tokio aliases to `#[tokio::test]` when neither `rt` nor `rt-multi-thread` is enabled (always fails with this message). Mirrors `main_fail` for the test macro.

Source

Thrown at tokio-macros/src/lib.rs:648

#[proc_macro_attribute]
pub fn main_fail(_args: TokenStream, _item: TokenStream) -> TokenStream {
    syn::Error::new(
        proc_macro2::Span::call_site(),
        "The #[tokio::main] macro requires rt or rt-multi-thread.",
    )
    .to_compile_error()
    .into()
}

/// Always fails with the error message below.
/// ```text
/// The #[tokio::test] macro requires rt or rt-multi-thread.
/// ```
#[proc_macro_attribute]
pub fn test_fail(_args: TokenStream, _item: TokenStream) -> TokenStream {
    syn::Error::new(
        proc_macro2::Span::call_site(),
        "The #[tokio::test] macro requires rt or rt-multi-thread.",
    )
    .to_compile_error()
    .into()
}

/// Implementation detail of the `select!` macro. This macro is **not** intended
/// to be used as part of the public API and is permitted to change.
#[proc_macro]
#[doc(hidden)]
pub fn select_priv_declare_output_enum(input: TokenStream) -> TokenStream {
    select::declare_output_enum(input)
}

/// Implementation detail of the `select!` macro. This macro is **not** intended
/// to be used as part of the public API and is permitted to change.
#[proc_macro]
#[doc(hidden)]
pub fn select_priv_clean_pattern(input: TokenStream) -> TokenStream {

View on GitHub (pinned to 625954f365)

Solutions

  1. Add a runtime feature: `tokio = { version = "1", features = ["rt", "macros"] }` (or `rt-multi-thread`).
  2. Check `cargo tree -e features -p tokio` to verify the active features in the test build.
  3. If using `#[tokio::test(flavor = "multi_thread")]`, ensure `rt-multi-thread` is enabled.

Example fix

# before
tokio = { version = "1", features = ["macros"] }

# after
tokio = { version = "1", features = ["rt", "macros"] }
Defensive patterns

Strategy: validation

Validate before calling

// Verify the test build resolves an rt feature:
// `cargo tree -e features -p tokio --target-host | grep -E '\b(rt|rt-multi-thread)\b'`

Prevention

When it happens

Trigger: Annotating `#[tokio::test]` on an async test while `tokio` lacks both `rt` and `rt-multi-thread` features.

Common situations: Dev/test dependency declared as `tokio = { version = "...", features = ["macros"] }` only; `default-features = false` minus `rt`; shared workspace dependency template that omits the runtime feature.

Related errors


AI-assisted analysis of tokio-rs/tokio@625954f365 (2026-08-11). Data as JSON: /api/errors/1c3079bd0c4216a2. Report an issue: GitHub.