Skip to content

Commit 12f996f

Browse files
committed
Deal better with goModVersion < minGoVersion
1 parent 32b5df6 commit 12f996f

File tree

1 file changed

+86
-40
lines changed

1 file changed

+86
-40
lines changed

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

Lines changed: 86 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -719,12 +719,23 @@ func installDependenciesAndBuild() {
719719
const minGoVersion = "1.11"
720720
const maxGoVersion = "1.20"
721721

722+
// Check if `version` is lower than `minGoVersion`. Note that for this comparison we ignore the
723+
// patch part of the version, so 1.20.1 and 1.20 are considered equal.
724+
func belowSupportedRange(version string) bool {
725+
return semver.Compare(semver.MajorMinor("v"+version), "v"+minGoVersion) < 0
726+
}
727+
728+
// Check if `version` is higher than `maxGoVersion`. Note that for this comparison we ignore the
729+
// patch part of the version, so 1.20.1 and 1.20 are considered equal.
730+
func aboveSupportedRange(version string) bool {
731+
return semver.Compare(semver.MajorMinor("v"+version), "v"+maxGoVersion) > 0
732+
}
733+
722734
// Check if `version` is lower than `minGoVersion` or higher than `maxGoVersion`. Note that for
723735
// this comparison we ignore the patch part of the version, so 1.20.1 and 1.20 are considered
724736
// equal.
725737
func outsideSupportedRange(version string) bool {
726-
short := semver.MajorMinor("v" + version)
727-
return semver.Compare(short, "v"+minGoVersion) < 0 || semver.Compare(short, "v"+maxGoVersion) > 0
738+
return belowSupportedRange(version) || aboveSupportedRange(version)
728739
}
729740

730741
// Assuming `v.goModVersionFound` is false, emit a diagnostic and return the version to install,
@@ -765,49 +776,84 @@ func checkForGoModVersionNotFound(v versionInfo) (msg, version string) {
765776
// Assuming `v.goModVersionFound` is true, emit a diagnostic and return the version to install,
766777
// or the empty string if we should not attempt to install a version of Go.
767778
func checkForGoModVersionFound(v versionInfo) (msg, version string) {
768-
if outsideSupportedRange(v.goModVersion) {
769-
// The project is intended to be built with a version of Go that is not supported.
770-
// We do not install a version of Go.
779+
if aboveSupportedRange(v.goModVersion) {
780+
// The project is intended to be built with a version of Go that is above the supported
781+
// range. We do not install a version of Go.
771782
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion +
772-
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion +
783+
") is above the supported range (" + minGoVersion + "-" + maxGoVersion +
773784
"). Writing an environment file not specifying any version of Go."
774785
version = ""
775786
diagnostics.EmitUnsupportedVersionGoMod(msg)
776-
} else if !v.goEnvVersionFound {
777-
// There is no Go version installed. The version in the `go.mod` file is supported.
778-
// We install the version from the `go.mod` file.
779-
msg = "No version of Go installed. Writing an environment file specifying the version " +
780-
"of Go found in the `go.mod` file (" + v.goModVersion + ")."
781-
version = v.goModVersion
782-
diagnostics.EmitNoGoEnv(msg)
783-
} else if outsideSupportedRange(v.goEnvVersion) {
784-
// The version of Go that is installed is outside of the supported range. The version in
785-
// the `go.mod` file is supported. We install the version from the `go.mod` file.
786-
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
787-
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion + "). " +
788-
"Writing an environment file specifying the version of Go from the `go.mod` file (" +
789-
v.goModVersion + ")."
790-
version = v.goModVersion
791-
diagnostics.EmitVersionGoModSupportedAndGoEnvUnsupported(msg)
792-
} else if semver.Compare("v"+v.goModVersion, "v"+v.goEnvVersion) > 0 {
793-
// The version of Go that is installed is supported. The version in the `go.mod` file is
794-
// supported and is higher than the version that is installed. We install the version from
795-
// the `go.mod` file.
796-
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
797-
") is lower than the version found in the `go.mod` file (" + v.goModVersion +
798-
"). Writing an environment file specifying the version of Go from the `go.mod` " +
799-
"file (" + v.goModVersion + ")."
800-
version = v.goModVersion
801-
diagnostics.EmitVersionGoModHigherVersionEnvironment(msg)
787+
} else if belowSupportedRange(v.goModVersion) {
788+
if !v.goEnvVersionFound {
789+
// There is no Go version installed. The version in the `go.mod` file is below the
790+
// supported range. Go versions are generally backwards compatible, so we install the
791+
// minimum supported version.
792+
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion +
793+
") is below the supported range (" + minGoVersion + "-" + maxGoVersion +
794+
"). No version of Go installed. Writing an environment file specifying the " +
795+
"minimum supported version of Go (" + minGoVersion + ")."
796+
version = minGoVersion
797+
diagnostics.EmitNoGoEnv(msg)
798+
} else if outsideSupportedRange(v.goEnvVersion) {
799+
// The version of Go that is installed is outside of the supported range. The version
800+
// in the `go.mod` file is below the supported range. Go versions are generally
801+
// backwards compatible, so we install the minimum supported version.
802+
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion +
803+
") is below the supported range (" + minGoVersion + "-" + maxGoVersion +
804+
"). The version of Go installed in the environment (" + v.goEnvVersion +
805+
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion + "). " +
806+
"Writing an environment file specifying the minimum supported version of Go (" +
807+
minGoVersion + ")."
808+
version = minGoVersion
809+
diagnostics.EmitVersionGoModSupportedAndGoEnvUnsupported(msg)
810+
} else {
811+
// The version of Go that is installed is supported. The version in the `go.mod` file is
812+
// below the supported range. We do not install a version of Go.
813+
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
814+
") is supported and is high enough for the version found in the `go.mod` file (" +
815+
v.goModVersion + "). Writing an environment file not specifying any version of Go."
816+
version = ""
817+
diagnostics.EmitVersionGoModNotHigherVersionEnvironment(msg)
818+
}
802819
} else {
803-
// The version of Go that is installed is supported. The version in the `go.mod` file is
804-
// supported and is lower than or equal to the version that is installed. We do not install
805-
// a version of Go.
806-
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
807-
") is supported and is high enough for the version found in the `go.mod` file (" +
808-
v.goModVersion + "). Writing an environment file not specifying any version of Go."
809-
version = ""
810-
diagnostics.EmitVersionGoModNotHigherVersionEnvironment(msg)
820+
// v.goModVersion is within the supported range.
821+
if !v.goEnvVersionFound {
822+
// There is no Go version installed. The version in the `go.mod` file is supported.
823+
// We install the version from the `go.mod` file.
824+
msg = "No version of Go installed. Writing an environment file specifying the version " +
825+
"of Go found in the `go.mod` file (" + v.goModVersion + ")."
826+
version = v.goModVersion
827+
diagnostics.EmitNoGoEnv(msg)
828+
} else if outsideSupportedRange(v.goEnvVersion) {
829+
// The version of Go that is installed is outside of the supported range. The version in
830+
// the `go.mod` file is supported. We install the version from the `go.mod` file.
831+
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
832+
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion + "). " +
833+
"Writing an environment file specifying the version of Go from the `go.mod` file (" +
834+
v.goModVersion + ")."
835+
version = v.goModVersion
836+
diagnostics.EmitVersionGoModSupportedAndGoEnvUnsupported(msg)
837+
} else if semver.Compare("v"+v.goModVersion, "v"+v.goEnvVersion) > 0 {
838+
// The version of Go that is installed is supported. The version in the `go.mod` file is
839+
// supported and is higher than the version that is installed. We install the version from
840+
// the `go.mod` file.
841+
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
842+
") is lower than the version found in the `go.mod` file (" + v.goModVersion +
843+
"). Writing an environment file specifying the version of Go from the `go.mod` " +
844+
"file (" + v.goModVersion + ")."
845+
version = v.goModVersion
846+
diagnostics.EmitVersionGoModHigherVersionEnvironment(msg)
847+
} else {
848+
// The version of Go that is installed is supported. The version in the `go.mod` file is
849+
// supported and is lower than or equal to the version that is installed. We do not install
850+
// a version of Go.
851+
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
852+
") is supported and is high enough for the version found in the `go.mod` file (" +
853+
v.goModVersion + "). Writing an environment file not specifying any version of Go."
854+
version = ""
855+
diagnostics.EmitVersionGoModNotHigherVersionEnvironment(msg)
856+
}
811857
}
812858

813859
return msg, version

0 commit comments

Comments
 (0)