From fe9626af296f7c7893af8cff88bc4195724bea07 Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 16 Mar 2022 03:50:26 +0100 Subject: [PATCH] Use `go run` for tool dependencies, require go 1.17 (#18874) This ensures the tools only run in the versions we've tested and it also does not polute PATH with those tools so they are truly isolated. This syntax of `go run` requires go 1.17, so the minimum version is set accordingly. Fixes: https://github.com/go-gitea/gitea/issues/18867 Co-authored-by: techknowlogick --- .drone.yml | 13 ++++- .golangci.yml | 4 +- Makefile | 98 +++++++++++++++---------------------- README.md | 2 +- build/code-batch-process.go | 6 +-- docs/config.yaml | 2 +- go.mod | 2 +- 7 files changed, 59 insertions(+), 68 deletions(-) diff --git a/.drone.yml b/.drone.yml index 31706ac213d9..6e5b3da55ed4 100644 --- a/.drone.yml +++ b/.drone.yml @@ -49,11 +49,14 @@ steps: GOSUMDB: sum.golang.org TAGS: bindata sqlite sqlite_unlock_notify depends_on: [deps-backend] + volumes: + - name: deps + path: /go - name: lint-backend-windows image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env commands: - - make golangci-lint vet + - make golangci-lint-windows vet environment: GOPROXY: https://goproxy.cn # proxy.golang.org is blocked in China, this proxy is not GOSUMDB: sum.golang.org @@ -61,6 +64,9 @@ steps: GOOS: windows GOARCH: amd64 depends_on: [deps-backend] + volumes: + - name: deps + path: /go - name: lint-backend-gogit image: gitea/test_env:linux-amd64 # https://gitea.com/gitea/test-env @@ -71,6 +77,9 @@ steps: GOSUMDB: sum.golang.org TAGS: bindata gogit sqlite sqlite_unlock_notify depends_on: [deps-backend] + volumes: + - name: deps + path: /go - name: checks-frontend image: node:16 @@ -100,7 +109,7 @@ steps: depends_on: [test-frontend] - name: build-backend-no-gcc - image: golang:1.16 # this step is kept as the lowest version of golang that we support + image: golang:1.17 # this step is kept as the lowest version of golang that we support pull: always environment: GO111MODULE: on diff --git a/.golangci.yml b/.golangci.yml index 91faa75d7389..f06c9cc4804b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -23,7 +23,7 @@ linters: fast: false run: - timeout: 3m + timeout: 10m skip-dirs: - node_modules - public @@ -64,7 +64,7 @@ linters-settings: - name: modifies-value-receiver gofumpt: extra-rules: true - lang-version: 1.16 + lang-version: 1.17 issues: exclude-rules: diff --git a/Makefile b/Makefile index 74067597cb11..873ad6083290 100644 --- a/Makefile +++ b/Makefile @@ -25,9 +25,18 @@ HAS_GO = $(shell hash $(GO) > /dev/null 2>&1 && echo "GO" || echo "NOGO" ) COMMA := , XGO_VERSION := go-1.17.x -MIN_GO_VERSION := 001016000 +MIN_GO_VERSION := 001017000 MIN_NODE_VERSION := 012017000 -MIN_GOLANGCI_LINT_VERSION := 001044000 + +AIR_PACKAGE ?= github.com/cosmtrek/air@bedc18201271882c2be66d216d0e1a275b526ec4 +EDITORCONFIG_CHECKER_PACKAGE ?= github.com/editorconfig-checker/editorconfig-checker/cmd/editorconfig-checker@50adf46752da119dfef66e57be3ce2693ea4aa9c +ERRCHECK_PACKAGE ?= github.com/kisielk/errcheck@8ddee489636a8311a376fc92e27a6a13c6658344 +GOFUMPT_PACKAGE ?= mvdan.cc/gofumpt@v0.3.0 +GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/cmd/golangci-lint@v1.44.0 +GXZ_PAGAGE ?= github.com/ulikunitz/xz/cmd/gxz@v0.5.10 +MISSPELL_PACKAGE ?= github.com/client9/misspell/cmd/misspell@v0.3.4 +SWAGGER_PACKAGE ?= github.com/go-swagger/go-swagger/cmd/swagger@v0.27.0 +XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest DOCKER_IMAGE ?= gitea/gitea DOCKER_TAG ?= latest @@ -125,8 +134,6 @@ ifeq ($(filter $(TAGS_SPLIT),bindata),bindata) GO_SOURCES += $(BINDATA_DEST) endif -#To update swagger use: GO111MODULE=on go get -u github.com/go-swagger/go-swagger/cmd/swagger -SWAGGER := $(GO) run github.com/go-swagger/go-swagger/cmd/swagger SWAGGER_SPEC := templates/swagger/v1_json.tmpl SWAGGER_SPEC_S_TMPL := s|"basePath": *"/api/v1"|"basePath": "{{AppSubUrl \| JSEscape \| Safe}}/api/v1"|g SWAGGER_SPEC_S_JSON := s|"basePath": *"{{AppSubUrl \| JSEscape \| Safe}}/api/v1"|"basePath": "/api/v1"|g @@ -234,11 +241,8 @@ clean: .PHONY: fmt fmt: - @hash gofumpt > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) install mvdan.cc/gofumpt@v0.3.0; \ - fi @echo "Running gitea-fmt (with gofumpt)..." - @$(GO) run build/code-batch-process.go gitea-fmt -w '{file-list}' + @MISSPELL_PACKAGE=$(MISSPELL_PACKAGE) GOFUMPT_PACKAGE=$(GOFUMPT_PACKAGE) $(GO) run build/code-batch-process.go gitea-fmt -w '{file-list}' .PHONY: vet vet: @@ -257,7 +261,7 @@ endif .PHONY: generate-swagger generate-swagger: - $(SWAGGER) generate spec -x "$(SWAGGER_EXCLUDE)" -o './$(SWAGGER_SPEC)' + $(GO) run $(SWAGGER_PACKAGE) generate spec -x "$(SWAGGER_EXCLUDE)" -o './$(SWAGGER_SPEC)' $(SED_INPLACE) '$(SWAGGER_SPEC_S_TMPL)' './$(SWAGGER_SPEC)' $(SED_INPLACE) $(SWAGGER_NEWLINE_COMMAND) './$(SWAGGER_SPEC)' @@ -273,24 +277,18 @@ swagger-check: generate-swagger .PHONY: swagger-validate swagger-validate: $(SED_INPLACE) '$(SWAGGER_SPEC_S_JSON)' './$(SWAGGER_SPEC)' - $(SWAGGER) validate './$(SWAGGER_SPEC)' + $(GO) run $(SWAGGER_PACKAGE) validate './$(SWAGGER_SPEC)' $(SED_INPLACE) '$(SWAGGER_SPEC_S_TMPL)' './$(SWAGGER_SPEC)' .PHONY: errcheck errcheck: - @hash errcheck > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) install github.com/kisielk/errcheck@8ddee489636a8311a376fc92e27a6a13c6658344; \ - fi @echo "Running errcheck..." - @errcheck $(GO_PACKAGES) + $(GO) run $(ERRCHECK_PACKAGE) $(GO_PACKAGES) .PHONY: fmt-check fmt-check: - @hash gofumpt > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) install mvdan.cc/gofumpt@0.3.0; \ - fi # get all go files and run gitea-fmt (with gofmt) on them - @diff=$$($(GO) run build/code-batch-process.go gitea-fmt -l '{file-list}'); \ + @diff=$$(MISSPELL_PACKAGE=$(MISSPELL_PACKAGE) GOFUMPT_PACKAGE=$(GOFUMPT_PACKAGE) $(GO) run build/code-batch-process.go gitea-fmt -l '{file-list}'); \ if [ -n "$$diff" ]; then \ echo "Please run 'make fmt' and commit the result:"; \ echo "$${diff}"; \ @@ -328,10 +326,7 @@ watch-frontend: node-check node_modules .PHONY: watch-backend watch-backend: go-check - @hash air > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) install github.com/cosmtrek/air@bedc18201271882c2be66d216d0e1a275b526ec4; \ - fi - air -c .air.toml + $(GO) run $(AIR_PACKAGE) -c .air.toml .PHONY: test test: test-frontend test-backend @@ -611,12 +606,9 @@ $(DIST_DIRS): .PHONY: release-windows release-windows: | $(DIST_DIRS) - @hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) install src.techknowlogick.com/xgo@latest; \ - fi - CGO_CFLAGS="$(CGO_CFLAGS)" xgo -go $(XGO_VERSION) -buildmode exe -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'windows/*' -out gitea-$(VERSION) . + CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -buildmode exe -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'windows/*' -out gitea-$(VERSION) . ifeq (,$(findstring gogit,$(TAGS))) - CGO_CFLAGS="$(CGO_CFLAGS)" xgo -go $(XGO_VERSION) -buildmode exe -dest $(DIST)/binaries -tags 'netgo osusergo gogit $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'windows/*' -out gitea-$(VERSION)-gogit . + CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -buildmode exe -dest $(DIST)/binaries -tags 'netgo osusergo gogit $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'windows/*' -out gitea-$(VERSION)-gogit . endif ifeq ($(CI),drone) cp /build/* $(DIST)/binaries @@ -624,20 +616,14 @@ endif .PHONY: release-linux release-linux: | $(DIST_DIRS) - @hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) install src.techknowlogick.com/xgo@latest; \ - fi - CGO_CFLAGS="$(CGO_CFLAGS)" xgo -go $(XGO_VERSION) -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets '$(LINUX_ARCHS)' -out gitea-$(VERSION) . + CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets '$(LINUX_ARCHS)' -out gitea-$(VERSION) . ifeq ($(CI),drone) cp /build/* $(DIST)/binaries endif .PHONY: release-darwin release-darwin: | $(DIST_DIRS) - @hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) install src.techknowlogick.com/xgo@latest; \ - fi - CGO_CFLAGS="$(CGO_CFLAGS)" xgo -go $(XGO_VERSION) -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '$(LDFLAGS)' -targets 'darwin-10.12/amd64,darwin-10.12/arm64' -out gitea-$(VERSION) . + CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '$(LDFLAGS)' -targets 'darwin-10.12/amd64,darwin-10.12/arm64' -out gitea-$(VERSION) . ifeq ($(CI),drone) cp /build/* $(DIST)/binaries endif @@ -652,10 +638,7 @@ release-check: | $(DIST_DIRS) .PHONY: release-compress release-compress: | $(DIST_DIRS) - @hash gxz > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) install github.com/ulikunitz/xz/cmd/gxz@v0.5.10; \ - fi - cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "compressing $${file}" && gxz -k -9 $${file}; done; + cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "compressing $${file}" && $(GO) run $(GXZ_PAGAGE) -k -9 $${file}; done; .PHONY: release-sources release-sources: | $(DIST_DIRS) @@ -685,6 +668,15 @@ deps-frontend: node_modules .PHONY: deps-backend deps-backend: $(GO) mod download + $(GO) install $(AIR_PACKAGE) + $(GO) install $(EDITORCONFIG_CHECKER_PACKAGE) + $(GO) install $(ERRCHECK_PACKAGE) + $(GO) install $(GOFUMPT_PACKAGE) + $(GO) install $(GOLANGCI_LINT_PACKAGE) + $(GO) install $(GXZ_PAGAGE) + $(GO) install $(MISSPELL_PACKAGE) + $(GO) install $(SWAGGER_PACKAGE) + $(GO) install $(XGO_PACKAGE) node_modules: package-lock.json npm install --no-save @@ -778,29 +770,19 @@ pr\#%: clean-all $(GO) run contrib/pr/checkout.go $* .PHONY: golangci-lint -golangci-lint: golangci-lint-check - golangci-lint run --timeout 10m +golangci-lint: + $(GO) run $(GOLANGCI_LINT_PACKAGE) run -.PHONY: golangci-lint-check -golangci-lint-check: - $(eval GOLANGCI_LINT_VERSION := $(shell printf "%03d%03d%03d" $(shell golangci-lint --version | grep -Eo '[0-9]+\.[0-9.]+' | tr '.' ' ');)) - $(eval MIN_GOLANGCI_LINT_VER_FMT := $(shell printf "%g.%g.%g" $(shell echo $(MIN_GOLANGCI_LINT_VERSION) | grep -o ...))) - @hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - echo "Downloading golangci-lint v${MIN_GOLANGCI_LINT_VER_FMT}"; \ - export BINARY="golangci-lint"; \ - curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/v${MIN_GOLANGCI_LINT_VER_FMT}/install.sh" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VER_FMT); \ - elif [ "$(GOLANGCI_LINT_VERSION)" -lt "$(MIN_GOLANGCI_LINT_VERSION)" ]; then \ - echo "Downloading newer version of golangci-lint v${MIN_GOLANGCI_LINT_VER_FMT}"; \ - export BINARY="golangci-lint"; \ - curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/v${MIN_GOLANGCI_LINT_VER_FMT}/install.sh" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VER_FMT); \ - fi +# workaround step for the lint-backend-windows CI task because 'go run' can not +# have distinct GOOS/GOARCH for its build and run steps +.PHONY: golangci-lint-windows +golangci-lint-windows: + @GOOS= GOARCH= $(GO) install $(GOLANGCI_LINT_PACKAGE) + golangci-lint run .PHONY: editorconfig-checker editorconfig-checker: - @hash editorconfig-checker > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) install github.com/editorconfig-checker/editorconfig-checker/cmd/editorconfig-checker@50adf46752da119dfef66e57be3ce2693ea4aa9c; \ - fi - editorconfig-checker templates + $(GO) run $(EDITORCONFIG_CHECKER_PACKAGE) templates .PHONY: docker docker: diff --git a/README.md b/README.md index bbe27fab4d9c..305931b1b8cc 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ or if SQLite support is required: The `build` target is split into two sub-targets: -- `make backend` which requires [Go 1.16](https://golang.org/dl/) or greater. +- `make backend` which requires [Go 1.17](https://golang.org/dl/) or greater. - `make frontend` which requires [Node.js LTS](https://nodejs.org/en/download/) or greater and Internet connectivity to download npm dependencies. When building from the official source tarballs which include pre-built frontend files, the `frontend` target will not be triggered, making it possible to build without Node.js and Internet connectivity. diff --git a/build/code-batch-process.go b/build/code-batch-process.go index 8139fe762375..b2290af77157 100644 --- a/build/code-batch-process.go +++ b/build/code-batch-process.go @@ -40,7 +40,7 @@ func passThroughCmd(cmd string, args []string) error { } c := exec.Cmd{ Path: foundCmd, - Args: args, + Args: append([]string{cmd}, args...), Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr, @@ -271,9 +271,9 @@ func main() { log.Print("the -d option is not supported by gitea-fmt") } cmdErrors = append(cmdErrors, giteaFormatGoImports(files, containsString(subArgs, "-l"), containsString(subArgs, "-w"))) - cmdErrors = append(cmdErrors, passThroughCmd("gofumpt", append([]string{"-extra", "-lang", "1.16"}, substArgs...))) + cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("GOFUMPT_PACKAGE"), "-extra", "-lang", "1.17"}, substArgs...))) case "misspell": - cmdErrors = append(cmdErrors, passThroughCmd("misspell", substArgs)) + cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("MISSPELL_PACKAGE")}, substArgs...))) default: log.Fatalf("unknown cmd: %s %v", subCmd, subArgs) } diff --git a/docs/config.yaml b/docs/config.yaml index f2b98ba5545d..333870028cbc 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -19,7 +19,7 @@ params: author: The Gitea Authors website: https://docs.gitea.io version: 1.16.4 - minGoVersion: 1.16 + minGoVersion: 1.17 goVersion: 1.17 minNodeVersion: 12.17 diff --git a/go.mod b/go.mod index d1f4b7c4cf98..a545dacca75d 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module code.gitea.io/gitea -go 1.16 +go 1.17 require ( code.gitea.io/gitea-vet v0.2.2-0.20220122151748-48ebc902541b