alibaba/open-code-review · error

file %q has only %d lines, requested range %d-%d

Error message

file %q has only %d lines, requested range %d-%d

What it means

Range error from FileReadProvider.Execute: the requested start_line lies at or beyond the end of the file. The tool knows the file's total line count after reading (totalLines) and rejects the request when start_line-1 >= totalLines, reporting the actual line count and the requested range instead of returning empty content.

Source

Thrown at internal/tool/file_read.go:55

	maxLines := fileReadMaxLines
	if endLine > 0 {
		requested := int(endLine) - int(startLine) + 1
		if requested <= 0 {
			return "", fmt.Errorf("invalid line range: start_line %d is greater than end_line %d", int(startLine), int(endLine))
		}
		if requested < maxLines {
			maxLines = requested
		}
	}

	lines, totalLines, err := p.FileReader.ReadLines(ctx, filePath, int(startLine), maxLines)
	if err != nil {
		return "", fmt.Errorf("file %q not found: %w", filePath, err)
	}

	if totalLines > 0 && int(startLine)-1 >= totalLines {
		return "", fmt.Errorf("file %q has only %d lines, requested range %d-%d", filePath, totalLines, int(startLine), int(endLine))
	}

	effectiveEnd := totalLines
	if endLine > 0 && int(endLine) < effectiveEnd {
		effectiveEnd = int(endLine)
	}
	fullRange := effectiveEnd - (int(startLine) - 1)
	truncated := fullRange > fileReadMaxLines

	displayEnd := int(startLine) - 1 + len(lines)

	var sb strings.Builder
	sb.WriteString(fmt.Sprintf("File: %s (Total lines: %d)\n", filePath, totalLines))
	sb.WriteString(fmt.Sprintf("IS_TRUNCATED: %t\n", truncated))
	sb.WriteString(fmt.Sprintf("LINE_RANGE: %d-%d\n", int(startLine), displayEnd))
	for i, line := range lines {
		sb.WriteString(fmt.Sprintf("%d|%s\n", int(startLine)+i, line))
	}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Clamp start_line to at most the file's total line count reported in the error.
  2. Re-read from line 1 or re-list the file to get its current size before paging.
  3. If you were searching for content, re-run code_search — the file may have changed.

Example fix

// before: file has 120 lines
{"file_path": "a.go", "start_line": 500}
// after
{"file_path": "a.go", "start_line": 1, "end_line": 120}
Defensive patterns

Strategy: validation

Validate before calling

total, err := lineCount(filePath) // e.g. via ReadLines first page
if err != nil { return err }
if startLine > total {
    return fmt.Errorf("clamping start_line %d to total %d", startLine, total)
}

Type guard

func inBounds(start, total int) bool { return total == 0 || start >= 1 && start <= total }

Try / catch

out, err := provider.Execute(ctx, args)
if err != nil && strings.Contains(err.Error(), "has only") {
    var total int
    fmt.Sscanf(err.Error(), "file %q has only %d lines", new(string), &total)
    args["start_line"] = float64(total)
    out, err = provider.Execute(ctx, args)
}

Prevention

When it happens

Trigger: Executing file_read with start_line greater than the file's total number of lines (e.g. start_line=500 on a 120-line file). Only triggers when totalLines > 0; empty files (totalLines == 0) are not rejected by this branch.

Common situations: Agent using line numbers from a previous (longer) version of the file; paging past EOF with a fixed page size; stale index or search results referencing deleted lines.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/72eb4a78472fe021. Report an issue: GitHub.