actix/actix-web · error · syn::Error

argument to scope macro is not a string literal, expected: #

Error message

argument to scope macro is not a string literal, expected: #[scope("/prefix")]

What it means

The scope prefix must be a string literal. At actix-web-codegen/src/scope.rs:25-30 the args are parsed as `syn::LitStr`; if parsing fails the macro reports that the argument is not a string literal and shows the expected form.

Source

Thrown at actix-web-codegen/src/scope.rs:26

};

pub fn with_scope(args: TokenStream, input: TokenStream) -> TokenStream {
    match with_scope_inner(args, input.clone()) {
        Ok(stream) => stream,
        Err(err) => input_and_compile_error(input, err),
    }
}

fn with_scope_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
    if args.is_empty() {
        return Err(syn::Error::new(
            Span::call_site(),
            "missing arguments for scope macro, expected: #[scope(\"/prefix\")]",
        ));
    }

    let scope_prefix = syn::parse::<syn::LitStr>(args.clone()).map_err(|err| {
        syn::Error::new(
            err.span(),
            "argument to scope macro is not a string literal, expected: #[scope(\"/prefix\")]",
        )
    })?;

    let scope_prefix_value = scope_prefix.value();

    if scope_prefix_value.ends_with('/') {
        // trailing slashes cause non-obvious problems
        // it's better to point them out to developers rather than

        return Err(syn::Error::new(
            scope_prefix.span(),
            "scopes should not have trailing slashes; see https://docs.rs/actix-web/4/actix_web/struct.Scope.html#avoid-trailing-slashes",
        ));
    }

    let mut module = syn::parse::<syn::ItemMod>(input).map_err(|err| {

View on GitHub (pinned to 937960ca67)

Solutions

  1. Use a quoted string literal: `#[scope("/api")]`.
  2. Inline any constant value as a literal since proc-macro args must be literals.

Example fix

// before
const PREFIX: &str = "/api";
#[scope(PREFIX)]
mod api { ... }

// after
#[scope("/api")]
mod api { ... }
Defensive patterns

Strategy: validation

Validate before calling

// Compile-time only. The scope argument must be a string literal "...".
// Constants/idents are not allowed; inline the literal.

Prevention

When it happens

Trigger: `#[scope(api)]` (ident), `#[scope(PREFIX)]` (const), or `#[scope("/api".to_string())]` (expression) instead of a quoted string.

Common situations: Trying to use a constant or computed prefix, or forgetting quotes.

Related errors


AI-assisted analysis of actix/actix-web@937960ca67 (2026-08-06). Data as JSON: /data/errors/6b963d3448fd9c75.json. Report an issue: GitHub.