tauri-apps/tauri · error

failed to serialize scope

Error message

failed to serialize scope

What it means

Panic in CapabilityBuilder::permission_scoped when serde_json::to_value fails to serialize a scope entry passed to the scoped-permission API. Scope entries must be JSON-serializable values (strings or objects); a non-serializable input makes the expect fire and the capability build aborts.

Solutions

  1. Ensure scope entries are plain JSON values: strings, or objects with string keys
  2. Check the permission's documented scope schema (e.g. fs:scope expects {"path": ...} or "**")
  3. Test serialization manually with serde_json::to_value(&entry) before calling permission_scoped
  4. Fix or replace custom Serialize impls that can return errors

Example fix

// before
let mut scope = HashMap::new();
scope.insert(1, "**"); // non-string key -> serialization fails
builder.permission_scoped("fs:allow-read", vec![scope], vec![]);
// after
builder.permission_scoped("fs:allow-read", vec![serde_json::json!({"path": "**"})], vec![]);
Defensive patterns

Strategy: validation

Validate before calling

for entry in &allowed {
    serde_json::to_value(entry).expect("scope entry not JSON-serializable");
}

Try / catch

let value = serde_json::to_value(entry)
    .map_err(|e| format!("failed to serialize scope: {e}"))?;

Prevention

When it happens

Trigger: Calling permission_scoped (or related scoped APIs) with allowed/denied scope entries whose serialized form cannot be represented as JSON — e.g. a type with a non-string key map or a serializer that errors (NaN, custom Serialize impl returning Err).

Common situations: Passing scope objects built from non-JSON-compatible Rust types (e.g. HashMap with non-string keys), typos producing values serde cannot serialize, or using a custom type whose Serialize implementation fails.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


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

Appendix: source

Thrown at crates/tauri/src/ipc/capability_builder.rs:117

  /// Add a new scoped permission to this capability.
  pub fn permission_scoped<T: Serialize>(
    mut self,
    permission: impl Into<String>,
    allowed: Vec<T>,
    denied: Vec<T>,
  ) -> Self {
    let permission = permission.into();
    let identifier = permission
      .clone()
      .try_into()
      .unwrap_or_else(|_| panic!("invalid permission identifier '{permission}'"));

    let allowed_scope = allowed
      .into_iter()
      .map(|a| {
        serde_json::to_value(a)
          .expect("failed to serialize scope")
          .into()
      })
      .collect();
    let denied_scope = denied
      .into_iter()
      .map(|a| {
        serde_json::to_value(a)
          .expect("failed to serialize scope")
          .into()
      })
      .collect();
    let scope = Scopes {
      allow: Some(allowed_scope),
      deny: Some(denied_scope),
    };

    self
      .0

View on GitHub (pinned to 460ec35447)