Skip to content

Commit 57fc6b2

Browse files
xuezhaojunclaude
andauthored
🌱 Add golangci-lint v2 configuration and fix all lint issues (#200)
* Add golangci-lint v2 configuration and fix all lint issues Add golangci-lint v2 configuration with automated version detection and install scripts. Fix all 69 lint issues across the codebase without introducing any breaking changes. Lint fixes applied: - goimports: fix import grouping across non-test files - misspell: fix US English spelling in comments (cancelled -> canceled) - prealloc: preallocate slices with known capacity - staticcheck QF1008: simplify embedded field selectors - unconvert: remove unnecessary type conversions - revive var-naming: rename local variables and parameters to follow Go conventions (metaJson -> metaJSON, overloadId -> overloadID, etc.) - revive context-as-argument: move ctx to first parameter position - unparam: remove always-nil error return from unexported function Lint exclusions for public API stability: - Exported type names that stutter (e.g., grpc.GRPCServer) are excluded via config rules since renaming would break external consumers - Exported functions returning unexported types are excluded - Package names (common, utils, types) are excluded - Individual nolint directives added for JsonLib, MQTT_*_KEY constants, and two unparam cases where parameters are kept for readability Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: xuezhaojun <zxue@redhat.com> * fix: preserve nil slice behavior in decisionGroupsToGroupKeys Add early return for empty input to maintain nil return value, which was broken by the prealloc optimization (make returns non-nil empty slice). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: xuezhaojun <zxue@redhat.com> * Add OWNERS file for ci directory Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: xuezhaojun <zxue@redhat.com> --------- Signed-off-by: xuezhaojun <zxue@redhat.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3157f67 commit 57fc6b2

60 files changed

Lines changed: 777 additions & 73 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ GO_TEST_FLAGS :=-race
1818
GO_LD_FLAGS:=
1919

2020
verify-gocilint:
21-
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.64.6
22-
${GOPATH}/bin/golangci-lint run --timeout=3m ./...
21+
@./ci/lint/run-lint.sh
2322

2423
verify-govet:
2524
go vet -mod=vendor ./...

ci/OWNERS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
approvers:
2+
- qiujian16
3+
- xuezhaojun
4+
5+
reviewers:
6+
- qiujian16
7+
- xuezhaojun

ci/lint/README-golangci-lint.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# golangci-lint Management for sdk-go
2+
3+
This directory contains scripts and configurations for golangci-lint v2 version management. All builds require **Go 1.23+**.
4+
5+
## Quick Start
6+
7+
Add this to your `Makefile`:
8+
9+
```makefile
10+
.PHONY: lint
11+
lint:
12+
@curl -sSL https://raw.githubusercontent.com/open-cluster-management-io/sdk-go/main/ci/lint/run-lint.sh | bash
13+
```
14+
15+
That's it! The script will:
16+
1. Auto-detect Go version from `go.mod` (falls back to system Go if no `go.mod`)
17+
2. Install a compatible golangci-lint v2 version (if needed)
18+
3. Use the local config file from `ci/lint/golangci-v2.yml` (if no local config exists)
19+
4. Run `golangci-lint run`
20+
21+
**Zero configuration needed** - just run `make lint` and everything works automatically.
22+
23+
**Seamless Go version upgrades**: When you update `go.mod` (e.g., Go 1.23 to 1.25), the script automatically selects the correct golangci-lint v2 version. No manual migration required!
24+
25+
**go.mod-based detection**: The script reads the Go version from your project's `go.mod`, not the system-installed Go. This ensures consistent behavior between local development (where you may have a newer Go) and CI (which matches `go.mod`).
26+
27+
### Configuration Priority
28+
29+
1. **Local config** (`.golangci.yml` or `.golangci.yaml`) - if exists, used as-is
30+
2. **Bundled config** - copied from `ci/lint/golangci-v2.yml`
31+
32+
### Override Version (Optional)
33+
34+
If you need to use a specific version:
35+
36+
```bash
37+
GOLANGCI_LINT_VERSION=v2.8.0 make lint
38+
```
39+
40+
---
41+
42+
## Files Included
43+
44+
| File | Description |
45+
|------|-------------|
46+
| `run-lint.sh` | One-command lint runner (install + config + run) |
47+
| `install-golangci-lint.sh` | Installation script with Go version detection |
48+
| `golangci-v2.yml` | Default config for golangci-lint v2.x |
49+
50+
---
51+
52+
## Go Version Compatibility
53+
54+
The script reads the Go version from `go.mod` and selects the correct golangci-lint v2 version accordingly (falls back to system Go version if no `go.mod` is found).
55+
56+
| Go Version | golangci-lint Version | Config File |
57+
|------------|----------------------|-------------|
58+
| Go 1.24+ | v2.8.0 | `golangci-v2.yml` |
59+
| Go 1.23 | v2.3.1 | `golangci-v2.yml` |
60+
61+
---
62+
63+
## Upgrading Version
64+
65+
To upgrade golangci-lint:
66+
67+
1. Edit the version mapping in both `run-lint.sh` and `install-golangci-lint.sh`
68+
2. Commit and push the changes
69+
3. The new version will be used on the next lint run
70+
71+
---
72+
73+
## Environment Variables
74+
75+
| Variable | Default | Description |
76+
|----------|---------|-------------|
77+
| `GOLANGCI_CONFIG_DIR` | `/tmp/golangci-lint-config` | Config cache directory |
78+
| `GOLANGCI_UPDATE_CONFIG` | `false` | Force re-copy config if set to `true` |
79+
| `GOLANGCI_LINT_VERSION` | (auto) | Override auto-detected golangci-lint version |
80+
81+
---
82+
83+
## Known Issues and Solutions
84+
85+
### 1. Go Version Mismatch Error
86+
87+
**Error**: `the Go language version used to build golangci-lint is lower than the targeted Go version`
88+
89+
**Solution**: This script automatically selects a compatible version. If you see this error, update the version mapping or use the environment variable override.
90+
91+
### 2. Network Issues
92+
93+
**Problem**: Download fails in CI or behind firewall.
94+
95+
**Solution**: The script skips download if the correct version is already installed. Consider caching the binary or using an internal artifact server.
96+
97+
---
98+
99+
## References
100+
101+
- [golangci-lint Configuration](https://golangci-lint.run/docs/configuration/)
102+
- [Local Installation](https://golangci-lint.run/docs/welcome/install/local/)
103+
- [Issue #5032: Go version compatibility](https://github.com/golangci/golangci-lint/issues/5032)
104+
- [Discussion #3954: Sharing Configs Across Repos](https://github.com/golangci/golangci-lint/discussions/3954)
105+
106+
---
107+
108+
## License
109+
110+
Copyright (c) Red Hat, Inc.
111+
Copyright Contributors to the Open Cluster Management project

ci/lint/golangci-v2.yml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# golangci-lint v2.x configuration
2+
# Compatible with golangci-lint v2.0.0+
3+
# Documentation: https://golangci-lint.run/docs/configuration/
4+
# Migration guide: https://golangci-lint.run/docs/product/migration-guide/
5+
6+
version: "2"
7+
8+
run:
9+
timeout: 5m
10+
modules-download-mode: readonly
11+
12+
linters:
13+
# Use 'none' as default and explicitly enable linters
14+
# Options: none, standard, all, fast
15+
default: none
16+
17+
enable:
18+
# Default/standard linters
19+
- errcheck
20+
- govet
21+
- ineffassign
22+
- staticcheck # includes gosimple and stylecheck
23+
- unused
24+
25+
# Code style
26+
- misspell
27+
- unconvert
28+
- unparam
29+
- nakedret
30+
- prealloc
31+
- copyloopvar # replaces exportloopref in v2
32+
- revive
33+
34+
settings:
35+
misspell:
36+
locale: US
37+
38+
nakedret:
39+
max-func-lines: 30
40+
41+
revive:
42+
rules:
43+
- name: blank-imports
44+
- name: context-as-argument
45+
- name: context-keys-type
46+
- name: dot-imports
47+
- name: error-return
48+
- name: error-strings
49+
- name: error-naming
50+
- name: exported
51+
- name: increment-decrement
52+
- name: var-naming
53+
- name: var-declaration
54+
- name: package-comments
55+
- name: range
56+
- name: receiver-naming
57+
- name: time-naming
58+
- name: unexported-return
59+
- name: indent-error-flow
60+
- name: errorf
61+
62+
# Exclusions configuration (v2 style)
63+
exclusions:
64+
# Use presets for common exclusions
65+
# Options: comments, std-error-handling, common-false-positives, legacy
66+
presets:
67+
- comments
68+
- std-error-handling
69+
70+
# Generated files handling: strict, lax, or disable
71+
generated: strict
72+
73+
rules:
74+
# Exported type names that stutter (e.g., grpc.GRPCServer) cannot be renamed
75+
# without breaking external consumers of this SDK.
76+
- linters: [revive]
77+
text: "exported: type name will be used as"
78+
# Exported functions returning unexported types are part of the public API
79+
# and cannot be changed without breaking external consumers.
80+
- linters: [revive]
81+
text: "unexported-return: exported func"
82+
# Package names (common, utils, util) cannot be renamed without breaking
83+
# import paths for external consumers.
84+
- linters: [revive]
85+
text: "avoid meaningless package names"
86+
87+
# Paths to exclude from all linting
88+
paths:
89+
- _test\.go
90+
- ^e2e/
91+
- zz_generated\.deepcopy\.go
92+
93+
# Formatters are separate from linters in v2
94+
formatters:
95+
enable:
96+
- gofmt
97+
- goimports
98+
99+
settings:
100+
goimports:
101+
local-prefixes: open-cluster-management.io

0 commit comments

Comments
 (0)