@@ -93,32 +93,15 @@ func detectGoVersion() string {
9393// else it returns `go` version if present,
9494// else it returns empty.
9595func detectGoVersionFromGoMod () string {
96- info , err := gomod .GetModuleInfo ()
97- if err != nil {
98- return ""
99- }
100-
101- wd , err := os .Getwd ()
96+ modPath , err := gomod .GetGoModPath ()
10297 if err != nil {
103- return ""
104- }
105-
106- slices .SortFunc (info , func (a , b gomod.ModInfo ) int {
107- return cmp .Compare (len (b .Path ), len (a .Path ))
108- })
109-
110- goMod := info [0 ]
111- for _ , m := range info {
112- if ! strings .HasPrefix (wd , m .Dir ) {
113- continue
98+ modPath = detectGoModFallback ()
99+ if modPath == "" {
100+ return ""
114101 }
115-
116- goMod = m
117-
118- break
119102 }
120103
121- file , err := parseGoMod (goMod . GoMod )
104+ file , err := parseGoMod (modPath )
122105 if err != nil {
123106 return ""
124107 }
@@ -144,3 +127,32 @@ func parseGoMod(goMod string) (*modfile.File, error) {
144127
145128 return modfile .Parse ("go.mod" , raw , nil )
146129}
130+
131+ func detectGoModFallback () string {
132+ info , err := gomod .GetModuleInfo ()
133+ if err != nil {
134+ return ""
135+ }
136+
137+ wd , err := os .Getwd ()
138+ if err != nil {
139+ return ""
140+ }
141+
142+ slices .SortFunc (info , func (a , b gomod.ModInfo ) int {
143+ return cmp .Compare (len (b .Path ), len (a .Path ))
144+ })
145+
146+ goMod := info [0 ]
147+ for _ , m := range info {
148+ if ! strings .HasPrefix (wd , m .Dir ) {
149+ continue
150+ }
151+
152+ goMod = m
153+
154+ break
155+ }
156+
157+ return goMod .GoMod
158+ }
0 commit comments