golangci/golangci-lint · error

invalid file line index0 < 0: %d

Error message

invalid file line index0 < 0: %d

What it means

Range guard in LineCache.getRawLine: the computed 0-based line index is negative, meaning a caller passed index1 < 1 to GetLine (after the -1 offset). Indicates an internal/upper-layer bug where a linter emitted a zero or negative line number.

Source

Thrown at pkg/fsutils/linecache.go:44

	}

	const index1To0Offset = -1
	rawLine, err := lc.getRawLine(filePath, index1+index1To0Offset)
	if err != nil {
		return "", err
	}

	return string(bytes.Trim(rawLine, "\r")), nil
}

func (lc *LineCache) getRawLine(filePath string, index0 int) ([]byte, error) {
	fc, err := lc.getFileCache(filePath)
	if err != nil {
		return nil, fmt.Errorf("failed to get file %s lines cache: %w", filePath, err)
	}

	if index0 < 0 {
		return nil, fmt.Errorf("invalid file line index0 < 0: %d", index0)
	}

	if index0 >= len(fc) {
		return nil, fmt.Errorf("invalid file line index0 (%d) >= len(fc) (%d)", index0, len(fc))
	}

	return fc[index0], nil
}

func (lc *LineCache) getFileCache(filePath string) (fileLinesCache, error) {
	loadedFc, ok := lc.files.Load(filePath)
	if ok {
		return loadedFc.(fileLinesCache), nil
	}

	fileBytes, err := lc.fileCache.GetFileBytes(filePath)
	if err != nil {
		return nil, fmt.Errorf("can't get file %s bytes from cache: %w", filePath, err)

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Fix the linter or caller supplying an invalid line number
  2. Validate line numbers before requesting lines
  3. Report the offending linter if it emits line 0 or negative positions
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pkg/fsutils/linecache.go:44 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of golangci/golangci-lint@ed7a235d2d (2026-09-02). Data as JSON: /api/errors/c5f98a1d6d86601c. Report an issue: GitHub.