Skip to content

Commit b18c047

Browse files
silverwindlunny
andauthored
Upgrade gopls to v0.19.0, add make fix (#34772)
Upgrade to [v0.19.0](https://github.com/golang/tools/releases/tag/gopls%2Fv0.19.0) and fix issues. Runs with new `warning` serverity setting. This likely does less checks than before. Additionally, add `make fix` which runs modernize. This is also verified on CI. For the record, here are the issues discoverd when running with `info` severity, in case we want to fix these: ``` tests/integration/repo_test.go:95:5-14: could use tagged switch on i tests/integration/api_packages_generic_test.go:149:4-64: could use tagged switch on setting.Packages.Storage.Type services/webhook/msteams_test.go:33:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:59:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:85:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:111:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:138:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:161:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:187:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:213:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:239:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:266:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:407:4-33: could use tagged switch on fact.Name tests/integration/api_packages_conan_test.go:350:6-33: could use tagged switch on pf.Name models/issues/tracked_time_test.go:98:3-18: could use tagged switch on user.ID tests/integration/api_token_test.go:505:5-43: could use tagged switch on minRequiredLevel services/gitdiff/gitdiff.go:220:33-46: method "getLineLegacy" is unused ``` --------- Co-authored-by: Lunny Xiao <[email protected]>
1 parent 8efc4ca commit b18c047

File tree

6 files changed

+24
-9
lines changed

6 files changed

+24
-9
lines changed

Makefile

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
3636
GO_LICENSES_PACKAGE ?= github.com/google/go-licenses@v1
3737
GOVULNCHECK_PACKAGE ?= golang.org/x/vuln/cmd/govulncheck@v1
3838
ACTIONLINT_PACKAGE ?= github.com/rhysd/actionlint/cmd/actionlint@v1
39-
GOPLS_PACKAGE ?= golang.org/x/tools/[email protected]
39+
GOPLS_PACKAGE ?= golang.org/x/tools/[email protected]
40+
GOPLS_MODERNIZE_PACKAGE ?= golang.org/x/tools/gopls/internal/analysis/modernize/cmd/[email protected]
4041

4142
DOCKER_IMAGE ?= gitea/gitea
4243
DOCKER_TAG ?= latest
@@ -230,7 +231,7 @@ clean: ## delete backend and integration files
230231
tests/e2e/reports/ tests/e2e/test-artifacts/ tests/e2e/test-snapshots/
231232

232233
.PHONY: fmt
233-
fmt: ## format the Go code
234+
fmt: ## format the Go and template code
234235
@GOFUMPT_PACKAGE=$(GOFUMPT_PACKAGE) $(GO) run build/code-batch-process.go gitea-fmt -w '{file-list}'
235236
$(eval TEMPLATES := $(shell find templates -type f -name '*.tmpl'))
236237
@# strip whitespace after '{{' or '(' and before '}}' or ')' unless there is only
@@ -249,6 +250,19 @@ fmt-check: fmt
249250
exit 1; \
250251
fi
251252

253+
.PHONY: fix
254+
fix: ## apply automated fixes to Go code
255+
$(GO) run $(GOPLS_MODERNIZE_PACKAGE) -fix ./...
256+
257+
.PHONY: fix-check
258+
fix-check: fix
259+
@diff=$$(git diff --color=always $(GO_SOURCES)); \
260+
if [ -n "$$diff" ]; then \
261+
echo "Please run 'make fix' and commit the result:"; \
262+
printf "%s" "$${diff}"; \
263+
exit 1; \
264+
fi
265+
252266
.PHONY: $(TAGS_EVIDENCE)
253267
$(TAGS_EVIDENCE):
254268
@mkdir -p $(MAKE_EVIDENCE_DIR)
@@ -288,7 +302,7 @@ checks: checks-frontend checks-backend ## run various consistency checks
288302
checks-frontend: lockfile-check svg-check ## check frontend files
289303

290304
.PHONY: checks-backend
291-
checks-backend: tidy-check swagger-check fmt-check swagger-validate security-check ## check backend files
305+
checks-backend: tidy-check swagger-check fmt-check fix-check swagger-validate security-check ## check backend files
292306

293307
.PHONY: lint
294308
lint: lint-frontend lint-backend lint-spell ## lint everything
@@ -809,6 +823,7 @@ deps-tools: ## install tool dependencies
809823
$(GO) install $(GOVULNCHECK_PACKAGE) & \
810824
$(GO) install $(ACTIONLINT_PACKAGE) & \
811825
$(GO) install $(GOPLS_PACKAGE) & \
826+
$(GO) install $(GOPLS_MODERNIZE_PACKAGE) & \
812827
wait
813828

814829
node_modules: package-lock.json

models/migrations/v1_22/v294_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package v1_22 //nolint
55

66
import (
7-
"slices"
87
"testing"
98

109
"code.gitea.io/gitea/models/migrations/base"
@@ -44,7 +43,7 @@ func Test_AddUniqueIndexForProjectIssue(t *testing.T) {
4443
for _, index := range tables[0].Indexes {
4544
if index.Type == schemas.UniqueType {
4645
found = true
47-
slices.Equal(index.Cols, []string{"project_id", "issue_id"})
46+
assert.ElementsMatch(t, index.Cols, []string{"project_id", "issue_id"})
4847
break
4948
}
5049
}

modules/public/public.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,4 @@ func servePublicAsset(w http.ResponseWriter, req *http.Request, fi os.FileInfo,
110110
}
111111
}
112112
http.ServeContent(w, req, fi.Name(), modtime, content)
113-
return
114113
}

routers/private/hook_verification_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ func TestVerifyCommits(t *testing.T) {
1818
unittest.PrepareTestEnv(t)
1919

2020
gitRepo, err := git.OpenRepository(t.Context(), testReposDir+"repo1_hook_verification")
21-
defer gitRepo.Close()
21+
if err != nil {
22+
defer gitRepo.Close()
23+
}
2224
assert.NoError(t, err)
2325

2426
objectFormat, err := gitRepo.GetObjectFormat()

services/packages/arch/vercmp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func parseEVR(evr string) (epoch, version, release string) {
3535
func compareSegments(a, b []string) int {
3636
lenA, lenB := len(a), len(b)
3737
l := min(lenA, lenB)
38-
for i := 0; i < l; i++ {
38+
for i := range l {
3939
if r := compare(a[i], b[i]); r != 0 {
4040
return r
4141
}

tools/lint-go-gopls.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ IGNORE_PATTERNS=(
1111
# current absolute path, indicating a error was found. This is necessary
1212
# because the tool does not set non-zero exit code when errors are found.
1313
# ref: https://github.com/golang/go/issues/67078
14-
ERROR_LINES=$("$GO" run "$GOPLS_PACKAGE" check "$@" 2>/dev/null | grep -E "^$PWD" | grep -vFf <(printf '%s\n' "${IGNORE_PATTERNS[@]}"));
14+
ERROR_LINES=$("$GO" run "$GOPLS_PACKAGE" check -severity=warning "$@" 2>/dev/null | grep -E "^$PWD" | grep -vFf <(printf '%s\n' "${IGNORE_PATTERNS[@]}"));
1515
NUM_ERRORS=$(echo -n "$ERROR_LINES" | wc -l)
1616

1717
if [ "$NUM_ERRORS" -eq "0" ]; then

0 commit comments

Comments
 (0)