{"record":{"id":"e9c9bc06ff91bef0","repo":"swc-project/swc","slug":"failed-to-serialize-output-e","errorCode":null,"errorMessage":"Failed to serialize output: {e}","messagePattern":"Failed to serialize output: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/swc/src/wasm_analysis.rs","lineNumber":118,"sourceCode":"                    .iter()\n                    .map(|p| {\n                        GLOBALS.set(globals, || {\n                            self.inovke_wasm_analysis_plugin(\n                                plugin_runtime.clone(),\n                                &serialized,\n                                unresolved_mark,\n                                &transform_metadata_context,\n                                p,\n                                comments,\n                            )\n                        })\n                    })\n                    .collect::<Result<Vec<_>>>()?;\n\n                let result = result.into_iter().flatten().collect::<FxHashMap<_, _>>();\n\n                serde_json::to_string(&result)\n                    .map_err(|e| anyhow::anyhow!(\"Failed to serialize output: {e}\"))\n            })\n        })\n    }\n\n    fn inovke_wasm_analysis_plugin(\n        &self,\n        plugin_runtime: Arc<dyn swc_plugin_runner::runtime::Runtime>,\n        serialized: &PluginSerializedBytes,\n        unresolved_mark: Mark,\n        transform_metadata_context: &Arc<TransformPluginMetadataContext>,\n        p: &PluginConfig,\n        comments: &SingleThreadedComments,\n    ) -> Result<FxHashMap<String, String>> {\n        swc_plugin_proxy::COMMENTS.set(\n            &HostCommentsStorage {\n                inner: Some(comments.clone()),\n            },\n            || {","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/swc-project/swc/blob/d7d74346665e36e692aa07e13d4ee3ee692ee35c/crates/swc/src/wasm_analysis.rs#L100-L136","documentation":"Thrown by @swc/wasm_analysis-style multi-plugin analysis when serde_json::to_string fails on the FxHashMap that aggregates the plugin results. The map is already built, so failure means the data itself is not representable as JSON, not that the plugins failed. serde_json rejects non-string map keys at nested levels and NaN/Infinity floating point values.","triggerScenarios":"Calling the wasm analysis entry (process_plugins-style API in swc/src/wasm_analysis.rs) where a plugin's serialized result contains a value JSON cannot encode: NaN/Infinity f32/f64 metrics, or nested maps with non-string keys.","commonSituations":"Analysis plugins computing numeric metrics (sizes, ratios, durations) that divide by zero or take logs of zero and emit NaN, or plugins returning struct-keyed dictionaries; upgrading an analysis plugin that starts emitting float metrics.","solutions":["Fix the plugin so it never emits NaN/Infinity floats (map them to null, 0, or a string) and so all nested map keys serialize as strings","Rebuild the analysis plugin against the same swc_core version as the host to rule out schema drift producing garbage values","Isolate the offending plugin by running plugins one at a time, then inspect the intermediate result for non-JSON-encodable values"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Rust: sanity-check analysis results are JSON-encodable before to_string\nfn is_json_safe(v: &serde_json::Value) -> bool {\n    match v {\n        serde_json::Value::Number(n) => n.as_f64().map_or(true, |f| f.is_finite()),\n        serde_json::Value::Array(a) => a.iter().all(is_json_safe),\n        serde_json::Value::Object(o) => o.iter().all(|(_, v)| is_json_safe(v)),\n        _ => true,\n    }\n}","typeGuard":null,"tryCatchPattern":"// Degrade gracefully when analysis output cannot be serialized\nmatch serde_json::to_string(&result) {\n    Ok(json) => json,\n    Err(e) => {\n        tracing::warn!(\"analysis result not JSON-serializable ({e}); emitting empty result\");\n        \"{}\".to_string()\n    }\n}","preventionTips":["In analysis plugins, clamp or null non-finite floats before returning them","Use string keys for all maps in plugin analysis payloads","Add a unit test in the plugin that serializes its sample output with serde_json"],"tags":["swc","plugins","wasm","serde-json","serialization"],"backgroundTag":null,"analyzedSha":"d7d74346665e36e692aa07e13d4ee3ee692ee35c","analyzedAt":"2026-08-16T12:41:13.160Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}