-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathbuild-environment.go
More file actions
291 lines (261 loc) · 15.6 KB
/
build-environment.go
File metadata and controls
291 lines (261 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package autobuilder
import (
"fmt"
"log"
"os"
"github.com/github/codeql-go/extractor/diagnostics"
"github.com/github/codeql-go/extractor/project"
"github.com/github/codeql-go/extractor/toolchain"
"github.com/github/codeql-go/extractor/util"
)
var minGoVersion = util.NewSemVer("1.11")
// This can be increased before a new minor version has been released, based on testing with the release candidates.
var maxGoVersion = util.NewSemVer("1.26")
// This should be the maximum supported version which has been released.
var goVersionToInstall = util.NewSemVer("1.26")
type versionInfo struct {
goModVersion util.SemVer // The version of Go found in the go directive in the `go.mod` file.
goEnvVersion util.SemVer // The version of Go found in the environment.
}
func (v versionInfo) String() string {
return fmt.Sprintf(
"go.mod version: %s, go env version: %s",
v.goModVersion, v.goEnvVersion)
}
// Check if `version` is lower than `minGoVersion`. Note that for this comparison we ignore the
// patch part of the version, so 1.20.1 and 1.20 are considered equal.
func belowSupportedRange(version util.SemVer) bool {
return version.MajorMinor().IsOlderThan(minGoVersion.MajorMinor())
}
// Check if `version` is higher than `maxGoVersion`. Note that for this comparison we ignore the
// patch part of the version, so 1.20.1 and 1.20 are considered equal.
func aboveSupportedRange(version util.SemVer) bool {
return version.MajorMinor().IsNewerThan(maxGoVersion.MajorMinor())
}
// Check if `version` is lower than `minGoVersion` or higher than `maxGoVersion`. Note that for
// this comparison we ignore the patch part of the version, so 1.20.1 and 1.20 are considered
// equal.
func outsideSupportedRange(version util.SemVer) bool {
return belowSupportedRange(version) || aboveSupportedRange(version)
}
// Assuming `v.goModVersionFound` is false, emit a diagnostic and return the version to install,
// or the empty string if we should not attempt to install a version of Go.
func getVersionWhenGoModVersionNotFound(v versionInfo) (msg string, version util.SemVer) {
if v.goEnvVersion == nil {
// There is no Go version installed in the environment. We have no indication which version
// was intended to be used to build this project. Go versions are generally backwards
// compatible, so we install the maximum supported released version.
msg = "No version of Go installed and no `go.mod` file found. Requesting the maximum " +
"supported version of Go that has been released (" + goVersionToInstall.String() + ")."
version = goVersionToInstall
diagnostics.EmitNoGoModAndNoGoEnv(msg)
} else if outsideSupportedRange(v.goEnvVersion) {
// The Go version installed in the environment is not supported. We have no indication
// which version was intended to be used to build this project. Go versions are generally
// backwards compatible, so we install the maximum supported released version.
msg = "No `go.mod` file found. The version of Go installed in the environment (" +
v.goEnvVersion.String() + ") is outside of the supported range (" + minGoVersion.String() + "-" +
maxGoVersion.String() + "). Requesting the maximum supported version of Go that has " +
"been released (" + goVersionToInstall.String() + ")."
version = goVersionToInstall
diagnostics.EmitNoGoModAndGoEnvUnsupported(msg)
} else {
// The version of Go that is installed is supported. We have no indication which version
// was intended to be used to build this project. We assume that the installed version is
// suitable and do not install a version of Go.
msg = "No `go.mod` file found. Version " + v.goEnvVersion.String() + " installed in the " +
"environment is supported. Not requesting any version of Go."
version = nil
diagnostics.EmitNoGoModAndGoEnvSupported(msg)
}
return msg, version
}
// Assuming `v.goModVersion` is above the supported range, emit a diagnostic and return the
// version to install, or the empty string if we should not attempt to install a version of Go.
func getVersionWhenGoModVersionTooHigh(v versionInfo) (msg string, version util.SemVer) {
if v.goEnvVersion == nil {
// The version in the `go.mod` file is above the supported range. There is no Go version
// installed. We install the maximum supported version as a best effort.
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion.String() +
") is above the supported range (" + minGoVersion.String() + "-" + maxGoVersion.String() +
"). No version of Go installed. Requesting the maximum supported version of Go that has " +
"been released (" + goVersionToInstall.String() + ")."
version = goVersionToInstall
diagnostics.EmitGoModVersionTooHighAndNoGoEnv(msg)
} else if aboveSupportedRange(v.goEnvVersion) {
// The version in the `go.mod` file is above the supported range. The version of Go that
// is installed is above the supported range. We do not install a version of Go.
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion.String() +
") is above the supported range (" + minGoVersion.String() + "-" + maxGoVersion.String() +
"). The version of Go installed in the environment (" + v.goEnvVersion.String() +
") is above the supported range (" + minGoVersion.String() + "-" + maxGoVersion.String() +
"). Not requesting any version of Go."
version = nil
diagnostics.EmitGoModVersionTooHighAndEnvVersionTooHigh(msg)
} else if belowSupportedRange(v.goEnvVersion) {
// The version in the `go.mod` file is above the supported range. The version of Go that
// is installed is below the supported range. We install the maximum supported version as
// a best effort.
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion.String() +
") is above the supported range (" + minGoVersion.String() + "-" + maxGoVersion.String() +
"). The version of Go installed in the environment (" + v.goEnvVersion.String() +
") is below the supported range (" + minGoVersion.String() + "-" + maxGoVersion.String() +
"). Requesting the maximum supported version of Go that has been released (" + goVersionToInstall.String() + ")."
version = goVersionToInstall
diagnostics.EmitGoModVersionTooHighAndEnvVersionTooLow(msg)
} else if maxGoVersion.IsNewerThan(v.goEnvVersion) {
// The version in the `go.mod` file is above the supported range. The version of Go that
// is installed is supported and below the maximum supported version. We install the
// maximum supported version as a best effort.
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion.String() +
") is above the supported range (" + minGoVersion.String() + "-" + maxGoVersion.String() +
"). The version of Go installed in the environment (" + v.goEnvVersion.String() +
") is below the maximum supported version (" + maxGoVersion.String() +
"). Requesting the maximum supported version of Go that has been released (" + goVersionToInstall.String() + ")."
version = goVersionToInstall
diagnostics.EmitGoModVersionTooHighAndEnvVersionBelowMax(msg)
} else {
// The version in the `go.mod` file is above the supported range. The version of Go that
// is installed is the maximum supported version. We do not install a version of Go.
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion.String() +
") is above the supported range (" + minGoVersion.String() + "-" + maxGoVersion.String() +
"). The version of Go installed in the environment (" + v.goEnvVersion.String() +
") is the maximum supported version (" + maxGoVersion.String() +
"). Not requesting any version of Go."
version = nil
diagnostics.EmitGoModVersionTooHighAndEnvVersionMax(msg)
}
return msg, version
}
// Assuming `v.goModVersion` is below the supported range, emit a diagnostic and return the
// version to install, or the empty string if we should not attempt to install a version of Go.
func getVersionWhenGoModVersionTooLow(v versionInfo) (msg string, version util.SemVer) {
if v.goEnvVersion == nil {
// There is no Go version installed. The version in the `go.mod` file is below the
// supported range. Go versions are generally backwards compatible, so we install the
// minimum supported version.
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion.String() +
") is below the supported range (" + minGoVersion.String() + "-" + maxGoVersion.String() +
"). No version of Go installed. Requesting the minimum supported version of Go (" +
minGoVersion.String() + ")."
version = minGoVersion
diagnostics.EmitGoModVersionTooLowAndNoGoEnv(msg)
} else if outsideSupportedRange(v.goEnvVersion) {
// The version of Go that is installed is outside of the supported range. The version
// in the `go.mod` file is below the supported range. Go versions are generally
// backwards compatible, so we install the minimum supported version.
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion.String() +
") is below the supported range (" + minGoVersion.String() + "-" + maxGoVersion.String() +
"). The version of Go installed in the environment (" + v.goEnvVersion.String() +
") is outside of the supported range (" + minGoVersion.String() + "-" + maxGoVersion.String() + "). " +
"Requesting the minimum supported version of Go (" + minGoVersion.String() + ")."
version = minGoVersion
diagnostics.EmitGoModVersionTooLowAndEnvVersionUnsupported(msg)
} else {
// The version of Go that is installed is supported. The version in the `go.mod` file is
// below the supported range. We do not install a version of Go.
msg = "The version of Go installed in the environment (" + v.goEnvVersion.String() +
") is supported and is high enough for the version found in the `go.mod` file (" +
v.goModVersion.String() + "). Not requesting any version of Go."
version = nil
diagnostics.EmitGoModVersionTooLowAndEnvVersionSupported(msg)
}
return msg, version
}
// Assuming `v.goModVersion` is in the supported range, emit a diagnostic and return the version
// to install, or the empty string if we should not attempt to install a version of Go.
func getVersionWhenGoModVersionSupported(v versionInfo) (msg string, version util.SemVer) {
if v.goEnvVersion == nil {
// There is no Go version installed. The version in the `go.mod` file is supported.
// We install the version from the `go.mod` file.
msg = "No version of Go installed. Requesting the version of Go found in the `go.mod` " +
"file (" + v.goModVersion.String() + ")."
version = v.goModVersion
diagnostics.EmitGoModVersionSupportedAndNoGoEnv(msg)
} else if outsideSupportedRange(v.goEnvVersion) {
// The version of Go that is installed is outside of the supported range. The version in
// the `go.mod` file is supported. We install the version from the `go.mod` file.
msg = "The version of Go installed in the environment (" + v.goEnvVersion.String() +
") is outside of the supported range (" + minGoVersion.String() + "-" + maxGoVersion.String() + "). " +
"Requesting the version of Go from the `go.mod` file (" +
v.goModVersion.String() + ")."
version = v.goModVersion
diagnostics.EmitGoModVersionSupportedAndGoEnvUnsupported(msg)
} else if v.goModVersion.IsNewerThan(v.goEnvVersion) {
// The version of Go that is installed is supported. The version in the `go.mod` file is
// supported and is higher than the version that is installed. We install the version from
// the `go.mod` file.
msg = "The version of Go installed in the environment (" + v.goEnvVersion.String() +
") is lower than the version found in the `go.mod` file (" + v.goModVersion.String() +
"). Requesting the version of Go from the `go.mod` file (" + v.goModVersion.String() + ")."
version = v.goModVersion
diagnostics.EmitGoModVersionSupportedHigherGoEnv(msg)
} else {
// The version of Go that is installed is supported. The version in the `go.mod` file is
// supported and is lower than or equal to the version that is installed. We do not install
// a version of Go.
msg = "The version of Go installed in the environment (" + v.goEnvVersion.String() +
") is supported and is high enough for the version found in the `go.mod` file (" +
v.goModVersion.String() + "). Not requesting any version of Go."
version = nil
diagnostics.EmitGoModVersionSupportedLowerEqualGoEnv(msg)
}
return msg, version
}
// Check the versions of Go found in the environment and in the `go.mod` file, and return a
// version to install. If the version is the empty string then no installation is required.
// We never return a version of Go that is outside of the supported range.
//
// +-----------------------+-----------------------+-----------------------+-----------------------------------------------------+------------------------------------------------+
// | Found in go.mod > | *None* | *Below min supported* | *In supported range* | *Above max supported |
// | Installed \/ | | | | |
// |-----------------------|-----------------------|-----------------------|-----------------------------------------------------|------------------------------------------------|
// | *None* | Install max supported | Install min supported | Install version from go.mod | Install max supported |
// | *Below min supported* | Install max supported | Install min supported | Install version from go.mod | Install max supported |
// | *In supported range* | No action | No action | Install version from go.mod if newer than installed | Install max supported if newer than installed |
// | *Above max supported* | Install max supported | Install min supported | Install version from go.mod | No action |
// +-----------------------+-----------------------+-----------------------+-----------------------------------------------------+------------------------------------------------+
func getVersionToInstall(v versionInfo) (msg string, version util.SemVer) {
if v.goModVersion == nil {
return getVersionWhenGoModVersionNotFound(v)
}
if aboveSupportedRange(v.goModVersion) {
return getVersionWhenGoModVersionTooHigh(v)
}
if belowSupportedRange(v.goModVersion) {
return getVersionWhenGoModVersionTooLow(v)
}
return getVersionWhenGoModVersionSupported(v)
}
// Output some JSON to stdout specifying the version of Go to install, unless `version` is the
// empty string.
func outputEnvironmentJson(version util.SemVer) {
var content string
if version == nil {
content = `{ "go": {} }`
} else {
content = `{ "go": { "version": "` + version.StandardSemVer() + `" } }`
}
_, err := fmt.Fprint(os.Stdout, content)
if err != nil {
log.Println("Failed to write environment json to stdout: ")
log.Println(err)
}
}
// Get the version of Go to install and output it to stdout as json.
func IdentifyEnvironment() {
var v versionInfo
workspaces := project.GetWorkspaceInfo(false)
// Remove temporary extractor files (e.g. auto-generated go.mod files) when we are done
defer project.RemoveTemporaryExtractorFiles()
// Find the greatest Go version required by any of the workspaces.
v.goModVersion = project.RequiredGoVersion(&workspaces)
// Find which, if any, version of Go is installed on the system already.
if toolchain.IsInstalled() {
v.goEnvVersion = toolchain.GetEnvGoSemVer()
}
// Determine which version of Go we should recommend to install.
msg, versionToInstall := getVersionToInstall(v)
log.Println(msg)
outputEnvironmentJson(versionToInstall)
}