Skip to content

Commit 056680b

Browse files
authored
build: Consistently format even generated files (#923)
Reworked checks in pre-commit config to check if generated files are consistent after running `make go-generate`, which is called when building the release so we are confident this doesn't get broken in any future PRs. Calling golines when generating deepcopy, etc will ensure a consistent formatting for these generated files. Also reinstated go fix as this is now properly supported in recent go versions. And finally simplify the ignore external call by using golines flag rather than a grep on the go source dirs.
1 parent da32b0f commit 056680b

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ repos:
99
entry: make schema-chart
1010
language: system
1111
files: "^charts/cluster-api-runtime-extensions-nutanix/values.yaml$"
12+
- id: go-generate
13+
name: go-generate
14+
entry: make go-generate
15+
language: system
16+
files: "(.*\\.go|go.mod|go.sum|go.mk)$"
17+
pass_filenames: false
1218
- id: golangci-lint
1319
name: golangci-lint
14-
entry: make go-generate lint
20+
entry: make lint
1521
language: system
1622
files: "(.*\\.go|go.mod|go.sum|go.mk)$"
1723
pass_filenames: false

make/go.mk

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,23 @@ endif
137137

138138
.PHONY: lint.%
139139
lint.%: ## Runs golangci-lint for a specific module
140-
lint.%: ; $(info $(M) linting $* module)
141-
$(if $(filter-out root,$*),cd $* && )golines -w $$(GOWORK=off go list -tags e2e ./... | grep -v external | sed "s|^$$(GOWORK=off go list -m)|.|")
140+
lint.%: golines.% ; $(info $(M) linting $* module)
142141
$(if $(filter-out root,$*),cd $* && )golangci-lint run --fix --config=$(GOLANGCI_CONFIG_FILE)
143-
$(if $(filter-out root,$*),cd $* && )golines -w $$(GOWORK=off go list -tags e2e ./... | grep -v external | sed "s|^$$(GOWORK=off go list -m)|.|")
142+
$(MAKE) golines.$*
143+
144+
.PHONY: golines
145+
golines: ## Runs golines for all modules in repository
146+
ifneq ($(wildcard $(REPO_ROOT)/go.mod),)
147+
golines: golines.root
148+
endif
149+
ifneq ($(words $(GO_SUBMODULES_NO_DOCS)),0)
150+
golines: $(addprefix golines.,$(GO_SUBMODULES_NO_DOCS:/go.mod=))
151+
endif
152+
153+
.PHONY: golines.%
154+
golines.%: ## Runs golines for a specific module
155+
golines.%:
156+
$(if $(filter-out root,$*),cd $* && )golines -w --ignored-dirs external $$(GOWORK=off go list -tags e2e ./... | sed "s|^$$(GOWORK=off go list -m)|.|")
144157

145158
.PHONY: mod-tidy
146159
mod-tidy: ## Run go mod tidy for all modules
@@ -185,9 +198,9 @@ go-fix: $(addprefix go-fix.,$(GO_SUBMODULES_NO_DOCS:/go.mod=))
185198
endif
186199

187200
.PHONY: go-fix.%
188-
go-fix.%: ## Runs golangci-lint for a specific module
201+
go-fix.%: ## Runs go fix for a specific module
189202
go-fix.%: ; $(info $(M) go fixing $* module)
190-
$(if $(filter-out root,$*),cd $* && )go fix ./...
203+
$(if $(filter-out root,$*),cd $* && )go fix $$(GOWORK=off go list ./... | sed "s|^$$(GOWORK=off go list -m)|.|" | grep -v external)
191204

192205
.PHONY: go-generate
193206
go-generate: ## Runs go generate
@@ -210,7 +223,7 @@ go-generate: ; $(info $(M) running go generate)
210223
yq --inplace \
211224
--from-file=hack/update-webhook-configurations.yq \
212225
charts/cluster-api-runtime-extensions-nutanix/templates/webhooks.yaml
213-
#$(MAKE) go-fix
226+
$(MAKE) go-fix golines
214227
$(MAKE) configure-csi-providers
215228
# Update anyOf schemas for resource.Quantity fields to only accept strings
216229
# until CAPI ClusterClass variables support anyOf schemas.

make/make.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
MAKEFLAGS += --no-builtin-rules
55
MAKEFLAGS += --no-builtin-variables
6+
MAKEFLAGS += --no-print-directory

0 commit comments

Comments
 (0)