]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modload: fix bug in error message
authorMichael Matloob <matloob@golang.org>
Thu, 23 May 2024 19:00:49 +0000 (15:00 -0400)
committerMichael Matloob <matloob@golang.org>
Fri, 24 May 2024 15:54:40 +0000 (15:54 +0000)
CL 513756 moved the usage of modGo after it was set to its proper value,
so the error message text always listed the version of go as
"unspecified". Fix the error message in the case where the version was
set in the go.mod, and provide an error message where it wasn't.

Fixes #67587

Change-Id: I763f6be7ee811da32fcb7e785682fd6f48145981
Reviewed-on: https://go-review.googlesource.com/c/go/+/588075
Reviewed-by: Sam Thanawalla <samthanawalla@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/list_buildmod_reason_issue67587.txt [new file with mode: 0644]

index 89eeb5c71a3f6f50d43cde9ee7eaffd1a8e6b352..4ba1bf98ee65d3799116d493359aee051fea3292 100644 (file)
@@ -1472,13 +1472,12 @@ func setDefaultBuildMod() {
                        vendorDir = filepath.Join(modRoots[0], "vendor")
                }
                if fi, err := fsys.Stat(vendorDir); err == nil && fi.IsDir() {
-                       modGo := "unspecified"
                        if goVersion != "" {
                                if gover.Compare(goVersion, "1.14") < 0 {
                                        // The go version is less than 1.14. Don't set -mod=vendor by default.
                                        // Since a vendor directory exists, we should record why we didn't use it.
                                        // This message won't normally be shown, but it may appear with import errors.
-                                       cfg.BuildModReason = fmt.Sprintf("Go version in "+versionSource+" is %s, so vendor directory was not used.", modGo)
+                                       cfg.BuildModReason = fmt.Sprintf("Go version in "+versionSource+" is %s, so vendor directory was not used.", goVersion)
                                } else {
                                        vendoredWorkspace, err := modulesTextIsForWorkspace(vendorDir)
                                        if err != nil {
@@ -1499,9 +1498,9 @@ func setDefaultBuildMod() {
                                                return
                                        }
                                }
-                               modGo = goVersion
+                       } else {
+                               cfg.BuildModReason = fmt.Sprintf("Go version in " + versionSource + " is unspecified, so vendor directory was not used.")
                        }
-
                }
        }
 
diff --git a/src/cmd/go/testdata/script/list_buildmod_reason_issue67587.txt b/src/cmd/go/testdata/script/list_buildmod_reason_issue67587.txt
new file mode 100644 (file)
index 0000000..a89f0e4
--- /dev/null
@@ -0,0 +1,26 @@
+cd thirteen
+! go list -deps
+stderr         '(Go version in go.mod is 1.13, so vendor directory was not used.)'
+
+cd ../unspecified
+! go list -deps
+stderr         '(Go version in go.mod is unspecified, so vendor directory was not used.)'
+
+-- thirteen/foo.go --
+package foo
+
+import _ "github.com/foo/bar"
+-- thirteen/go.mod --
+module example.com
+
+go 1.13
+-- thirteen/vendor/github.com/foo/bar/bar.go --
+package bar
+-- unspecified/foo.go --
+package foo
+
+import _ "github.com/foo/bar"
+-- unspecified/go.mod --
+module example.com
+-- unspecified/vendor/github.com/foo/bar/bar.go --
+package bar
\ No newline at end of file