@@ -13,7 +13,6 @@ import (
13
13
"github.com/github/codeql-go/extractor/toolchain"
14
14
"github.com/github/codeql-go/extractor/util"
15
15
"golang.org/x/mod/modfile"
16
- "golang.org/x/mod/semver"
17
16
)
18
17
19
18
// DependencyInstallerMode is an enum describing how dependencies should be installed
@@ -49,53 +48,47 @@ type GoWorkspace struct {
49
48
}
50
49
51
50
// Represents a nullable version string.
52
- type GoVersionInfo struct {
53
- // The version string, if any
54
- Version string
55
- // A value indicating whether a version string was found
56
- Found bool
57
- }
51
+ type GoVersionInfo = util.SemVer
58
52
59
53
// Determines the version of Go that is required by this workspace. This is, in order of preference:
60
54
// 1. The Go version specified in the `go.work` file, if any.
61
55
// 2. The greatest Go version specified in any `go.mod` file, if any.
62
- func (workspace * GoWorkspace ) RequiredGoVersion () GoVersionInfo {
56
+ func (workspace * GoWorkspace ) RequiredGoVersion () util. SemVer {
63
57
if workspace .WorkspaceFile != nil && workspace .WorkspaceFile .Go != nil {
64
58
// If we have parsed a `go.work` file, return the version number from it.
65
- return GoVersionInfo { Version : workspace .WorkspaceFile .Go .Version , Found : true }
59
+ return util . NewSemVer ( workspace .WorkspaceFile .Go .Version )
66
60
} else if workspace .Modules != nil && len (workspace .Modules ) > 0 {
67
61
// Otherwise, if we have `go.work` files, find the greatest Go version in those.
68
- var greatestVersion string = ""
62
+ var greatestVersion util. SemVer = nil
69
63
for _ , module := range workspace .Modules {
70
64
if module .Module != nil && module .Module .Go != nil {
71
65
// If we have parsed the file, retrieve the version number we have already obtained.
72
- if greatestVersion == "" || semver .Compare ("v" + module .Module .Go .Version , "v" + greatestVersion ) > 0 {
73
- greatestVersion = module .Module .Go .Version
66
+ modVersion := util .NewSemVer (module .Module .Go .Version )
67
+ if greatestVersion == nil || modVersion .IsNewerThan (greatestVersion ) {
68
+ greatestVersion = modVersion
74
69
}
75
70
} else {
76
71
modVersion := tryReadGoDirective (module .Path )
77
- if modVersion . Found && (greatestVersion == "" || semver . Compare ( "v" + modVersion .Version , "v" + greatestVersion ) > 0 ) {
78
- greatestVersion = modVersion . Version
72
+ if modVersion != nil && (greatestVersion == nil || modVersion .IsNewerThan ( greatestVersion )) {
73
+ greatestVersion = modVersion
79
74
}
80
75
}
81
76
}
82
77
83
78
// If we have found some version, return it.
84
- if greatestVersion != "" {
85
- return GoVersionInfo {Version : greatestVersion , Found : true }
86
- }
79
+ return greatestVersion
87
80
}
88
81
89
- return GoVersionInfo { Version : "" , Found : false }
82
+ return nil
90
83
}
91
84
92
85
// Finds the greatest Go version required by any of the given `workspaces`.
93
86
// Returns a `GoVersionInfo` value with `Found: false` if no version information is available.
94
- func RequiredGoVersion (workspaces * []GoWorkspace ) GoVersionInfo {
95
- greatestGoVersion := GoVersionInfo { Version : "" , Found : false }
87
+ func RequiredGoVersion (workspaces * []GoWorkspace ) util. SemVer {
88
+ var greatestGoVersion util. SemVer = nil
96
89
for _ , workspace := range * workspaces {
97
90
goVersionInfo := workspace .RequiredGoVersion ()
98
- if goVersionInfo . Found && (! greatestGoVersion . Found || semver . Compare ( "v" + goVersionInfo .Version , "v" + greatestGoVersion . Version ) > 0 ) {
91
+ if goVersionInfo != nil && (greatestGoVersion == nil || goVersionInfo .IsNewerThan ( greatestGoVersion ) ) {
99
92
greatestGoVersion = goVersionInfo
100
93
}
101
94
}
@@ -182,8 +175,9 @@ var toolchainVersionRe *regexp.Regexp = regexp.MustCompile(`(?m)^([0-9]+\.[0-9]+
182
175
// Returns true if the `go.mod` file specifies a Go language version, that version is `1.21` or greater, and
183
176
// there is no `toolchain` directive, and the Go language version is not a valid toolchain version.
184
177
func hasInvalidToolchainVersion (modFile * modfile.File ) bool {
178
+ v1_21 := util .NewSemVer ("v1.21.0" )
185
179
return modFile .Toolchain == nil && modFile .Go != nil &&
186
- ! toolchainVersionRe .Match ([]byte (modFile .Go .Version )) && semver . Compare ( "v" + modFile .Go .Version , "v1.21.0" ) >= 0
180
+ ! toolchainVersionRe .Match ([]byte (modFile .Go .Version )) && util . NewSemVer ( modFile .Go .Version ). IsAtLeast ( v1_21 )
187
181
}
188
182
189
183
// Given a list of `go.mod` file paths, try to parse them all. The resulting array of `GoModule` objects
@@ -537,17 +531,15 @@ const (
537
531
538
532
// argsForGoVersion returns the arguments to pass to the Go compiler for the given `ModMode` and
539
533
// Go version
540
- func (m ModMode ) ArgsForGoVersion (version string ) []string {
534
+ func (m ModMode ) ArgsForGoVersion (version util. SemVer ) []string {
541
535
switch m {
542
536
case ModUnset :
543
537
return []string {}
544
538
case ModReadonly :
545
539
return []string {"-mod=readonly" }
546
540
case ModMod :
547
- if ! semver .IsValid (version ) {
548
- log .Fatalf ("Invalid Go semver: '%s'" , version )
549
- }
550
- if semver .Compare (version , "v1.14" ) < 0 {
541
+ v1_14 := util .NewSemVer ("v1.14" )
542
+ if version .IsOlderThan (v1_14 ) {
551
543
return []string {} // -mod=mod is the default behaviour for go <= 1.13, and is not accepted as an argument
552
544
} else {
553
545
return []string {"-mod=mod" }
@@ -574,7 +566,7 @@ func getModMode(depMode DependencyInstallerMode, baseDir string) ModMode {
574
566
575
567
// Tries to open `go.mod` and read a go directive, returning the version and whether it was found.
576
568
// The version string is returned in the "1.2.3" format.
577
- func tryReadGoDirective (path string ) GoVersionInfo {
569
+ func tryReadGoDirective (path string ) util. SemVer {
578
570
versionRe := regexp .MustCompile (`(?m)^go[ \t\r]+([0-9]+\.[0-9]+(\.[0-9]+)?)` )
579
571
goMod , err := os .ReadFile (path )
580
572
if err != nil {
@@ -583,9 +575,9 @@ func tryReadGoDirective(path string) GoVersionInfo {
583
575
matches := versionRe .FindSubmatch (goMod )
584
576
if matches != nil {
585
577
if len (matches ) > 1 {
586
- return GoVersionInfo { string (matches [1 ]), true }
578
+ return util . NewSemVer ( string (matches [1 ]))
587
579
}
588
580
}
589
581
}
590
- return GoVersionInfo { "" , false }
582
+ return nil
591
583
}
0 commit comments