swc-project/swc · error
invalid name reference: {name_id}
Error message
invalid name reference: {name_id} What it means
Same decoder loop as the source-index check, but for the optional 5th VLQ field: name_id accumulates deltas across segments and must remain within the `names` array bounds. When the accumulated name index >= names.len(), the mappings reference an original identifier name that does not exist in the `names` array, and decoding fails. The `names` array and `mappings` are out of sync.
Source
Thrown at crates/swc_config/src/source_map.rs:93
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}");
}
name = name_id;
}
}
tokens.push(RawToken {
dst_line: dst_line as u32,
dst_col,
src_line,
src_col,
src_id: src,
name_id: name,
is_range: false,
});
}
}
let mut map = SourceMap::new(View on GitHub (pinned to 5176682b65)
Solutions
- Regenerate the source map from the original tool so `names` and `mappings` stay consistent
- If name mappings are not needed, strip the 5th field from segments as well when stripping `names` (or set names to match indices)
- Validate with a standard source-map library first to pinpoint the inconsistent segment
- Pass the map as a JSON string or omit it if not required by your pipeline
Example fix
// before: names array dropped but mappings still reference it map.names = vec![]; // after: keep names, or regenerate the map wholesale // (preferred) let fresh = producer.generate_source_map();
Defensive patterns
Strategy: validation
Validate before calling
// Cheap consistency gate: if you strip `names`, ensure no 5-field segments remain
function hasNameRefs(mappings) { /* decode VLQ; true if any segment has 5 fields */ }
if (map.names.length === 0 && hasNameRefs(map.mappings)) throw new Error('mappings reference missing names'); Try / catch
try { await transform(src, { inputSourceMap: map }) } catch (e) { if (/invalid name reference/.test(e.message)) { return transform(src, {}); } throw e; } Prevention
- Never truncate the `names` array without also stripping the 5th field from segments
- Regenerate maps wholesale instead of surgically editing fields
- Gate third-party source maps through a standard consumer in CI
When it happens
Trigger: Supplying a parsed input source map whose `names` array was stripped (e.g. minified down to save bytes) while mappings still contain 5-field segments with name deltas; or splicing mappings from another file.
Common situations: Source map post-processors that drop or truncate the `names` array for size; merging/concatenating source maps without remapping name indices; producer tools emitting inconsistent maps after renaming passes; cached stale maps after a build change.
Related errors
- invalid source reference: {src_id}
- invalid vlq segment size; expected 4 or 5, got {}
- InvalidData
- invalid utf8
- Failed to convert RawValue to Data
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/a342a4ee905069ab.
Report an issue: GitHub.