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

failed to serialize json: {}

Error message

failed to serialize json: {}

What it means

Emitted by the build_print_sync wasm macro after printing succeeded, when serde_wasm_bindgen::to_value fails to convert the printed output (code + optional source map object) into a JsValue. Printing itself worked; only the final JS-value conversion failed - an internal, very rare binding failure.

Source

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

                    .map_err(|e| $crate::wasm::anyhow::anyhow!("failed to deserialize program: {}", e))?;
                  let s = $crate::wasm::anyhow::Context::context(c
                    .print(
                        &program,
                        PrintArgs {
                          inline_sources_content: true,
                          source_map: opts.source_maps
                              .clone()
                              .unwrap_or($crate::wasm::SourceMapsConfig::Bool(false)),
                          emit_source_map_columns: opts.config.emit_source_map_columns.into_bool(),
                          codegen_config: swc_core::ecma::codegen::Config::default()
                              .with_target(opts.codegen_target().unwrap_or($crate::wasm::EsVersion::Es2020))
                              .with_minify(opts.config.minify.into()),
                          ..Default::default()
                        },
                    ),"failed to print code")?;

                    serde_wasm_bindgen::to_value(&s)
                    .map_err(|e| anyhow::anyhow!("failed to serialize json: {}", e))
              })
          },
      )
      .map_err(|e| $crate::wasm::convert_err(e, None))
    }
  };
}

/// Currently this relies on existence of print_sync.
#[macro_export]
macro_rules! build_print {
  ($(#[$m:meta])*) => {
    build_print!($(#[$m])*, Default::default());
  };
  ($(#[$m:meta])*, $opt: expr) => {

      $(#[$m])*
      pub fn print(s: $crate::wasm::JsValue, opts: $crate::wasm::JsValue) -> $crate::wasm::js_sys::Promise {

View on GitHub (pinned to d7d7434666)

Solutions

  1. Disable source maps (sourceMaps: false) or print smaller inputs to rule out memory pressure
  2. Upgrade the wasm binding; to_value failures around version bumps are fixed upstream
  3. Report with the {e} detail and a repro if current versions still fail
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const out = printSync(program, opts);
} catch (e) {
  if (String(e).startsWith('failed to serialize json')) {
    reportInternal(e, { programSize }); // include {e} detail in the report
  }
  throw e;
}

Prevention

When it happens

Trigger: Serializer limitations on the output struct for specific binding versions; wasm memory exhaustion while building the result JsValue for very large outputs with inline source maps.

Common situations: Very large printed bundles in constrained wasm environments; version-transition bugs in the wasm print binding.

Related errors


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