Currently, without a go.mod file, gopls assumes the latest version of go regardless of what's installed. This can cause problems where gopls recommendations can break code. I ran into this recently with the following MWE on go 1.24.6:

MWE code
package main
​
import (
    "fmt"
    "sync"
)
​
func doit(i int) {
    fmt.Println("i = ", i)
}
​
func main() {
    var wg sync.WaitGroup
    for i := range 5 {
        wg.Add(1)
        go func() {
            defer wg.Done()
            doit(i)
        }()
    }
​
    wg.Wait()
}

using

gopls:  /opt/homebrew/bin/gopls (version: (devel) built with go: go1.24.5)`
golang.org/x/tools/gopls v0.20.0

with no go.mod file.

gopls recommended I use waitgroup.Go in this code (part of modernization), but this method was introduced in 1.25. Since I don't have go 1.25 installed (nor are any of my tools compiled with 1.25), implementing the recommendation resulted in broken code. With a go.mod file restricting the version to 1.24, the suggestion does not appear.

In the absence of a go.mod file, would it be possible to check the version of go on $PATH and limit recommendations to that version?

Comment From: gopherbot

Change https://go.dev/cl/695935 mentions this issue: gopls/internal/cache: set goVersion when there is no module version

Comment From: gabyhelp

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

Comment From: sbromberger

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

I'm not sure this is strictly related but I don't know which emoji to use to vote "unhelpful".

Comment From: findleyr

@sbromberger the 👎 emoji works :)