swc-project/swc · error

Duplicate __source is found

Error message

Duplicate __source is found

What it means

With runtime=automatic and development=true (the !use_create_element path in jsx_elem_to_expr, jsx/mod.rs:792-808), the transform treats a `__source` attribute specially: the first one becomes the dev-mode source argument passed to jsxDEV. A second `__source` attribute on the same opening element has nowhere to go, so the transform panics with "Duplicate __source is found".

Source

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

                                            HANDLER.with(|handler| {
                                                handler
                                                    .struct_span_err(
                                                        i.span,
                                                        "The value of property 'key' should not \
                                                         be empty",
                                                    )
                                                    .emit();
                                            });
                                        }
                                        continue;
                                    }

                                    if !use_create_element
                                        && *i.sym == *"__source"
                                        && self.development
                                    {
                                        if source_props.is_some() {
                                            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");

View on GitHub (pinned to 5176682b65)

Solutions

  1. Delete manually written `__source` attributes from JSX and let the dev runtime inject them.
  2. Deduplicate the duplicated attributes in the offending element.
  3. If a tool must inject `__source` itself, disable the react transform's development option so the attribute is no longer special-cased.

Example fix

// before
<div __source={{ fileName: "a.js" }} __self={this} __source={{ fileName: "b.js" }} />

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

Strategy: validation

Validate before calling

// Reject duplicate special dev attributes before compiling.
#[derive(Default)]
struct DupSource { bad: Vec<Span> }
impl Visit for DupSource {
    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 == "__source"))).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 `__source` attributes (e.g. `<div __source={a} __source={b} />`) with jsc.transform.react.development=true and the automatic runtime (Next.js dev mode uses exactly this combination).

Common situations: Pasting compiled dev output (which already contains injected `__source`) back into JSX source; a tooling/codemod that injects `__source` for error tracking while the compiler injects its own; ESLint autofix duplicating props.

Related errors


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