golangci/golangci-lint · error

failed to read go.mod: %w

Error message

failed to read go.mod: %w

What it means

computeGoModSalt reads the go.mod file (located via goenv.GOMOD) to compute a dirhash that salts lint-result caching. If os.ReadFile fails, this error wraps the underlying OS error and aborts hash-salt initialization during `golangci-lint run`/cache setup.

Source

Thrown at pkg/commands/run.go:713

	h := sha256.New()
	if _, err := h.Write(configData.Bytes()); err != nil {
		return nil, err
	}

	return h.Sum(nil), nil
}

func computeGoModSalt() (string, error) {
	values, err := goenv.Get(context.Background(), goenv.GOMOD)
	if err != nil {
		return "", fmt.Errorf("failed to get goenv: %w", err)
	}

	goModPath := filepath.Clean(values[goenv.GOMOD])

	data, err := os.ReadFile(goModPath)
	if err != nil {
		return "", fmt.Errorf("failed to read go.mod: %w", err)
	}

	// NOTE: the variable `goModPath` is not used here to ensure getting the same hash, independently of the location, for the same content.
	sum, err := dirhash.Hash1([]string{"go.mod"}, func(string) (io.ReadCloser, error) {
		return io.NopCloser(bytes.NewReader(data)), nil
	})
	if err != nil {
		return "", fmt.Errorf("failed to compute go.sum: %w", err)
	}

	return sum, nil
}

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Run golangci-lint from a directory inside a valid Go module that contains go.mod
  2. Verify `go env GOMOD` points at an existing go.mod file
  3. Restore go.mod (e.g. `git checkout -- go.mod`) if it was deleted
  4. Fix file permissions on go.mod so the lint process can read it

Example fix

// before (broken CI)
run: golangci-lint run ./...
// after
run: |
  test -f go.mod || go mod init
golangci-lint run ./...
Defensive patterns

Strategy: validation

Validate before calling

gomod := os.Getenv("GOMOD")
if gomod == "" || gomod == os.DevNull {
	return fmt.Errorf("not inside a Go module: GOMOD is empty")
}
if _, err := os.Stat(gomod); err != nil {
	return fmt.Errorf("go.mod unreadable at %s: %w", gomod, err)
}

Prevention

When it happens

Trigger: `golangci-lint run` with cache hashing enabled where the go.mod path reported by `go env GOMOD` does not exist or is unreadable: running outside a Go module, GOFLAGS/env pointing elsewhere, deleted go.mod, or permission-restricted go.mod.

Common situations: Running golangci-lint in a directory without go.mod (GOPATH mode or non-module dir); CI checkout missing go.mod; restrictive file permissions; GOMOD env var overridden in containers.

Related errors


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