Skip to content

Commit 4de4f35

Browse files
authored
Merge pull request github#12957 from owen-mc/go/autobuilder-identify-environment
Go: Add `go-autobuilder --identify-environment`
2 parents f29db40 + 841db15 commit 4de4f35

File tree

9 files changed

+414
-70
lines changed

9 files changed

+414
-70
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 272 additions & 55 deletions
Large diffs are not rendered by default.

go/extractor/cli/go-autobuilder/go-autobuilder_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,56 @@ func TestParseGoVersion(t *testing.T) {
3333
}
3434
}
3535
}
36+
37+
func TestGetVersionToInstall(t *testing.T) {
38+
tests := map[versionInfo]string{
39+
// checkForUnsupportedVersions()
40+
41+
// go.mod version below minGoVersion
42+
{"0.0", true, "1.20.3", true}: "",
43+
{"0.0", true, "9999.0", true}: "",
44+
{"0.0", true, "1.2.2", true}: "",
45+
{"0.0", true, "", false}: "",
46+
// go.mod version above maxGoVersion
47+
{"9999.0", true, "1.20.3", true}: "",
48+
{"9999.0", true, "9999.0.1", true}: "",
49+
{"9999.0", true, "1.1", true}: "",
50+
{"9999.0", true, "", false}: "",
51+
// Go installation found with version below minGoVersion
52+
{"1.20", true, "1.2.2", true}: "",
53+
{"1.11", true, "1.2.2", true}: "",
54+
{"", false, "1.2.2", true}: "",
55+
// Go installation found with version above maxGoVersion
56+
{"1.20", true, "9999.0.1", true}: "",
57+
{"1.11", true, "9999.0.1", true}: "",
58+
{"", false, "9999.0.1", true}: "",
59+
60+
// checkForVersionsNotFound()
61+
62+
// Go installation not found, go.mod version in supported range
63+
{"1.20", true, "", false}: "1.20",
64+
{"1.11", true, "", false}: "1.11",
65+
// Go installation not found, go.mod not found
66+
{"", false, "", false}: maxGoVersion,
67+
// Go installation found with version in supported range, go.mod not found
68+
{"", false, "1.11.13", true}: "",
69+
{"", false, "1.20.3", true}: "",
70+
71+
// compareVersions()
72+
73+
// Go installation found with version in supported range, go.mod version in supported range and go.mod version > go installation version
74+
{"1.20", true, "1.11.13", true}: "1.20",
75+
{"1.20", true, "1.12", true}: "1.20",
76+
// Go installation found with version in supported range, go.mod version in supported range and go.mod version <= go installation version
77+
// (Note comparisons ignore the patch version)
78+
{"1.11", true, "1.20", true}: "",
79+
{"1.11", true, "1.20.3", true}: "",
80+
{"1.20", true, "1.20.3", true}: "",
81+
}
82+
for input, expected := range tests {
83+
_, actual := getVersionToInstall(input)
84+
if actual != expected {
85+
t.Errorf("Expected getVersionToInstall(\"%s\") to be \"%s\", but got \"%s\".", input, expected, actual)
86+
}
87+
}
88+
}

go/extractor/cli/go-bootstrap/go-bootstrap.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"log"
76
"os"
87
"regexp"
@@ -22,7 +21,7 @@ func main() {
2221
buildSteps := os.Args[2]
2322

2423
haveRepo := false
25-
content, err := ioutil.ReadFile(vars)
24+
content, err := os.ReadFile(vars)
2625
if err != nil {
2726
log.Fatal(err)
2827
}
@@ -34,7 +33,7 @@ func main() {
3433
additionalVars += "SEMMLE_REPO_URL=${repository}\n"
3534
}
3635
content = append(content, []byte(additionalVars)...)
37-
err = ioutil.WriteFile(vars, content, 0644)
36+
err = os.WriteFile(vars, content, 0644)
3837
if err != nil {
3938
log.Fatal(err)
4039
}
@@ -47,7 +46,7 @@ func main() {
4746
<build export="%s">${semmle_dist}/language-packs/go/tools/platform/${semmle_platform}/bin/go-autobuilder</build>
4847
</autoupdate>
4948
`, export))
50-
err = ioutil.WriteFile(buildSteps, content, 0644)
49+
err = os.WriteFile(buildSteps, content, 0644)
5150
if err != nil {
5251
log.Fatal(err)
5352
}

go/extractor/cli/go-tokenizer/go-tokenizer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"go/scanner"
88
"go/token"
9-
"io/ioutil"
109
"log"
1110
"os"
1211
"strings"
@@ -20,7 +19,7 @@ func main() {
2019
defer csv.Flush()
2120

2221
for _, fileName := range flag.Args() {
23-
src, err := ioutil.ReadFile(fileName)
22+
src, err := os.ReadFile(fileName)
2423
if err != nil {
2524
log.Fatalf("Unable to read file %s.", fileName)
2625
}

go/extractor/diagnostics/diagnostics.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type visibilityStruct struct {
3030
}
3131

3232
var fullVisibility *visibilityStruct = &visibilityStruct{true, true, true}
33+
var telemetryOnly *visibilityStruct = &visibilityStruct{false, false, true}
3334

3435
type locationStruct struct {
3536
File string `json:"file,omitempty"`
@@ -192,3 +193,80 @@ func EmitRelativeImportPaths() {
192193
noLocation,
193194
)
194195
}
196+
197+
func EmitUnsupportedVersionGoMod(msg string) {
198+
emitDiagnostic(
199+
"go/autobuilder/env-unsupported-version-in-go-mod",
200+
"Unsupported Go version in `go.mod` file",
201+
msg,
202+
severityNote,
203+
telemetryOnly,
204+
noLocation,
205+
)
206+
}
207+
208+
func EmitUnsupportedVersionEnvironment(msg string) {
209+
emitDiagnostic(
210+
"go/autobuilder/env-unsupported-version-in-environment",
211+
"Unsupported Go version in environment",
212+
msg,
213+
severityNote,
214+
telemetryOnly,
215+
noLocation,
216+
)
217+
}
218+
219+
func EmitNoGoModAndNoGoEnv(msg string) {
220+
emitDiagnostic(
221+
"go/autobuilder/env-no-go-mod-and-no-go-env",
222+
"No `go.mod` file found and no Go version in environment",
223+
msg,
224+
severityNote,
225+
telemetryOnly,
226+
noLocation,
227+
)
228+
}
229+
230+
func EmitNoGoEnv(msg string) {
231+
emitDiagnostic(
232+
"go/autobuilder/env-no-go-env",
233+
"No Go version in environment",
234+
msg,
235+
severityNote,
236+
telemetryOnly,
237+
noLocation,
238+
)
239+
}
240+
241+
func EmitNoGoMod(msg string) {
242+
emitDiagnostic(
243+
"go/autobuilder/env-no-go-mod",
244+
"No `go.mod` file found",
245+
msg,
246+
severityNote,
247+
telemetryOnly,
248+
noLocation,
249+
)
250+
}
251+
252+
func EmitVersionGoModHigherVersionEnvironment(msg string) {
253+
emitDiagnostic(
254+
"go/autobuilder/env-version-go-mod-higher-than-go-env",
255+
"The Go version in `go.mod` file is higher than the Go version in environment",
256+
msg,
257+
severityNote,
258+
telemetryOnly,
259+
noLocation,
260+
)
261+
}
262+
263+
func EmitVersionGoModNotHigherVersionEnvironment(msg string) {
264+
emitDiagnostic(
265+
"go/autobuilder/env-version-go-mod-lower-than-or-equal-to-go-env",
266+
"The Go version in `go.mod` file is lower than or equal to the Go version in environment",
267+
msg,
268+
severityNote,
269+
telemetryOnly,
270+
noLocation,
271+
)
272+
}

go/extractor/extractor.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"go/token"
1111
"go/types"
1212
"io"
13-
"io/ioutil"
1413
"log"
1514
"os"
1615
"path/filepath"
@@ -1807,7 +1806,7 @@ func extractNumLines(tw *trap.Writer, fileName string, ast *ast.File) {
18071806

18081807
// count lines of code by tokenizing
18091808
linesOfCode := 0
1810-
src, err := ioutil.ReadFile(fileName)
1809+
src, err := os.ReadFile(fileName)
18111810
if err != nil {
18121811
log.Fatalf("Unable to read file %s.", fileName)
18131812
}

go/extractor/gomodextractor.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package extractor
22

33
import (
44
"fmt"
5-
"golang.org/x/mod/modfile"
6-
"io/ioutil"
5+
"io"
76
"log"
87
"os"
98
"path/filepath"
109
"strings"
1110

11+
"golang.org/x/mod/modfile"
12+
1213
"github.com/github/codeql-go/extractor/dbscheme"
1314
"github.com/github/codeql-go/extractor/srcarchive"
1415
"github.com/github/codeql-go/extractor/trap"
@@ -45,7 +46,7 @@ func (extraction *Extraction) extractGoMod(path string) error {
4546
if err != nil {
4647
return fmt.Errorf("failed to open go.mod file %s: %s", path, err.Error())
4748
}
48-
data, err := ioutil.ReadAll(file)
49+
data, err := io.ReadAll(file)
4950
if err != nil {
5051
return fmt.Errorf("failed to read go.mod file %s: %s", path, err.Error())
5152
}

go/extractor/srcarchive/projectlayout_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package srcarchive
22

33
import (
4-
"io/ioutil"
54
"os"
65
"testing"
76
)
87

98
func mkProjectLayout(projectLayoutSource string, t *testing.T) (*ProjectLayout, error) {
10-
pt, err := ioutil.TempFile("", "path-transformer")
9+
pt, err := os.CreateTemp("", "path-transformer")
1110
if err != nil {
1211
t.Fatalf("Unable to create temporary file for project layout: %s", err.Error())
1312
}

go/extractor/trap/trapwriter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"go/ast"
99
"go/types"
10-
"io/ioutil"
1110
"os"
1211
"path/filepath"
1312
"unicode/utf8"
@@ -51,7 +50,7 @@ func NewWriter(path string, pkg *packages.Package) (*Writer, error) {
5150
if err != nil {
5251
return nil, err
5352
}
54-
tmpFile, err := ioutil.TempFile(trapFileDir, filepath.Base(trapFilePath))
53+
tmpFile, err := os.CreateTemp(trapFileDir, filepath.Base(trapFilePath))
5554
if err != nil {
5655
return nil, err
5756
}

0 commit comments

Comments
 (0)