swc-project/swc · error
invalid utf8
Error message
invalid utf8
What it means
The lazy SourceMap path duplicates serialize_range_mappings: it assembles the rangeMappings field as base64 characters plus ';' separators into a Vec<u8>, then asserts String::from_utf8 succeeds. All emitted bytes are ASCII by construction, so this expect firing means an internal swc_sourcemap bug or corrupted token state during re-serialization of a lazily decoded map, not bad user input.
Source
Thrown at crates/swc_sourcemap/src/lazy/mod.rs:648
encode_rmi(&mut buf, &rmi_data);
rmi_data.clear();
}
buf.push(b';');
prev_line += 1;
had_rmi = false;
idx_of_first_in_line = idx;
}
}
if empty {
return None;
}
if had_rmi {
encode_rmi(&mut buf, &rmi_data);
}
Some(String::from_utf8(buf).expect("invalid utf8"))
}
fn serialize_mappings(sm: &SourceMap) -> String {
let mut rv = String::new();
// dst == minified == generated
let mut prev_dst_line = 0;
let mut prev_dst_col = 0;
let mut prev_src_line = 0;
let mut prev_src_col = 0;
let mut prev_name_id = 0;
let mut prev_src_id = 0;
for (idx, token) in sm.tokens.iter().enumerate() {
if token.dst_line != prev_dst_line {
prev_dst_col = 0;
while token.dst_line != prev_dst_line {
rv.push(';');
prev_dst_line += 1;View on GitHub (pinned to 5176682b65)
Solutions
- Pin swc_core/swc to the last known-good version in your pipeline.
- Drop range-mapping info before re-encoding (rebuild tokens without is_range) if the field is not required downstream.
- Report upstream with a minimal reproducing map.
Defensive patterns
Strategy: try-catch
Try / catch
// Contain the internal assertion during re-encode of lazily decoded maps.
let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| sm.to_writer(&mut w)));
if out.is_err() {
drop_range_tokens(&mut sm);
sm.to_writer(&mut w)?;
} Prevention
- Pin the swc_core version whose sourcemap output your pipeline verifies in tests.
- Drop range-mapping info before re-encoding if downstream tools do not require it.
- Add encode-decode-encode round-trip tests for maps with rangeMappings.
When it happens
Trigger: Re-encoding (to_writer/to_data) a lazily decoded SourceMap that still carries range mapping tokens, e.g. flattening or rewriting a map produced by a tool that emits rangeMappings.
Common situations: Sourcemap processing pipelines that decode a range-mappings map and re-emit it (chained transforms, bundler concat of maps); swc_sourcemap regressions.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- invalid utf8
- Failed to convert RawValue to Data
- Invalid attempt to iterate non-iterable instance. In order t
- Duplicated methods (${element.key}) can't be decorated.
- Decorators can't be placed on different accessors with for t
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/193a402dc053cf74.
Report an issue: GitHub.