{"record":{"id":"7e1dd28fe2e27b0d","repo":"tauri-apps/tauri","slug":"unable-to-use-self-as-a-command-function-parameter","errorCode":null,"errorMessage":"unable to use self as a command function parameter","messagePattern":"unable to use self as a command function parameter","errorType":"exception","errorClass":"syn::Error","httpStatus":null,"severity":"error","filePath":"crates/tauri-macros/src/command/wrapper.rs","lineNumber":470,"sourceCode":"      )\n    })\n    .collect()\n}\n\n/// Transform a [`FnArg`] into a command argument.\nfn parse_arg(\n  plugin_name: &TokenStream2,\n  command: &Ident,\n  arg: &FnArg,\n  message: &Ident,\n  acl: &Ident,\n  attributes: &WrapperAttributes,\n) -> syn::Result<TokenStream2> {\n  // we have no use for self arguments\n  let mut arg = match arg {\n    FnArg::Typed(arg) => arg.pat.as_ref().clone(),\n    FnArg::Receiver(arg) => {\n      return Err(syn::Error::new(\n        arg.span(),\n        \"unable to use self as a command function parameter\",\n      ));\n    }\n  };\n\n  // we only support patterns that allow us to extract some sort of keyed identifier\n  let mut key = match &mut arg {\n    Pat::Ident(arg) => arg.ident.unraw().to_string(),\n    Pat::Wild(_) => \"\".into(), // we always convert to camelCase, so \"_\" will end up empty anyways\n    Pat::Struct(s) => super::path_to_command(&mut s.path).ident.to_string(),\n    Pat::TupleStruct(s) => super::path_to_command(&mut s.path).ident.to_string(),\n    err => {\n      return Err(syn::Error::new(\n        err.span(),\n        \"only named, wildcard, struct, and tuple struct arguments allowed\",\n      ));\n    }","sourceCodeStart":452,"sourceCodeEnd":488,"githubUrl":"https://github.com/tauri-apps/tauri/blob/460ec35447493200d64290dd7f015d5a91d0fd58/crates/tauri-macros/src/command/wrapper.rs#L452-L488","documentation":"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.","triggerScenarios":"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.","commonSituations":"Developers trying to expose struct methods directly to the frontend instead of free functions; refactoring an existing method into a command without removing self.","solutions":["Move the command into a free function and pass the needed state explicitly (e.g. via tauri::State<T>)","Remove self and convert method arguments to plain parameters","Keep the method internal and expose a thin free #[tauri::command] that calls it"],"exampleFix":"// before\nstruct Counter { n: i32 }\n#[tauri::command]\nimpl Counter { fn increment(&self) {} }\n// after\nstruct Counter(i32);\n#[tauri::command]\nfn increment(counter: tauri::State<Counter>) { /* use counter.inner() */ }","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Guard: commands must be free functions without receiver params\nfn assert_no_self(sig: &syn::Signature) -> syn::Result<()> {\n  sig.inputs.iter().try_for_each(|a| match a {\n    syn::FnArg::Receiver(r) => Err(syn::Error::new(r.span(), \"no self allowed\")),\n    _ => Ok(()),\n  })\n}","tryCatchPattern":null,"preventionTips":["Never put #[tauri::command] on impl-block methods","Use tauri::State<T>, AppHandle or Window for state access instead of self","Expose thin free functions that delegate to methods"],"tags":["proc-macro","tauri","compile-time","self"],"backgroundTag":"invalid-argument-value","analyzedSha":"460ec35447493200d64290dd7f015d5a91d0fd58","analyzedAt":"2026-09-18T23:55:51.277Z","contentChangedAt":"2026-09-18T23:55:51.277Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}