@@ -261,8 +261,6 @@ func getDepMode() DependencyInstallerMode {
261
261
262
262
// Tries to open `go.mod` and read a go directive, returning the version and whether it was found.
263
263
func tryReadGoDirective (depMode DependencyInstallerMode ) (string , bool ) {
264
- version := ""
265
- found := false
266
264
if depMode == GoGetWithModules {
267
265
versionRe := regexp .MustCompile (`(?m)^go[ \t\r]+([0-9]+\.[0-9]+)$` )
268
266
goMod , err := os .ReadFile ("go.mod" )
@@ -271,18 +269,13 @@ func tryReadGoDirective(depMode DependencyInstallerMode) (string, bool) {
271
269
} else {
272
270
matches := versionRe .FindSubmatch (goMod )
273
271
if matches != nil {
274
- found = true
275
272
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
281
274
}
282
275
}
283
276
}
284
277
}
285
- return version , found
278
+ return "" , false
286
279
}
287
280
288
281
// Returns the appropriate ModMode for the current project
@@ -664,7 +657,11 @@ func installDependenciesAndBuild() {
664
657
os .Setenv ("GO111MODULE" , "auto" )
665
658
}
666
659
667
- _ , goModVersionFound := tryReadGoDirective (depMode )
660
+ goModVersion , goModVersionFound := tryReadGoDirective (depMode )
661
+
662
+ if semver .Compare ("v" + goModVersion , getEnvGoSemVer ()) >= 0 {
663
+ diagnostics .EmitNewerGoVersionNeeded ()
664
+ }
668
665
669
666
modMode := getModMode (depMode )
670
667
modMode = fixGoVendorIssues (modMode , depMode , goModVersionFound )
@@ -736,42 +733,43 @@ func outsideSupportedRange(version string) bool {
736
733
func checkForUnsupportedVersions (v versionInfo ) (msg , version string ) {
737
734
if v .goModVersionFound && outsideSupportedRange (v .goModVersion ) {
738
735
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."
740
738
version = ""
741
739
diagnostics .EmitUnsupportedVersionGoMod (msg )
742
- }
743
-
744
- if v .goEnVersionFound && outsideSupportedRange (v .goEnvVersion ) {
740
+ } else if v .goEnvVersionFound && outsideSupportedRange (v .goEnvVersion ) {
745
741
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."
747
744
version = ""
748
745
diagnostics .EmitUnsupportedVersionEnvironment (msg )
749
746
}
750
747
751
748
return msg , version
752
749
}
753
750
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
755
752
// a diagnostic and return the version to install, or the empty string if we should not attempt to
756
753
// install a version of Go. We assume that `checkForUnsupportedVersions` has already been
757
754
// called, so any versions that are found are within the supported range.
758
755
func checkForVersionsNotFound (v versionInfo ) (msg , version string ) {
759
- if ! v .goEnVersionFound && ! v .goModVersionFound {
756
+ if ! v .goEnvVersionFound && ! v .goModVersionFound {
760
757
msg = "No version of Go installed and no `go.mod` file found. Writing an environment " +
761
758
"file specifying the maximum supported version of Go (" + maxGoVersion + ")."
762
759
version = maxGoVersion
763
760
diagnostics .EmitNoGoModAndNoGoEnv (msg )
764
761
}
765
762
766
- if ! v .goEnVersionFound && v .goModVersionFound {
763
+ if ! v .goEnvVersionFound && v .goModVersionFound {
767
764
msg = "No version of Go installed. Writing an environment file specifying the version " +
768
765
"of Go found in the `go.mod` file (" + v .goModVersion + ")."
769
766
version = v .goModVersion
770
767
diagnostics .EmitNoGoEnv (msg )
771
768
}
772
769
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."
775
773
version = ""
776
774
diagnostics .EmitNoGoMod (msg )
777
775
}
@@ -787,13 +785,14 @@ func compareVersions(v versionInfo) (msg, version string) {
787
785
if semver .Compare ("v" + v .goModVersion , "v" + v .goEnvVersion ) > 0 {
788
786
msg = "The version of Go installed in the environment (" + v .goEnvVersion +
789
787
") is lower than the version found in the `go.mod` file (" + v .goModVersion +
790
- ").\n Writing 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` " +
791
789
"file (" + v .goModVersion + ")."
792
790
version = v .goModVersion
793
791
diagnostics .EmitVersionGoModHigherVersionEnvironment (msg )
794
792
} else {
795
793
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."
797
796
version = ""
798
797
diagnostics .EmitVersionGoModNotHigherVersionEnvironment (msg )
799
798
}
@@ -859,13 +858,13 @@ type versionInfo struct {
859
858
goModVersion string // The version of Go found in the go directive in the `go.mod` file.
860
859
goModVersionFound bool // Whether a `go` directive was found in the `go.mod` file.
861
860
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.
863
862
}
864
863
865
864
func (v versionInfo ) String () string {
866
865
return fmt .Sprintf (
867
866
"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 )
869
868
}
870
869
871
870
// Check if Go is installed in the environment.
@@ -880,8 +879,8 @@ func identifyEnvironment() {
880
879
depMode := getDepMode ()
881
880
v .goModVersion , v .goModVersionFound = tryReadGoDirective (depMode )
882
881
883
- v .goEnVersionFound = isGoInstalled ()
884
- if v .goEnVersionFound {
882
+ v .goEnvVersionFound = isGoInstalled ()
883
+ if v .goEnvVersionFound {
885
884
v .goEnvVersion = getEnvGoVersion ()[2 :]
886
885
}
887
886
0 commit comments