swc-project/swc · error

Duplicate __self is found

Error message

Duplicate __self is found

What it means

Symmetric to `__source`: with runtime=automatic and development=true, the transform reserves the first `__self` attribute for the dev-mode self argument (jsx/mod.rs:810-826). A second `__self` attribute on the same opening element cannot be forwarded, so the transform panics with "Duplicate __self is found".

Source

Thrown at crates/swc_ecma_transforms_react/src/jsx/mod.rs:815

                                            panic!("Duplicate __source is found");
                                        }
                                        source_props = attr
                                            .value
                                            .and_then(jsx_attr_value_to_expr)
                                            .map(|expr| expr.as_arg());
                                        assert_ne!(
                                            source_props, None,
                                            "value of property '__source' should not be empty"
                                        );
                                        continue;
                                    }

                                    if !use_create_element
                                        && *i.sym == *"__self"
                                        && self.development
                                    {
                                        if self_props.is_some() {
                                            panic!("Duplicate __self is found");
                                        }
                                        self_props = attr
                                            .value
                                            .and_then(jsx_attr_value_to_expr)
                                            .map(|expr| expr.as_arg());
                                        assert_ne!(
                                            self_props, None,
                                            "value of property '__self' should not be empty"
                                        );
                                        continue;
                                    }

                                    let value = self.process_attr_value(attr.value);

                                    // TODO: Check if `i` is a valid identifier.
                                    let key = if i.sym.contains('-') {
                                        PropName::Str(Str {
                                            span: i.span,

View on GitHub (pinned to 5176682b65)

Solutions

  1. Remove manually written `__self` attributes from JSX and rely on the dev runtime's injection.
  2. Deduplicate the duplicated `__self` attribute in the offending element.
  3. Turn off the react transform's development option if external tooling must manage `__self`.

Example fix

// before
<div __self={this} __self={globalThis} />

// after
<div /> // dev runtime injects __self itself
Defensive patterns

Strategy: validation

Validate before calling

// Reject duplicate __self attributes before compiling.
#[derive(Default)]
struct DupSelf { bad: Vec<Span> }
impl Visit for DupSelf {
    fn visit_jsx_opening_element(&mut self, el: &JSXOpeningElement) {
        let n = el.attrs.iter().filter(|a| matches!(a,
            JSXAttrOrSpread::JSXAttr(a)
                if matches!(&a.name, JSXAttrName::Ident(i) if i.sym == "__self"))).count();
        if n > 1 { self.bad.push(el.span); }
        el.visit_children_with(self);
    }
}

Prevention

When it happens

Trigger: Compiling JSX whose opening tag contains two `__self` attributes (e.g. `<div __self={this} __self={that} />`) with jsc.transform.react.development=true and the automatic runtime.

Common situations: Reusing compiled dev-mode output as JSX source; codemods injecting `__self` for React error tracking on top of the compiler's own injection.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/74bb7159d24b3519. Report an issue: GitHub.