golangci/golangci-lint · error

invalid file line index0 (%d) >= len(fc) (%d)

Error message

invalid file line index0 (%d) >= len(fc) (%d)

What it means

Bounds guard in LineCache.getRawLine: the requested 0-based line index is beyond the cached line count of the file, i.e. the reported position points past end-of-file. Usually means a linter issued an issue with a line number exceeding the file's length, or the file changed size after being cached.

Source

Thrown at pkg/fsutils/linecache.go:48

	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)
	}

	fc := bytes.Split(fileBytes, []byte("\n"))
	lc.files.Store(filePath, fileLinesCache(fc))

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Check the linter emitting out-of-range line numbers
  2. Ensure the file is not truncated/modified during the run
  3. Clear caches if the file changed while cached
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pkg/fsutils/linecache.go:48 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/7500130bd0c35c38. Report an issue: GitHub.