{"record":{"id":"9ebba2cf750f3f8b","repo":"vitejs/vite","slug":"offset-is-longer-than-source-length-offset-offs","errorCode":null,"errorMessage":"offset is longer than source length! offset ${offset} > length ${source.length}","messagePattern":"offset is longer than source length! offset (.+?) > length (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/utils.ts","lineNumber":510,"sourceCode":"  /** 0-based */\n  column: number\n}\n\nexport function posToNumber(source: string, pos: number | Pos): number {\n  if (typeof pos === 'number') return pos\n  const lines = source.split(splitRE)\n  const { line, column } = pos\n  let start = 0\n  for (let i = 0; i < line - 1 && i < lines.length; i++) {\n    start += lines[i].length + 1\n  }\n  return start + column\n}\n\nexport function numberToPos(source: string, offset: number | Pos): Pos {\n  if (typeof offset !== 'number') return offset\n  if (offset > source.length) {\n    throw new Error(\n      `offset is longer than source length! offset ${offset} > length ${source.length}`,\n    )\n  }\n\n  const lines = source.slice(0, offset).split(splitRE)\n  return {\n    line: lines.length,\n    column: lines[lines.length - 1].length,\n  }\n}\n\nconst MAX_DISPLAY_LEN = 120\nconst ELLIPSIS = '...'\n\nexport function generateCodeFrame(\n  source: string,\n  start: number | Pos = 0,\n  end?: number | Pos,","sourceCodeStart":492,"sourceCodeEnd":528,"githubUrl":"https://github.com/vitejs/vite/blob/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/node/utils.ts#L492-L528","documentation":"Thrown by numberToPos() when the supplied character offset exceeds the source string length. numberToPos converts a numeric offset to a {line, column} position; an out-of-range offset points to a bug in whatever produced the offset (sourcemap, plugin, error overlay).","triggerScenarios":"A plugin, sourcemap, or error-reporting path calling numberToPos(source, offset) where offset > source.length. Guard at utils.ts:509. Often surfaced via generateCodeFrame or error overlay rendering after a transform.","commonSituations":"A sourcemap mapping points past EOF after a transform mutated code length. A plugin returns a position from a stale AST after the source changed (HMR partial update). Minified/bundled output whose sourcemap offsets don't match. Outdated Vite cache after editing a file.","solutions":["Clear Vite's cache (node_modules/.vite) and restart the dev server to drop stale sourcemaps/ASTs.","Identify the plugin doing source-position reporting (search stack trace for numberToPos / generateCodeFrame callers) and check it recomputes positions after transforms.","Verify the failing file isn't being transformed by two plugins that disagree on source length.","Report a bug with a reproduction if it persists on a clean cache — this usually indicates an internal position-tracking defect."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (offset > source.length) {\n  throw new RangeError(`offset ${offset} out of bounds for length ${source.length}`)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["In plugins, recompute AST/source positions after each transform, never reuse pre-transform offsets.","Clear node_modules/.vite after large edits to drop stale sourcemaps."],"tags":["sourcemap","transform","internal"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}