chenhg5/cc-connect · error

invalid end line

Error message

invalid end line

What it means

readFileRange requires end to be a positive line number and at least equal to start, since ranges are inclusive 1-based [start,end]. An end of 0, negative, or below start is logically impossible and rejected.

Source

Thrown at core/reference_show.go:207

	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)

	lines := make([]string, 0, minInt(end-start+1, maxLines))
	lineNo := 0
	truncated := false

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Swap the range bounds so end >= start before calling (or normalize inside the parser)
  2. Ensure the end line defaults to start (or file length) when only one line is given
  3. Validate user-supplied N-M locations at parse time and reject end < start with a clear message

Example fix

// before
readFileRange(path, 10, 5, 100) // end < start
// after
if end < start {
    start, end = end, start
}
readFileRange(path, start, end, 100)
Defensive patterns

Strategy: validation

Validate before calling

if end < 1 || end < start {
    return fmt.Errorf("invalid range %d-%d", start, end)
}

Try / catch

lines, _, err := readFileRange(path, start, end, max)
if err != nil && strings.Contains(err.Error(), "invalid end line") {
    return "use format :start-end with end >= start", nil
}

Prevention

When it happens

Trigger: A reference location like file.go:10-5 (end < start) or file.go:10-0 (end <= 0); callers passing swapped or uninitialized end values.

Common situations: Users typing reversed ranges (10-5 instead of 5-10); parsers misordering the two halves of an N-M suffix; default-zero end variables never set.

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/380dee3c80748ad0. Report an issue: GitHub.