]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: set dependency go versions for go install --mod=readonly mod@vers
authorMichael Matloob <matloob@golang.org>
Thu, 6 Oct 2022 20:14:47 +0000 (16:14 -0400)
committerMichael Matloob <matloob@golang.org>
Tue, 11 Oct 2022 20:37:36 +0000 (20:37 +0000)
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>
src/cmd/go/internal/modload/build.go
src/cmd/go/internal/modload/import.go
src/cmd/go/testdata/mod/example.com_depends_on_generics_v1.0.0.txt [new file with mode: 0644]
src/cmd/go/testdata/mod/example.com_generics_v1.0.0.txt [new file with mode: 0644]
src/cmd/go/testdata/script/install_dep_version.txt [new file with mode: 0644]

index 9381acf7983dc64ec946a2196d22285cfece43c3..30b248e65a361f753e98c4373d889cc9bf3826ee 100644 (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})
                }
 
index b314656b96a9534e229b0b827ca167f20da3d3ba..87b065630502b2b9b37fa338300e59e94057f230 100644 (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
 }
diff --git a/src/cmd/go/testdata/mod/example.com_depends_on_generics_v1.0.0.txt b/src/cmd/go/testdata/mod/example.com_depends_on_generics_v1.0.0.txt
new file mode 100644 (file)
index 0000000..80d3095
--- /dev/null
@@ -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()}
\ No newline at end of file
diff --git a/src/cmd/go/testdata/mod/example.com_generics_v1.0.0.txt b/src/cmd/go/testdata/mod/example.com_generics_v1.0.0.txt
new file mode 100644 (file)
index 0000000..092241e
--- /dev/null
@@ -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() {}
\ No newline at end of file
diff --git a/src/cmd/go/testdata/script/install_dep_version.txt b/src/cmd/go/testdata/script/install_dep_version.txt
new file mode 100644 (file)
index 0000000..22b52e5
--- /dev/null
@@ -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
\ No newline at end of file