swc-project/swc · error · anyhow::Error

failed to serialize transform result: {}

Error message

failed to serialize transform result: {}

What it means

Emitted by the build_transform_sync wasm macro (the entry point behind @swc/core's wasm transform) when the successful TransformOutput of process_js cannot be serialized to a JsValue via the compatibility serializer. Transform ran to completion; only the final boundary conversion to JS failed, which indicates an internal binding defect or wasm memory exhaustion - not bad input.

Source

Thrown at crates/binding_macros/src/wasm.rs:404

                          let comments = $crate::wasm::SingleThreadedComments::default();
                          $crate::wasm::anyhow::Context::context(
                            c.process_js_with_custom_pass(
                              fm,
                              None,
                              handler,
                              &opts,
                              comments,
                              $before_pass,
                              $after_pass,
                          ), "failed to process js file"
                          )?
                      }
                      Err(v) => unsafe { c.process_js(handler, $crate::wasm::serde_wasm_bindgen::from_value(v).expect(""), &opts)? },
                  };

                  out
                    .serialize($crate::wasm::compat_serializer().as_ref())
                    .map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize transform result: {}", e))
              })
            },
        )
        .map_err(|e| $crate::wasm::convert_err(e, Some(error_format)))
    }
  };
}

/// Currently this relies on existence of transform_sync.
#[macro_export]
macro_rules! build_transform {
  ($(#[$m:meta])*) => {
    build_transform!($(#[$m])*, Default::default());
  };
  ($(#[$m:meta])*, $opt: expr) => {
      $(#[$m])*
      pub fn transform(
        s: $crate::wasm::JsValue,

View on GitHub (pinned to d7d7434666)

Solutions

  1. Clean-reinstall the wasm binding (remove node_modules/.cache and lockfile-cached artifacts) to eliminate mixed-version binaries
  2. Transform without source maps or with smaller inputs to rule out wasm memory limits
  3. Upgrade to the latest coherent release; these serializer failures are treated as release-blocking bugs
  4. Report upstream with options and a size-reduced repro, including the {e} detail
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const out = transformSync(src, opts);
} catch (e) {
  if (String(e).startsWith('failed to serialize transform result')) {
    // transform succeeded; only result serialization failed: retry once clean, then report
    reportInternal(e, { swcVersion, opts });
  }
  throw e;
}

Prevention

When it happens

Trigger: Serializer/struct mismatches in a mis-versioned wasm build; memory pressure building the result object for very large outputs (code + sourcemap + metadata); regressions observed around binding version transitions.

Common situations: Large production bundles transformed in browser/wasm toolchains; after upgrading @swc/core where the wasm binary and JS glue got mixed across versions (partial install, cached artifacts).

Related errors


AI-assisted analysis of swc-project/swc@d7d7434666 (2026-08-16). Data as JSON: /api/errors/1aacf1d4b2f59f84. Report an issue: GitHub.