{"record":{"id":"22c2c6219ec5d55d","repo":"swc-project/swc","slug":"invalid-source-reference-src-id","errorCode":null,"errorMessage":"invalid source reference: {src_id}","messagePattern":"invalid source reference: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_config/src/source_map.rs","lineNumber":83,"sourceCode":"                        }\n\n                        nums.clear();\n                        nums = parse_vlq_segment(segment)?;\n                        dst_col = (i64::from(dst_col) + nums[0]) as u32;\n\n                        let mut src = !0;\n                        let mut name = !0;\n\n                        if nums.len() > 1 {\n                            if nums.len() != 4 && nums.len() != 5 {\n                                bail!(\n                                    \"invalid vlq segment size; expected 4 or 5, got {}\",\n                                    nums.len()\n                                );\n                            }\n                            src_id = (i64::from(src_id) + nums[1]) as u32;\n                            if src_id >= sources.len() as u32 {\n                                bail!(\"invalid source reference: {src_id}\");\n                            }\n\n                            src = src_id;\n                            src_line = (i64::from(src_line) + nums[2]) as u32;\n                            src_col = (i64::from(src_col) + nums[3]) as u32;\n\n                            if nums.len() > 4 {\n                                name_id = (i64::from(name_id) + nums[4]) as u32;\n                                if name_id >= names.len() as u32 {\n                                    bail!(\"invalid name reference: {name_id}\");\n                                }\n                                name = name_id;\n                            }\n                        }\n\n                        tokens.push(RawToken {\n                            dst_line: dst_line as u32,\n                            dst_col,","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_config/src/source_map.rs#L65-L101","documentation":"While decoding the `mappings` of a parsed input source map, swc_config tracks the source index as a delta: src_id = src_id + nums[1] per segment. After applying the delta, the absolute index must stay within the `sources` array bounds. If the accumulated src_id >= sources.len(), the mappings reference a source that does not exist, so decoding bails. This means the `sources` array and the `mappings` string are inconsistent with each other.","triggerScenarios":"Passing an input source map object where `sources` was truncated/replaced (fewer entries) while `mappings` still carries deltas pointing past the end, or where mappings from a different file were spliced into this source map.","commonSituations":"Rewriting `sources` paths for deployment (e.g. stripping prefixes, deduplicating) while leaving mappings untouched; merging source maps from multiple chunks; a producer tool emitting 1-based or otherwise offset source indices; hand-rolled source map manipulation in build scripts.","solutions":["Regenerate the whole source map from the original producer so `sources` and `mappings` stay consistent","If you must rewrite `sources`, keep the array length and order identical (rewrite entries in place instead of removing them)","Validate mappings against sources length with a standard source-map library first and locate the offending segment","Pass the source map as a JSON string (SourceMapContent::Json) or drop it if not strictly needed"],"exampleFix":"// before: sources rewritten and shrunk\nmap.sources = map.sources.map(shorten).filter(Boolean); // length changed!\nconfig.input_source_map = Some(InputSourceMap::SourceMapFile(map.into()));\n\n// after: rewrite in place, never change length/order\nmap.sources = map.sources\n    .iter()\n    .map(|s| shorten(s)) // returns a placeholder instead of removing\n    .collect::<Vec<_>>();","handlingStrategy":"validation","validationCode":"// Reject maps whose mappings can index outside `sources` before calling swc\nconst { decode } = require('vlq'); // or inline VLQ decoder\nfunction indicesInRange(mappings, len) {\n  let src = 0;\n  for (const line of mappings.split(';'))\n    for (const seg of line.split(',')) {\n      if (!seg) continue;\n      const f = seg.split(/(?=[A-Za-z0-9+/])/); // rough split; prefer vlq lib\n      const nums = [...seg.matchAll(/[A-Za-z0-9+/]/g)];\n      // decode properly with the vlq package in real code:\n      // const parts = seg.match(/..../) etc.\n      void f; void nums;\n    }\n  return true;\n}\n// In practice: const SourceMap = require('source-map'); new SourceMap.SourceMapConsumer(json) — throws on inconsistency.","typeGuard":null,"tryCatchPattern":"try { await transform(src, { inputSourceMap: map }) } catch (e) { if (/invalid source reference/.test(e.message)) { map = null; return transform(src, {}); } throw e; }","preventionTips":["When rewriting `sources` paths, preserve array length and order exactly","Validate with `new (require('source-map').SourceMapConsumer)(json)` before injecting the map","Avoid concatenating mappings from different files without remapping indices"],"tags":["sourcemap","source-index","input-sourcemap","delta-encoding","malformed-input"],"backgroundTag":"sourcemap-index-out-of-range","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}