swc-project/swc · error

invalid vlq segment size; expected 4 or 5, got {}

Error message

invalid vlq segment size; expected 4 or 5, got {}

What it means

SourceMapContent::Parsed::to_sourcemap() in swc_config decodes the `mappings` field of a source map passed in parsed-object form (e.g. swc's `inputSourceMap` config). Each comma-separated VLQ segment must contain either 1 field (generated column delta only) or exactly 4/5 fields (column, source index, source line, source column, optional name index). When a segment decodes to 2, 3, or 6+ numbers the mapping data is malformed and decoding aborts with this bail.

Source

Thrown at crates/swc_config/src/source_map.rs:76

                    }

                    dst_col = 0;

                    for segment in line.split(',') {
                        if segment.is_empty() {
                            continue;
                        }

                        nums.clear();
                        nums = parse_vlq_segment(segment)?;
                        dst_col = (i64::from(dst_col) + nums[0]) as u32;

                        let mut src = !0;
                        let mut name = !0;

                        if nums.len() > 1 {
                            if nums.len() != 4 && nums.len() != 5 {
                                bail!(
                                    "invalid vlq segment size; expected 4 or 5, got {}",
                                    nums.len()
                                );
                            }
                            src_id = (i64::from(src_id) + nums[1]) as u32;
                            if src_id >= sources.len() as u32 {
                                bail!("invalid source reference: {src_id}");
                            }

                            src = src_id;
                            src_line = (i64::from(src_line) + nums[2]) as u32;
                            src_col = (i64::from(src_col) + nums[3]) as u32;

                            if nums.len() > 4 {
                                name_id = (i64::from(name_id) + nums[4]) as u32;
                                if name_id >= names.len() as u32 {
                                    bail!("invalid name reference: {name_id}");
                                }

View on GitHub (pinned to 5176682b65)

Solutions

  1. Feed the source map as a raw JSON string (SourceMapContent::Json) so swc_sourcemap's full parser handles it, and fix the mappings if that parser also rejects them
  2. Validate the source map with a standard tool (e.g. the `source-map` npm library or `sourcemaps` CLI) before passing it to swc to find the exact corrupt segment
  3. Regenerate the source map from the tool that originally produced the minified/bundled output instead of reusing a mutated copy
  4. If the input source map is optional for your pipeline, omit it and let swc generate fresh mappings from sourcesContent

Example fix

// before (corrupt segment: 'AAAA,AA,BBBB')
let cfg = CompilerInput {
    input_source_map: Some(InputSourceMap::SourceMapFile(SourceMapFile {
        src: SourceMapContent::Parsed { /* mappings with bad segment */ .. },
        ..
    })),
    ..Default::default()
};

// after: pass the original JSON string so the standard parser validates it
let cfg = CompilerInput {
    input_source_map: Some(InputSourceMap::SourceMapFile(SourceMapFile {
        src: SourceMapContent::Json(original_json.to_string()),
        ..
    })),
    ..Default::default()
};
Defensive patterns

Strategy: validation

Validate before calling

// Validate segment field counts before handing the map to swc (Node side)
function mappingsLookSane(mappings) {
  for (const line of mappings.split(';')) {
    for (const seg of line.split(',')) {
      if (seg === '') continue;
      // count VLQ fields by sign-bit terminators
      let fields = 0;
      for (const ch of seg) {
        const v = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.indexOf(ch);
        if (v < 0) return false;
        if ((v & 32) === 0) fields += 1; // continuation bit clear -> field end
      }
      if (fields !== 1 && fields !== 4 && fields !== 5) return false;
    }
  }
  return true;
}
if (!mappingsLookSane(map.mappings)) throw new Error('corrupt input source map');

Try / catch

try { await transform(src, { inputSourceMap: map }) } catch (e) { if (/invalid vlq segment size/.test(e.message)) { /* drop bad map, retry without it */ } else throw e; }

Prevention

When it happens

Trigger: Calling a swc transform API with `inputSourceMap` supplied as an object whose `mappings` string contains a segment with an illegal field count, e.g. 'AAAA,AA' or a segment where fields were dropped or duplicated during hand-editing/truncation of the JSON.

Common situations: Round-tripping a source map through a custom tool that rewrites mappings; consuming a source map from a bundler/minifier that emits nonstandard segments; copying only part of the mappings string when constructing the config; stale/corrupted source maps cached on disk from a previous build.

Related errors


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