swc-project/swc · error

invalid utf8

Error message

invalid utf8

What it means

serialize_range_mappings in swc_sourcemap builds the rangeMappings field as a Vec<u8> of base64 characters (from encode_rmi, values A-Za-z0-9+/) plus ';' separators, then asserts the buffer is valid UTF-8. Since every byte pushed is ASCII by construction, this expect indicates an internal invariant violation or a bug in swc_sourcemap's range-mapping encoder, not bad user data.

Source

Thrown at crates/swc_sourcemap/src/encoder.rs:102

                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().enumerate() {
        if token.get_dst_line() != prev_dst_line {
            prev_dst_col = 0;
            while token.get_dst_line() != prev_dst_line {
                rv.push(';');
                prev_dst_line += 1;

View on GitHub (pinned to 5176682b65)

Solutions

  1. Pin the swc_core/swc version to the last known-good release for your pipeline.
  2. Strip range-mapping info before re-encoding (rebuild the map through SourceMapBuilder without is_range) if you depend on third-party range tokens.
  3. Report an upstream issue at swc-project/swc with the minimal SourceMap that reproduces it.
Defensive patterns

Strategy: try-catch

Try / catch

// Internal invariant assertion: contain it so map generation does not
// abort the pipeline, then fall back to a map without rangeMappings.
let encoded = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
    sm.to_writer(&mut out)
}));
if encoded.is_err() {
    tracing::error!("rangeMappings encoding panicked; emitting map without range info");
    drop_range_tokens(&mut sm);
    sm.to_writer(&mut out)?;
}

Prevention

When it happens

Trigger: Encoding a SourceMap that contains range tokens (is_range set on tokens) - e.g. maps produced by tools that attach range mapping info - triggers serialize_range_mappings; the panic itself only fires if the encoder emitted a non-ASCII byte.

Common situations: Practically limited to swc_sourcemap regressions or corrupted in-memory Token state after map manipulation; users see it as a crash while generating a sourcemap for output containing range mappings.

Understand the failure class

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/7881f03101b1e275. Report an issue: GitHub.