Skip to content

Commit bb3101e

Browse files
authored
Merge pull request github#13022 from owen-mc/go/identify-environment-fixes
Go: `go-autobuilder --identify-environment` fixes
2 parents b09772e + 011c927 commit bb3101e

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,6 @@ func getDepMode() DependencyInstallerMode {
261261

262262
// Tries to open `go.mod` and read a go directive, returning the version and whether it was found.
263263
func tryReadGoDirective(depMode DependencyInstallerMode) (string, bool) {
264-
version := ""
265-
found := false
266264
if depMode == GoGetWithModules {
267265
versionRe := regexp.MustCompile(`(?m)^go[ \t\r]+([0-9]+\.[0-9]+)$`)
268266
goMod, err := os.ReadFile("go.mod")
@@ -271,18 +269,13 @@ func tryReadGoDirective(depMode DependencyInstallerMode) (string, bool) {
271269
} else {
272270
matches := versionRe.FindSubmatch(goMod)
273271
if matches != nil {
274-
found = true
275272
if len(matches) > 1 {
276-
version := string(matches[1])
277-
semverVersion := "v" + version
278-
if semver.Compare(semverVersion, getEnvGoSemVer()) >= 0 {
279-
diagnostics.EmitNewerGoVersionNeeded()
280-
}
273+
return string(matches[1]), true
281274
}
282275
}
283276
}
284277
}
285-
return version, found
278+
return "", false
286279
}
287280

288281
// Returns the appropriate ModMode for the current project
@@ -664,7 +657,11 @@ func installDependenciesAndBuild() {
664657
os.Setenv("GO111MODULE", "auto")
665658
}
666659

667-
_, goModVersionFound := tryReadGoDirective(depMode)
660+
goModVersion, goModVersionFound := tryReadGoDirective(depMode)
661+
662+
if semver.Compare("v"+goModVersion, getEnvGoSemVer()) >= 0 {
663+
diagnostics.EmitNewerGoVersionNeeded()
664+
}
668665

669666
modMode := getModMode(depMode)
670667
modMode = fixGoVendorIssues(modMode, depMode, goModVersionFound)
@@ -736,42 +733,43 @@ func outsideSupportedRange(version string) bool {
736733
func checkForUnsupportedVersions(v versionInfo) (msg, version string) {
737734
if v.goModVersionFound && outsideSupportedRange(v.goModVersion) {
738735
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion +
739-
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion + ")."
736+
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion +
737+
"). Writing an environment file not specifying any version of Go."
740738
version = ""
741739
diagnostics.EmitUnsupportedVersionGoMod(msg)
742-
}
743-
744-
if v.goEnVersionFound && outsideSupportedRange(v.goEnvVersion) {
740+
} else if v.goEnvVersionFound && outsideSupportedRange(v.goEnvVersion) {
745741
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
746-
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion + ")."
742+
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion +
743+
"). Writing an environment file not specifying any version of Go."
747744
version = ""
748745
diagnostics.EmitUnsupportedVersionEnvironment(msg)
749746
}
750747

751748
return msg, version
752749
}
753750

754-
// Check if either `v.goEnVersionFound` or `v.goModVersionFound` are false. If so, emit
751+
// Check if either `v.goEnvVersionFound` or `v.goModVersionFound` are false. If so, emit
755752
// a diagnostic and return the version to install, or the empty string if we should not attempt to
756753
// install a version of Go. We assume that `checkForUnsupportedVersions` has already been
757754
// called, so any versions that are found are within the supported range.
758755
func checkForVersionsNotFound(v versionInfo) (msg, version string) {
759-
if !v.goEnVersionFound && !v.goModVersionFound {
756+
if !v.goEnvVersionFound && !v.goModVersionFound {
760757
msg = "No version of Go installed and no `go.mod` file found. Writing an environment " +
761758
"file specifying the maximum supported version of Go (" + maxGoVersion + ")."
762759
version = maxGoVersion
763760
diagnostics.EmitNoGoModAndNoGoEnv(msg)
764761
}
765762

766-
if !v.goEnVersionFound && v.goModVersionFound {
763+
if !v.goEnvVersionFound && v.goModVersionFound {
767764
msg = "No version of Go installed. Writing an environment file specifying the version " +
768765
"of Go found in the `go.mod` file (" + v.goModVersion + ")."
769766
version = v.goModVersion
770767
diagnostics.EmitNoGoEnv(msg)
771768
}
772769

773-
if v.goEnVersionFound && !v.goModVersionFound {
774-
msg = "No `go.mod` file found. Version " + v.goEnvVersion + " installed in the environment."
770+
if v.goEnvVersionFound && !v.goModVersionFound {
771+
msg = "No `go.mod` file found. Version " + v.goEnvVersion + " installed in the " +
772+
"environment. Writing an environment file not specifying any version of Go."
775773
version = ""
776774
diagnostics.EmitNoGoMod(msg)
777775
}
@@ -787,13 +785,14 @@ func compareVersions(v versionInfo) (msg, version string) {
787785
if semver.Compare("v"+v.goModVersion, "v"+v.goEnvVersion) > 0 {
788786
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
789787
") is lower than the version found in the `go.mod` file (" + v.goModVersion +
790-
").\nWriting an environment file specifying the version of Go from the `go.mod` " +
788+
"). Writing an environment file specifying the version of Go from the `go.mod` " +
791789
"file (" + v.goModVersion + ")."
792790
version = v.goModVersion
793791
diagnostics.EmitVersionGoModHigherVersionEnvironment(msg)
794792
} else {
795793
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
796-
") is high enough for the version found in the `go.mod` file (" + v.goModVersion + ")."
794+
") is high enough for the version found in the `go.mod` file (" + v.goModVersion +
795+
"). Writing an environment file not specifying any version of Go."
797796
version = ""
798797
diagnostics.EmitVersionGoModNotHigherVersionEnvironment(msg)
799798
}
@@ -859,13 +858,13 @@ type versionInfo struct {
859858
goModVersion string // The version of Go found in the go directive in the `go.mod` file.
860859
goModVersionFound bool // Whether a `go` directive was found in the `go.mod` file.
861860
goEnvVersion string // The version of Go found in the environment.
862-
goEnVersionFound bool // Whether an installation of Go was found in the environment.
861+
goEnvVersionFound bool // Whether an installation of Go was found in the environment.
863862
}
864863

865864
func (v versionInfo) String() string {
866865
return fmt.Sprintf(
867866
"go.mod version: %s, go.mod directive found: %t, go env version: %s, go installation found: %t",
868-
v.goModVersion, v.goModVersionFound, v.goEnvVersion, v.goEnVersionFound)
867+
v.goModVersion, v.goModVersionFound, v.goEnvVersion, v.goEnvVersionFound)
869868
}
870869

871870
// Check if Go is installed in the environment.
@@ -880,8 +879,8 @@ func identifyEnvironment() {
880879
depMode := getDepMode()
881880
v.goModVersion, v.goModVersionFound = tryReadGoDirective(depMode)
882881

883-
v.goEnVersionFound = isGoInstalled()
884-
if v.goEnVersionFound {
882+
v.goEnvVersionFound = isGoInstalled()
883+
if v.goEnvVersionFound {
885884
v.goEnvVersion = getEnvGoVersion()[2:]
886885
}
887886

0 commit comments

Comments
 (0)