Skip to content

Commit aaebd52

Browse files
committed
fix: improve Go version detection inside workspace
1 parent d40b6da commit aaebd52

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ require (
6767
github.com/kyoh86/exportloopref v0.1.11
6868
github.com/lasiar/canonicalheader v1.1.2
6969
github.com/ldez/gomoddirectives v0.4.2
70+
github.com/ldez/grignotin v0.6.0
7071
github.com/ldez/tagliatelle v0.6.0
7172
github.com/ldez/usetesting v0.2.0
7273
github.com/leonklingele/grouper v1.1.2
@@ -128,6 +129,7 @@ require (
128129
go-simpler.org/sloglint v0.7.2
129130
go.uber.org/automaxprocs v1.6.0
130131
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
132+
golang.org/x/mod v0.22.0
131133
golang.org/x/sys v0.27.0
132134
golang.org/x/tools v0.27.0
133135
gopkg.in/yaml.v3 v3.0.1
@@ -164,7 +166,6 @@ require (
164166
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
165167
github.com/hashicorp/hcl v1.0.0 // indirect
166168
github.com/inconshreveable/mousetrap v1.1.0 // indirect
167-
github.com/ldez/grignotin v0.6.0 // indirect
168169
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
169170
github.com/magiconair/properties v1.8.6 // indirect
170171
github.com/mattn/go-isatty v0.0.20 // indirect
@@ -197,7 +198,6 @@ require (
197198
go.uber.org/multierr v1.6.0 // indirect
198199
go.uber.org/zap v1.24.0 // indirect
199200
golang.org/x/exp/typeparams v0.0.0-20241108190413-2d47ceb2692f // indirect
200-
golang.org/x/mod v0.22.0 // indirect
201201
golang.org/x/sync v0.9.0 // indirect
202202
golang.org/x/text v0.18.0 // indirect
203203
google.golang.org/protobuf v1.34.2 // indirect

pkg/config/config.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package config
22

33
import (
4+
"cmp"
5+
"fmt"
46
"os"
7+
"path/filepath"
8+
"slices"
59
"strings"
610

711
hcversion "github.com/hashicorp/go-version"
8-
"github.com/ldez/gomoddirectives"
12+
"github.com/ldez/grignotin/gomod"
13+
"golang.org/x/mod/modfile"
914
)
1015

1116
// Config encapsulates the config data specified in the golangci-lint YAML config file.
@@ -93,8 +98,33 @@ func detectGoVersion() string {
9398
// else it returns `go` version if present,
9499
// else it returns empty.
95100
func detectGoVersionFromGoMod() string {
96-
file, _ := gomoddirectives.GetModuleFile()
97-
if file == nil {
101+
info, err := gomod.GetModuleInfo()
102+
if err != nil {
103+
return ""
104+
}
105+
106+
wd, err := os.Getwd()
107+
if err != nil {
108+
return ""
109+
}
110+
111+
slices.SortFunc(info, func(a, b gomod.ModInfo) int {
112+
return cmp.Compare(len(b.Path), len(a.Path))
113+
})
114+
115+
goMod := info[0]
116+
for _, m := range info {
117+
if !strings.HasPrefix(wd, m.Dir) {
118+
continue
119+
}
120+
121+
goMod = m
122+
123+
break
124+
}
125+
126+
file, err := parseGoMod(goMod.GoMod)
127+
if err != nil {
98128
return ""
99129
}
100130

@@ -110,3 +140,12 @@ func detectGoVersionFromGoMod() string {
110140

111141
return ""
112142
}
143+
144+
func parseGoMod(goMod string) (*modfile.File, error) {
145+
raw, err := os.ReadFile(filepath.Clean(goMod))
146+
if err != nil {
147+
return nil, fmt.Errorf("reading go.mod file: %w", err)
148+
}
149+
150+
return modfile.Parse("go.mod", raw, nil)
151+
}

0 commit comments

Comments
 (0)