Skip to content

Commit 350cc3b

Browse files
authored
build: Make module-relative "go list -m" compatible with GOWORK (#651)
**What problem does this PR solve?**: When GOWORK is enabled, "go list -m" always lists all modules in the workspace. To list the module for the current working directory, we disable GOWORK. This is sound whether or not you use Go workspaces. **Before** ```shell > pwd /capi-runtime-extensions/api > go env GOWORK /capi-runtime-extensions/go.work > go list -m github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/docs github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/external/caaph github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/external/capa github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/external/capx ``` **After** ```shell > pwd /capi-runtime-extensions/api > GOWORK=off go list -m github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api ``` **Which issue(s) this PR fixes**: Fixes # **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> **Special notes for your reviewer**: <!-- Use this to provide any additional information to the reviewers. This may include: - Best way to review the PR. - Where the author wants the most review attention on. - etc. --> It seems like `go list` is working as designed. Here's a vscode-go maintainer [recommending](golang/go#50745 (comment)) `GOWORK=off` when "module separation" is needed.
1 parent 3e3407a commit 350cc3b

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

make/apis.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ api.sync.%: ; $(info $(M) syncing external API: $(PROVIDER_MODULE_$*)/$(PROVIDER
5151
--exclude='*webhook*.go' \
5252
--exclude='*test.go' \
5353
--exclude='s3bucket.go' \
54-
$$(cd $(PROVIDER_MODULE_DIR) && go list -m -f '{{ .Dir }}' $(PROVIDER_MODULE_$*))/$(PROVIDER_API_PATH_$*)/$(PROVIDER_API_VERSION_$*)/*.go \
54+
$$(cd $(PROVIDER_MODULE_DIR) && GOWORK=off go list -m -f '{{ .Dir }}' $(PROVIDER_MODULE_$*))/$(PROVIDER_API_PATH_$*)/$(PROVIDER_API_VERSION_$*)/*.go \
5555
$(PROVIDER_API_DIR)
5656
find $(PROVIDER_API_DIR) -type d -exec chmod 0755 {} \;
5757
find $(PROVIDER_API_DIR) -type f -exec chmod 0644 {} \;

make/clusterctl.mk

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Copyright 2023 Nutanix. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
export CAPI_VERSION := $(shell go list -m -f '{{ .Version }}' sigs.k8s.io/cluster-api)
5-
export CAPD_VERSION := $(shell go list -m -f '{{ .Version }}' sigs.k8s.io/cluster-api/test)
6-
export CAPA_VERSION := $(shell cd hack/third-party/capa && go list -m -f '{{ .Version }}' sigs.k8s.io/cluster-api-provider-aws/v2)
7-
export CAPX_VERSION := $(shell cd hack/third-party/capx && go list -m -f '{{ .Version }}' github.com/nutanix-cloud-native/cluster-api-provider-nutanix)
8-
export CAAPH_VERSION := $(shell cd hack/third-party/caaph && go list -m -f '{{ .Version }}' sigs.k8s.io/cluster-api-addon-provider-helm)
4+
export CAPI_VERSION := $(shell GOWORK=off go list -m -f '{{ .Version }}' sigs.k8s.io/cluster-api)
5+
export CAPD_VERSION := $(shell GOWORK=off go list -m -f '{{ .Version }}' sigs.k8s.io/cluster-api/test)
6+
export CAPA_VERSION := $(shell cd hack/third-party/capa && GOWORK=off go list -m -f '{{ .Version }}' sigs.k8s.io/cluster-api-provider-aws/v2)
7+
export CAPX_VERSION := $(shell cd hack/third-party/capx && GOWORK=off go list -m -f '{{ .Version }}' github.com/nutanix-cloud-native/cluster-api-provider-nutanix)
8+
export CAAPH_VERSION := $(shell cd hack/third-party/caaph && GOWORK=off go list -m -f '{{ .Version }}' sigs.k8s.io/cluster-api-addon-provider-helm)
99

1010
# Leave Nutanix credentials empty here and set it when creating the clusters
1111
.PHONY: clusterctl.init

make/go.mk

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ endif
137137
.PHONY: lint.%
138138
lint.%: ## Runs golangci-lint for a specific module
139139
lint.%: ; $(info $(M) linting $* module)
140-
$(if $(filter-out root,$*),cd $* && )golines -w $$(go list -tags e2e ./... | grep -v external | sed "s|^$$(go list -m)|.|")
140+
$(if $(filter-out root,$*),cd $* && )golines -w $$(GOWORK=off go list -tags e2e ./... | grep -v external | sed "s|^$$(GOWORK=off go list -m)|.|")
141141
$(if $(filter-out root,$*),cd $* && )golangci-lint run --fix --config=$(GOLANGCI_CONFIG_FILE)
142-
$(if $(filter-out root,$*),cd $* && )golines -w $$(go list -tags e2e ./... | grep -v external | sed "s|^$$(go list -m)|.|")
142+
$(if $(filter-out root,$*),cd $* && )golines -w $$(GOWORK=off go list -tags e2e ./... | grep -v external | sed "s|^$$(GOWORK=off go list -m)|.|")
143143

144144
.PHONY: mod-tidy
145145
mod-tidy: ## Run go mod tidy for all modules

test/helpers/envtest.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11+
"os"
1112
"os/exec"
1213
"path/filepath"
1314
goruntime "runtime"
@@ -200,6 +201,7 @@ func getFilePathsToCAPICRDs() []string {
200201
func getModulePath(moduleDir, moduleName string) string {
201202
cmd := exec.Command("go", "list", "-m", "-f", "{{ .Dir }}", moduleName)
202203
cmd.Dir = moduleDir
204+
cmd.Env = append(os.Environ(), "GOWORK=off")
203205
out, err := cmd.CombinedOutput()
204206
if err != nil {
205207
// We include the combined output because the error is usually

0 commit comments

Comments
 (0)