chenhg5/cc-connect · error

invalid start line

Error message

invalid start line

What it means

readFileRange validates its bounds before opening the file; start must be a positive 1-based line number. start <= 0 means the requested range begins before the first line, which is invalid for 1-based file line addressing.

Source

Thrown at core/reference_show.go:204

	lines := make([]string, 0, maxLines)
	truncated := false
	for scanner.Scan() {
		if len(lines) >= maxLines {
			truncated = true
			break
		}
		lines = append(lines, scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		return nil, false, err
	}
	return lines, truncated, nil
}

func readFileRange(path string, start, end, maxLines int) ([]string, bool, error) {
	if start <= 0 {
		return nil, false, fmt.Errorf("invalid start line")
	}
	if end <= 0 || end < start {
		return nil, false, fmt.Errorf("invalid end line")
	}
	if maxLines <= 0 {
		maxLines = defaultShowMaxRange
	}

	f, err := os.Open(path)
	if err != nil {
		return nil, false, err
	}
	defer f.Close()

	scanner := bufio.NewScanner(f)
	buf := make([]byte, 0, 64*1024)
	scanner.Buffer(buf, 1024*1024)

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Clamp the start line to a minimum of 1 before calling readFileRange
  2. Reject location formats with start < 1 at reference-parse time and tell the user lines are 1-based
  3. Fix the caller's start computation to floor at 1 (as readFileContext already does)

Example fix

// before
readFileRange(path, 0, 20, 100)
// after
if start < 1 {
    start = 1
}
readFileRange(path, start, 20, 100)
Defensive patterns

Strategy: validation

Validate before calling

if start < 1 {
    return fmt.Errorf("lines are 1-based; got %d", start)
}

Try / catch

lines, truncated, err := readFileRange(path, start, end, max)
if err != nil && strings.Contains(err.Error(), "invalid start line") {
    start = 1
    lines, truncated, err = readFileRange(path, start, end, max)
}

Prevention

When it happens

Trigger: renderReferenceFile or readFileContext computing/carrying a start line of 0 or negative — e.g. a reference like file.go:0 or a context window calculation that underflows before clamping.

Common situations: Users specifying line 0 in a :N-M location; code that computes start = line - before without flooring at 1 before this call; off-by-one in reference parsers converting 0-based user input.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/7cb59b984d13e194. Report an issue: GitHub.