tauri-apps/tauri · error · syn::Error

unable to use self as a command function parameter

Error message

unable to use self as a command function parameter

What it means

Compile-time syn::Error from parse_arg in the command macro: a command function declares a self receiver (method syntax) as a parameter. Tauri commands must be free functions taking only typed arguments, so FnArg::Receiver is rejected during macro expansion.

Solutions

  1. Move the command into a free function and pass the needed state explicitly (e.g. via tauri::State<T>)
  2. Remove self and convert method arguments to plain parameters
  3. Keep the method internal and expose a thin free #[tauri::command] that calls it

Example fix

// before
struct Counter { n: i32 }
#[tauri::command]
impl Counter { fn increment(&self) {} }
// after
struct Counter(i32);
#[tauri::command]
fn increment(counter: tauri::State<Counter>) { /* use counter.inner() */ }
Defensive patterns

Strategy: type-guard

Type guard

// Guard: commands must be free functions without receiver params
fn assert_no_self(sig: &syn::Signature) -> syn::Result<()> {
  sig.inputs.iter().try_for_each(|a| match a {
    syn::FnArg::Receiver(r) => Err(syn::Error::new(r.span(), "no self allowed")),
    _ => Ok(()),
  })
}

Prevention

When it happens

Trigger: Marking a method with self, &self, &mut self, or self: Type as a #[tauri::command] — e.g. putting the macro on an impl-block method.

Common situations: Developers trying to expose struct methods directly to the frontend instead of free functions; refactoring an existing method into a command without removing self.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of tauri-apps/tauri@460ec35447 (2026-09-18). Data as JSON: /api/errors/7e1dd28fe2e27b0d. Report an issue: GitHub.

Appendix: source

Thrown at crates/tauri-macros/src/command/wrapper.rs:470

      )
    })
    .collect()
}

/// Transform a [`FnArg`] into a command argument.
fn parse_arg(
  plugin_name: &TokenStream2,
  command: &Ident,
  arg: &FnArg,
  message: &Ident,
  acl: &Ident,
  attributes: &WrapperAttributes,
) -> syn::Result<TokenStream2> {
  // we have no use for self arguments
  let mut arg = match arg {
    FnArg::Typed(arg) => arg.pat.as_ref().clone(),
    FnArg::Receiver(arg) => {
      return Err(syn::Error::new(
        arg.span(),
        "unable to use self as a command function parameter",
      ));
    }
  };

  // we only support patterns that allow us to extract some sort of keyed identifier
  let mut key = match &mut arg {
    Pat::Ident(arg) => arg.ident.unraw().to_string(),
    Pat::Wild(_) => "".into(), // we always convert to camelCase, so "_" will end up empty anyways
    Pat::Struct(s) => super::path_to_command(&mut s.path).ident.to_string(),
    Pat::TupleStruct(s) => super::path_to_command(&mut s.path).ident.to_string(),
    err => {
      return Err(syn::Error::new(
        err.span(),
        "only named, wildcard, struct, and tuple struct arguments allowed",
      ));
    }

View on GitHub (pinned to 460ec35447)