golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
121.19k stars 17.37k forks source link

x/tools/gopls: warn if language version is newer than compile version #68258

Open findleyr opened 1 week ago

findleyr commented 1 week ago

As suggested by @matta in #68248, we should warn when the version of Go used to compile gopls is older than the language version implied by the go.mod file. A diagnostic on the go directive of the go.mod file probably suffices.

FWIW, I reported a similar issue in staticcheck. There, the fix to build with go1.23rc1 worked as well.

@dominikh also added code to staticcheck that will report an error if the tool is used on a go module requiring versions of go newer than what the tool was built with. gopls could consider a similar check.

Originally posted by @matta in https://github.com/golang/go/issues/68248#issuecomment-2198396157

gabyhelp commented 1 week ago

Related Issues

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

dominikh commented 1 week ago

Note that Staticcheck primarily has its own logic for this to produce better error messages. go/types already complains when the asked-for language version is newer than the version of go/types, but in Go 1.22, the error message read package requires newer Go version go1.23. Starting with Go 1.23, it seems to produce a better error message: /home/dominikh/prj/src/example.com/foo.go:3:1: package requires newer Go version go1.24 (application built with go1.23).

8121604559035734c9677d5281bbdac8b1c17a1e seems to be the commit that improved the message.

matta commented 1 week ago

Will a diagnostic in go.mod be enough? In my experience, "show all workspace diagnostics" is not consistently supported (or used) in editors. They show diagnostics only for the files the editor has opened.

In the case of #68248, I built with go1.23rc1 on the command line with go build ./... and saw all was well, then opened up a .go file in the project and saw the diagnostic from gopls. At that point, I did not open go.mod to check for additional diagnostics there.

In @dominikh's example above, maybe the error would appear for every file, which would cover it, I think.