Debian Codegraph @

gopls/internal/util/goversion/goversion.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package goversions defines gopls's policy for which versions of Go it supports.
6 package goversion
7
8 import (
9 "fmt"
10 "strings"
11 )
12
13 // Support holds information about end-of-life Go version support.
14 //
15 // Exposed for testing.
16 type Support struct {
17 // GoVersion is the Go version to which these settings relate.
18 GoVersion int
19
20 // DeprecatedVersion is the first version of gopls that no longer supports
21 // this Go version.
22 //
23 // If unset, the version is already deprecated.
24 DeprecatedVersion string
25
26 // InstallGoplsVersion is the latest gopls version that supports this Go
27 // version without warnings.
28 InstallGoplsVersion string
29 }
30
31 // Supported maps Go versions to the gopls version in which support will
32 // be deprecated, and the final gopls version supporting them without warnings.
33 //
34 // Must be sorted in ascending order of Go version.
35 //
36 // Exposed (and mutable) for testing.
37 var Supported = []Support{
38 {12, "", "v0.7.5"},
39 {15, "", "v0.9.5"},
40 {16, "", "v0.11.0"},
41 {17, "", "v0.11.0"},
42 {18, "", "v0.14.2"},
43 {19, "v0.17.0", "v0.15.3"},
44 {20, "v0.17.0", "v0.15.3"},
45 }
46
47 // OldestSupported is the last X in Go 1.X that this version of gopls
48 // supports without warnings.
49 //
50 // Exported for testing.
51 func OldestSupported() int {
52 return Supported[len(Supported)-1].GoVersion + 1
53 }
54
55 // Message returns the message to display if the user has the given Go
56 // version, if any. The goVersion variable is the X in Go 1.X. If
57 // fromBuild is set, the Go version is the version used to build
58 // gopls. Otherwise, it is the go command version.
59 //
60 // The second component of the result indicates whether the message is
61 // an error, not a mere warning.
62 //
63 // If goVersion is invalid (< 0), it returns "", false.
64 func Message(goVersion int, fromBuild bool) (string, bool) {
65 if goVersion < 0 {
66 return "", false
67 }
68
69 for _, v := range Supported {
70 if goVersion <= v.GoVersion {
71 var msgBuilder strings.Builder
72
73 isError := true
74 if fromBuild {
75 fmt.Fprintf(&msgBuilder, "Gopls was built with Go version 1.%d", goVersion)
76 } else {
77 fmt.Fprintf(&msgBuilder, "Found Go version 1.%d", goVersion)
78 }
79 if v.DeprecatedVersion != "" {
80 // not deprecated yet, just a warning
81 fmt.Fprintf(&msgBuilder, ", which will be unsupported by gopls %s. ", v.DeprecatedVersion)
82 isError = false // warning
83 } else {
84 fmt.Fprint(&msgBuilder, ", which is not supported by this version of gopls. ")
85 }
86 fmt.Fprintf(&msgBuilder, "Please upgrade to Go 1.%d or later and reinstall gopls. ", OldestSupported())
87 fmt.Fprintf(&msgBuilder, "If you can't upgrade and want this message to go away, please install gopls %s. ", v.InstallGoplsVersion)
88 fmt.Fprint(&msgBuilder, "See https://go.dev/s/gopls-support-policy for more details.")
89
90 return msgBuilder.String(), isError
91 }
92 }
93 return "", false
94 }