swc-project/swc · error · anyhow::Error
failed to serialize program: {}
Error message
failed to serialize program: {} What it means
Emitted by the build_minify_sync wasm macro after a successful minification when program.serialize(...) (the compatibility serializer that converts the minified AST/result into a JsValue) fails. Parsing and minifying succeeded; only the boundary conversion to a JS value went wrong, which is an internal failure of the binding, not of the input.
Source
Thrown at crates/binding_macros/src/wasm.rs:123
$crate::wasm::try_with_handler_globals(
c.cm.clone(),
$opt,
|handler| {
c.run(|| {
let opts = if opts.is_null() || opts.is_undefined() {
Default::default()
} else {
$crate::wasm::serde_wasm_bindgen::from_value(opts)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to parse options: {}", e))?
};
let fm = c.cm.new_source_file($crate::wasm::FileName::Anon.into(), String::from(s));
let program = $crate::wasm::anyhow::Context::context(c.minify(fm, handler, &opts, Default::default()), "failed to minify file")?;
program
.serialize($crate::wasm::compat_serializer().as_ref())
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize program: {}", e))
})
},
)
.map_err(|e| $crate::wasm::convert_err(e, None))
}
};
}
/// Currently this relies on existence of minify_sync.
#[macro_export]
macro_rules! build_minify {
($(#[$m:meta])*) => {
build_minify!($(#[$m])*, Default::default());
};
($(#[$m:meta])*, $opt: expr) => {
$(#[$m])*
pub fn minify(s: $crate::wasm::js_sys::JsString, opts: $crate::wasm::JsValue) -> $crate::wasm::js_sys::Promise {
// TODO: This'll be properly scheduled once wasm have standard backed threadView on GitHub (pinned to d7d7434666)
Solutions
- Retry with smaller input or fewer output features (no source maps) to rule out memory limits
- Upgrade the wasm minify binding - serializer mismatches are fixed in tandem with struct changes
- Report to swc with the input and options if it reproduces on latest; include the {e} detail
- As a workaround use the napi (@swc/core native) binding which does not cross this serializer path
Defensive patterns
Strategy: try-catch
Try / catch
try {
const out = minifySync(src, opts);
} catch (e) {
if (String(e).startsWith('failed to serialize program')) {
// internal serializer failure: retry smaller / report upstream
report(e, { size: src.length, opts });
throw e;
}
throw e;
} Prevention
- Keep inputs and outputs well under the wasm memory ceiling (split large bundles)
- Use coherent package versions - never mix a wasm binary from one release with JS glue from another
When it happens
Trigger: Extremely rare: serializer bugs in the wasm binding, or wasm memory exhaustion while materializing the result object for very large outputs; occasionally observed across version transitions when the serializer format changed.
Common situations: Minifying unusually large bundles near wasm memory limits; after upgrading the wasm binding where serializer and struct got out of sync (should never happen on a coherent build).
Related errors
- failed to serialize json: {}
- failed to serialize transform result: {}
- failed to handle: {s}
- failed to parse options: {}
- Failed to serialize output: {e}
AI-assisted analysis of swc-project/swc@d7d7434666 (2026-08-16).
Data as JSON: /api/errors/a6ee117e410b879f.
Report an issue: GitHub.