vitest-dev/vitest · error · Error
offset is longer than source length! offset
Error message
offset is longer than source length! offset ${offset} > length ${source.length} What it means
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.
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.
Defensive patterns
Strategy: validation
Validate before calling
function safeOffsetToLineNumber(source: string, offset: number): number {
if (offset > source.length) offset = source.length
if (offset < 0) offset = 0
// ... then call offsetToLineNumber
} Prevention
- Clear build cache after modifying source files that have associated source maps.
- Ensure source strings and offsets are derived from the same file version.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Assertion name is not set. This is a bug in Vitest. Please…
- Browser is not initialized
- --cache.dir is deprecated
- Cannot find iframe element. This is a bug in Vitest…
- Data was not properly initialized. This is a bug in Vitest…
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/90e63c16be911a45.
Report an issue: GitHub.
Appendix: source
Thrown at packages/utils/src/offset.ts:25
): number {
const lines = source.split(lineSplitRE)
const nl = /\r\n/.test(source) ? 2 : 1
let start = 0
if (lineNumber > lines.length) {
return source.length
}
for (let i = 0; i < lineNumber - 1; i++) {
start += lines[i].length + nl
}
return start + columnNumber
}
export function offsetToLineNumber(source: string, offset: number): number {
if (offset > source.length) {
throw new Error(
`offset is longer than source length! offset ${offset} > length ${source.length}`,
)
}
const lines = source.split(lineSplitRE)
const nl = /\r\n/.test(source) ? 2 : 1
let counted = 0
let line = 0
for (; line < lines.length; line++) {
const lineLength = lines[line].length + nl
if (counted + lineLength >= offset) {
break
}
counted += lineLength
}
return line + 1
}
View on GitHub (pinned to 1fa9837ec2)