From 6b5f74294f03ed852bbbf0016edc239387448b7e Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Sun, 1 Dec 2024 14:42:57 +0100 Subject: [PATCH] fix: improve Go version detection inside workspace (#5179) --- go.mod | 4 +- pkg/config/config.go | 45 +++++++++++++++++-- pkg/golinters/gomodguard/testdata/go.mod | 8 ++++ pkg/golinters/gomodguard/testdata/go.sum | 6 +++ .../gomodguard/testdata/gomodguard.yml | 4 +- 5 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 pkg/golinters/gomodguard/testdata/go.mod create mode 100644 pkg/golinters/gomodguard/testdata/go.sum diff --git a/go.mod b/go.mod index b93f2dbd04ce..c0d43026e322 100644 --- a/go.mod +++ b/go.mod @@ -67,6 +67,7 @@ require ( github.com/kyoh86/exportloopref v0.1.11 github.com/lasiar/canonicalheader v1.1.2 github.com/ldez/gomoddirectives v0.4.2 + github.com/ldez/grignotin v0.6.0 github.com/ldez/tagliatelle v0.6.0 github.com/ldez/usetesting v0.2.0 github.com/leonklingele/grouper v1.1.2 @@ -128,6 +129,7 @@ require ( go-simpler.org/sloglint v0.7.2 go.uber.org/automaxprocs v1.6.0 golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 + golang.org/x/mod v0.22.0 golang.org/x/sys v0.27.0 golang.org/x/tools v0.27.0 gopkg.in/yaml.v3 v3.0.1 @@ -164,7 +166,6 @@ require ( github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/ldez/grignotin v0.6.0 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect github.com/magiconair/properties v1.8.6 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -197,7 +198,6 @@ require ( go.uber.org/multierr v1.6.0 // indirect go.uber.org/zap v1.24.0 // indirect golang.org/x/exp/typeparams v0.0.0-20241108190413-2d47ceb2692f // indirect - golang.org/x/mod v0.22.0 // indirect golang.org/x/sync v0.9.0 // indirect golang.org/x/text v0.18.0 // indirect google.golang.org/protobuf v1.34.2 // indirect diff --git a/pkg/config/config.go b/pkg/config/config.go index b863b329f931..f69035278afd 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,11 +1,16 @@ package config import ( + "cmp" + "fmt" "os" + "path/filepath" + "slices" "strings" hcversion "github.com/hashicorp/go-version" - "github.com/ldez/gomoddirectives" + "github.com/ldez/grignotin/gomod" + "golang.org/x/mod/modfile" ) // Config encapsulates the config data specified in the golangci-lint YAML config file. @@ -93,8 +98,33 @@ func detectGoVersion() string { // else it returns `go` version if present, // else it returns empty. func detectGoVersionFromGoMod() string { - file, _ := gomoddirectives.GetModuleFile() - if file == nil { + info, err := gomod.GetModuleInfo() + if err != nil { + return "" + } + + wd, err := os.Getwd() + if err != nil { + return "" + } + + slices.SortFunc(info, func(a, b gomod.ModInfo) int { + return cmp.Compare(len(b.Path), len(a.Path)) + }) + + goMod := info[0] + for _, m := range info { + if !strings.HasPrefix(wd, m.Dir) { + continue + } + + goMod = m + + break + } + + file, err := parseGoMod(goMod.GoMod) + if err != nil { return "" } @@ -110,3 +140,12 @@ func detectGoVersionFromGoMod() string { return "" } + +func parseGoMod(goMod string) (*modfile.File, error) { + raw, err := os.ReadFile(filepath.Clean(goMod)) + if err != nil { + return nil, fmt.Errorf("reading go.mod file: %w", err) + } + + return modfile.Parse("go.mod", raw, nil) +} diff --git a/pkg/golinters/gomodguard/testdata/go.mod b/pkg/golinters/gomodguard/testdata/go.mod new file mode 100644 index 000000000000..ab174ed19807 --- /dev/null +++ b/pkg/golinters/gomodguard/testdata/go.mod @@ -0,0 +1,8 @@ +module gomodguard + +go 1.22.0 + +require ( + golang.org/x/mod v0.22.0 + gopkg.in/yaml.v3 v3.0.1 +) diff --git a/pkg/golinters/gomodguard/testdata/go.sum b/pkg/golinters/gomodguard/testdata/go.sum new file mode 100644 index 000000000000..7c483c93b9e0 --- /dev/null +++ b/pkg/golinters/gomodguard/testdata/go.sum @@ -0,0 +1,6 @@ +golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= +golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/golinters/gomodguard/testdata/gomodguard.yml b/pkg/golinters/gomodguard/testdata/gomodguard.yml index ca21af62599b..d193142cfc09 100644 --- a/pkg/golinters/gomodguard/testdata/gomodguard.yml +++ b/pkg/golinters/gomodguard/testdata/gomodguard.yml @@ -1,8 +1,8 @@ linters-settings: gomodguard: allowed: - modules: # List of allowed modules - - golang.org/x/mod/modfile + modules: # List of allowed modules + - golang.org/x/mod blocked: modules: # List of blocked modules - gopkg.in/yaml.v3: # Blocked module