diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2f7c1d2d2..4e7538b0d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -167,7 +167,12 @@ jobs: uses: actions/setup-go@v6 with: go-version: 1.25.x + - name: Check formatting + run: ./scripts/gofumpt-check.sh - name: Lint + # Run even if the formatting check failed, so that both sets of + # problems are reported in a single CI run. + if: ${{ !cancelled() }} uses: golangci/golangci-lint-action@ba0d7d2ec06a0ea1cb5fa41b2e4a3ab91d21278a # v9 with: # If you change this, make sure to also update scripts/golangci-lint-shim.sh diff --git a/.golangci.yml b/.golangci.yml index c13f7b9f3..c46e438ac 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -102,7 +102,9 @@ linters: - vendor/ formatters: enable: - - gofumpt + # gofumpt is intentionally not listed here: golangci-lint bundles its own + # gofumpt version, which drifts from the one we pin in go.mod. We run that + # pinned version separately via scripts/gofumpt-check.sh instead. - goimports exclusions: generated: lax diff --git a/Makefile b/Makefile index 6b51d5250..10bea092a 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,7 @@ format: .PHONY: lint lint: + ./scripts/gofumpt-check.sh ./scripts/golangci-lint-shim.sh run # For more details about integration test, see https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md. diff --git a/justfile b/justfile index 7909cb2d6..f9351732a 100644 --- a/justfile +++ b/justfile @@ -37,6 +37,7 @@ format: go tool gofumpt -l -w . lint: + ./scripts/gofumpt-check.sh ./scripts/golangci-lint-shim.sh run e2e-test-command := "go test pkg/integration/clients/*.go" diff --git a/scripts/gofumpt-check.sh b/scripts/gofumpt-check.sh new file mode 100755 index 000000000..ff251306c --- /dev/null +++ b/scripts/gofumpt-check.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +# Checks that all Go files are gofumpt-formatted, and fails if any aren't. +# Used by `just lint`, `make lint`, and CI. We run gofumpt with the version +# pinned in go.mod (via `go tool`) rather than the one bundled with +# golangci-lint, so that formatting is identical across all of them and the +# editor. + +set -e + +script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +repo_root=$(dirname "$script_dir") + +cd "$repo_root" + +unformatted=$(go tool gofumpt -l .) +if [ -n "$unformatted" ]; then + echo "The following files are not formatted correctly:" + echo "$unformatted" + echo "Run 'just format' (or 'make format') and commit the result." + exit 1 +fi