oraios/serena · error · InvalidTextLocationError

{index=}

Error message

{index=}

What it means

InvalidTextLocationError with the offending index raised by FileUtils.get_line_col_from_index (src/solidlsp/ls_utils.py:211) when the given 0-based character index is past the end of the text — stepping never reached the index.

Source

Thrown at src/solidlsp/ls_utils.py:211

        # step over full lines; once the line containing the index has been processed, compute
        # the position from the difference to the respective line's start index
        text_stepper = TextStepper(text)
        while text_stepper.step_line():
            if text_stepper.idx > index:
                if text_stepper.is_newline:
                    # position was stepped over as part of the previous line
                    if index > text_stepper.prev_line_end_idx:
                        # edge case: the index points into a multi-character newline sequence ("\r\n");
                        # map this to the beginning of the following line
                        return text_stepper.line, 0
                    return text_stepper.line - 1, index - text_stepper.prev_line_start_idx
                else:
                    # position was stepped over as part of the current line (which was not followed by a newline)
                    return text_stepper.line, index - text_stepper.line_start_idx

        # handle the case where the end of the text was reached without stepping past the index
        if index > text_stepper.idx:
            raise InvalidTextLocationError(f"{index=}")
        return text_stepper.line, text_stepper.col

    @classmethod
    def get_line_from_index(cls, text: str, index: int) -> int:
        """
        :param text: the text in which the index is to be located
        :param index: the 0-based index in the text
        :return: the 0-based line number corresponding to the index in the text
        """
        return cls.get_line_col_from_index(text, index)[0]

    @staticmethod
    def get_index_from_line_col(text: str, line: int, col: int) -> int:
        """
        :param text: the text in which the coordinates are to be located
        :param line: the 0-based line number
        :param col: the 0-based column number
        :return: the corresponding 0-based index in the text

View on GitHub (pinned to 7fcbca7e62)

Solutions

  1. Assert index <= len(text) before converting the index to line/col.
  2. Recompute the index against the current text rather than reusing cached offsets.
  3. Guard search results so indices come from the same text object being queried.

Example fix

// before
line, col = FileUtils.get_line_col_from_index(text, stale_index)
// after
if stale_index <= len(text):
    line, col = FileUtils.get_line_col_from_index(text, stale_index)
Defensive patterns

Strategy: validation

Validate before calling

def index_in_range(text: str, index: int) -> bool:
    return 0 <= index <= len(text)

Type guard

def safe_index(text: str, index: int) -> int | None:
    return index if 0 <= index <= len(text) else None

Try / catch

try:
    line, col = FileUtils.get_line_col_from_index(text, idx)
except InvalidTextLocationError:
    line, col = FileUtils.get_line_col_from_index(text, len(text))

Prevention

When it happens

Trigger: Calling get_line_col_from_index(text, index), search_text, or find_text_coordinates with an index greater than len(text), often from stale index data captured before the document shrank.

Common situations: Caching character offsets across file edits; regex/search offsets applied to a different text version than the one searched; off-by-one using len(text) inclusive.

Related errors


AI-assisted analysis of oraios/serena@7fcbca7e62 (2026-08-29). Data as JSON: /api/errors/61de79dc0e527c03. Report an issue: GitHub.