cmd/go/internal/modload: fix bug in error message

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>
This commit is contained in:
Michael Matloob 2024-05-23 15:00:49 -04:00
parent 2785f4fad6
commit 378c48df3b
2 changed files with 29 additions and 4 deletions

View 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.")
}
}
}

View File

@ -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