cmd/go: set dependency go versions for go install --mod=readonly mod@vers

When running go install --mod=readonly module@version. modfetch.GoSumFile
was not set, so the checksumOk check that's done when checking whether
we need to set the GoVersion from the go mod file was failing. Bypass
the checksumOk check when there's no GoSumFile.

For #54908

Change-Id: I56cf9d36a505b1223e6bf82a7d455746e2f09849
Reviewed-on: https://go-review.googlesource.com/c/go/+/439855
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Michael Matloob 2022-10-06 16:14:47 -04:00
parent b6e7e16208
commit 2e0b97dde6
5 changed files with 58 additions and 2 deletions

View File

@ -319,7 +319,7 @@ func moduleInfo(ctx context.Context, rs *Requirements, m module.Version, mode Li
}
checksumOk := func(suffix string) bool {
return rs == nil || m.Version == "" || cfg.BuildMod == "mod" ||
return rs == nil || m.Version == "" || !mustHaveSums() ||
modfetch.HaveSum(module.Version{Path: m.Path, Version: m.Version + suffix})
}

View File

@ -714,7 +714,7 @@ func fetch(ctx context.Context, mod module.Version) (dir string, isLocal bool, e
mod = r
}
if HasModRoot() && cfg.BuildMod == "readonly" && !inWorkspaceMode() && !modfetch.HaveSum(mod) {
if mustHaveSums() && !modfetch.HaveSum(mod) {
return "", false, module.VersionError(mod, &sumMissingError{})
}
@ -722,6 +722,12 @@ func fetch(ctx context.Context, mod module.Version) (dir string, isLocal bool, e
return dir, false, err
}
// mustHaveSums reports whether we require that all checksums
// needed to load or build packages are already present in the go.sum file.
func mustHaveSums() bool {
return HasModRoot() && cfg.BuildMod == "readonly" && !inWorkspaceMode()
}
type sumMissingError struct {
suggestion string
}

View File

@ -0,0 +1,23 @@
example.com/depends/on/generics v1.0.0
written by hand
-- .mod --
module example.com/depends/on/generics
go 1.18
require example.com/generics v1.0.0
-- .info --
{"Version":"v1.0.0"}
-- go.mod --
module example.com/depends/on/generics
go 1.18
require example.com/generics v1.0.0
-- main.go --
package main
import "example.com/generics"
func main() {generics.Bar()}

View File

@ -0,0 +1,21 @@
example.com/generics v1.0.0
written by hand
-- .mod --
module example.com/generics
go 1.18
-- .info --
{"Version":"v1.0.0"}
-- go.mod --
module example.com/generics
go 1.18
-- generics.go --
package generics
type Int interface {
~int
}
func Bar() {}

View File

@ -0,0 +1,6 @@
# Regression test for Issue #54908. When running a go install module@version
# with --mod=readonly moduleInfo was not setting the GoVersion for the module
# because the checksumOk function was failing because modfetch.GoSumFile
# was not set when running outside of a module.
go install --mod=readonly example.com/depends/on/generics@v1.0.0