{"record":{"id":"90e63c16be911a45","repo":"vitest-dev/vitest","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/utils/src/offset.ts","lineNumber":25,"sourceCode":"): number {\n  const lines = source.split(lineSplitRE)\n  const nl = /\\r\\n/.test(source) ? 2 : 1\n  let start = 0\n\n  if (lineNumber > lines.length) {\n    return source.length\n  }\n\n  for (let i = 0; i < lineNumber - 1; i++) {\n    start += lines[i].length + nl\n  }\n\n  return start + columnNumber\n}\n\nexport function offsetToLineNumber(source: string, offset: number): number {\n  if (offset > source.length) {\n    throw new Error(\n      `offset is longer than source length! offset ${offset} > length ${source.length}`,\n    )\n  }\n  const lines = source.split(lineSplitRE)\n  const nl = /\\r\\n/.test(source) ? 2 : 1\n  let counted = 0\n  let line = 0\n  for (; line < lines.length; line++) {\n    const lineLength = lines[line].length + nl\n    if (counted + lineLength >= offset) {\n      break\n    }\n\n    counted += lineLength\n  }\n  return line + 1\n}\n","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/utils/src/offset.ts#L7-L43","documentation":"offsetToLineNumber converts a character offset into a 1-based line number within a source string. It guards that the offset does not exceed source.length. This is used by Vitest's source-map and stack-trace formatting paths when translating positions. An out-of-bounds offset usually means a source map or AST offset was computed against a different (longer) version of the source than the one passed in.","triggerScenarios":"Calling offsetToLineNumber with an offset derived from one version of a file while passing a shorter (e.g. cached/transformed) source string; source-map resolution producing an offset past EOF due to a mismatch between compiled and original source.","commonSituations":"Stale build cache where offsets refer to an older longer file; HMR/partial transforms; source maps pointing past the end of a minified or truncated source.","solutions":["Clear build/cache directories and rebuild so source strings and offsets are consistent.","Verify the source string passed to offsetToLineNumber matches the file the offset was computed from.","If this surfaces from Vitest internals during normal test runs, file a bug with the source map and source file attached."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"function safeOffsetToLineNumber(source: string, offset: number): number {\n  if (offset > source.length) offset = source.length\n  if (offset < 0) offset = 0\n  // ... then call offsetToLineNumber\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clear build cache after modifying source files that have associated source maps.","Ensure source strings and offsets are derived from the same file version."],"tags":["sourcemap","offset","internal","cache"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}