Skip to content

Commit 42dcb5f

Browse files
committed
Go: Better handle pre-release versions
1 parent 057ee85 commit 42dcb5f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,15 @@ func getEnvGoSemVer() string {
9696
if !strings.HasPrefix(goVersion, "go") {
9797
log.Fatalf("Expected 'go version' output of the form 'go1.2.3'; got '%s'", goVersion)
9898
}
99-
return "v" + goVersion[2:]
99+
// Go versions don't follow the SemVer format, but the only exception we normally care about
100+
// is release candidates; so this is a horrible hack to convert e.g. `go1.22rc1` into `go1.22-rc1`
101+
// which is compatible with the SemVer specification
102+
rcIndex := strings.Index(goVersion, "rc")
103+
if rcIndex != -1 {
104+
return semver.Canonical("v"+goVersion[2:rcIndex]) + "-" + goVersion[rcIndex:]
105+
} else {
106+
return "v" + goVersion[2:]
107+
}
100108
}
101109

102110
// Returns the import path of the package being built, or "" if it cannot be determined.
@@ -787,10 +795,11 @@ func installDependenciesAndBuild() {
787795
}
788796

789797
goVersionInfo := tryReadGoDirective(buildInfo)
798+
canonEnvSemVer := semver.Canonical(getEnvGoSemVer())
790799

791800
// This diagnostic is not required if the system Go version is 1.21 or greater, since the
792801
// Go tooling should install required Go versions as needed.
793-
if semver.Compare(getEnvGoSemVer(), "v1.21.0") < 0 && goVersionInfo.Found && semver.Compare("v"+goVersionInfo.Version, getEnvGoSemVer()) > 0 {
802+
if semver.Compare(canonEnvSemVer, "v1.21.0") < 0 && goVersionInfo.Found && semver.Compare("v"+goVersionInfo.Version, canonEnvSemVer) > 0 {
794803
diagnostics.EmitNewerGoVersionNeeded()
795804
}
796805

0 commit comments

Comments
 (0)