]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/go: allow toolchain upgrades in 'go mod download' when we would already allow...
authorBryan C. Mills <bcmills@google.com>
Wed, 8 Nov 2023 17:33:10 +0000 (12:33 -0500)
committerGopher Robot <gobot@golang.org>
Wed, 8 Nov 2023 19:32:39 +0000 (19:32 +0000)
This fixes an inconsistency that was introduced in CL 537480 and noted
in the review on CL 539697.

In particular, 'go mod download' already updates the go.mod file when
other kinds of updates are needed. (#45551 suggested that it should
not do so, but that part of the change was not implemented yet;
finishing that change is proposed as #64008.)

Updates #62054.

Change-Id: Ic659eb8538f4afdec0454737e982d42ef8957e56
Reviewed-on: https://go-review.googlesource.com/c/go/+/540779
Auto-Submit: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
src/cmd/go/internal/modcmd/download.go
src/cmd/go/testdata/script/gotoolchain_modcmds.txt
src/cmd/go/testdata/script/mod_download_exec_toolchain.txt

index b1f26975bcc4b8bc22d498dca0c3f2a3e22824e9..373accef0679610cd064445706fa46ee8065f1c9 100644 (file)
@@ -155,7 +155,10 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
                                // 'go mod graph', and similar commands.
                                _, err := modload.LoadModGraph(ctx, "")
                                if err != nil {
-                                       base.Fatal(err)
+                                       // TODO(#64008): call base.Fatalf instead of toolchain.SwitchOrFatal
+                                       // here, since we can only reach this point with an outdated toolchain
+                                       // if the go.mod file is inconsistent.
+                                       toolchain.SwitchOrFatal(ctx, err)
                                }
 
                                for _, m := range modFile.Require {
@@ -207,6 +210,16 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
        // no explicit arguments (their go.mod file should already list an appropriate
        // toolchain version) or only one module (as is used by the Go Module Proxy).
 
+       if infosErr != nil {
+               var sw toolchain.Switcher
+               sw.Error(infosErr)
+               if sw.NeedSwitch() {
+                       sw.Switch(ctx)
+               }
+               // Otherwise, wait to report infosErr after we have downloaded
+               // when we can.
+       }
+
        if !haveExplicitArgs && modload.WorkFilePath() == "" {
                // 'go mod download' is sometimes run without arguments to pre-populate the
                // module cache. In modules that aren't at go 1.17 or higher, it may fetch
@@ -215,20 +228,12 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
                // (golang.org/issue/45332). We do still fix inconsistencies in go.mod
                // though.
                //
-               // TODO(#45551): In the future, report an error if go.mod or go.sum need to
+               // TODO(#64008): In the future, report an error if go.mod or go.sum need to
                // be updated after loading the build list. This may require setting
                // the mode to "mod" or "readonly" depending on haveExplicitArgs.
                if err := modload.WriteGoMod(ctx, modload.WriteOpts{}); err != nil {
                        base.Fatal(err)
                }
-       } else if infosErr != nil {
-               var sw toolchain.Switcher
-               sw.Error(infosErr)
-               if sw.NeedSwitch() {
-                       sw.Switch(ctx)
-               }
-               // Otherwise, wait to report infosErr after we have downloaded
-               // when we can.
        }
 
        var downloadErrs sync.Map
index 64db6d9666c57fe7cec0930a719e8fca043185c6..1edd6d85a5e66c6884e9be4e645b613cd76cec8b 100644 (file)
@@ -3,19 +3,18 @@ env TESTGO_VERSION_SWITCH=switch
 
 # If the main module's go.mod file lists a version lower than the version
 # required by its dependencies, the commands that fetch and diagnose the module
-# graph (such as 'go mod download' and 'go mod graph') should fail explicitly:
+# graph (such as 'go mod graph' and 'go mod verify') should fail explicitly:
 # they can't interpret the graph themselves, and they aren't allowed to update
 # the go.mod file to record a specific, stable toolchain version that can.
 
-! go mod download
-stderr '^go: rsc.io/future@v1.0.0: module rsc.io/future@v1.0.0 requires go >= 1.999 \(running go 1.21.0\)'
-
 ! go mod verify
 stderr '^go: rsc.io/future@v1.0.0: module rsc.io/future@v1.0.0 requires go >= 1.999 \(running go 1.21.0\)'
 
 ! go mod graph
 stderr '^go: rsc.io/future@v1.0.0: module rsc.io/future@v1.0.0 requires go >= 1.999 \(running go 1.21.0\)'
 
+# TODO(#64008): 'go mod download' without arguments should fail too.
+
 
 # 'go get' should update the main module's go.mod file to a version compatible with the
 # go version required for rsc.io/future, not fail.
index e44145775456bb458fcf5729a606dde6288154c0..6cf863b28a28f6b16bcf38a8b9b1a9342713ae41 100644 (file)
@@ -87,10 +87,9 @@ cmp go.mod go.mod.121
 # If an upgrade is needed, GOTOOLCHAIN=auto should perform
 # the upgrade and record the resulting toolchain version.
 
-env GOTOOLCHAIN=go1.21
-! go mod download
-stderr 'rsc.io/needall@v0.0.1 requires go >= 1.23'
-! stderr switching
+env GOTOOLCHAIN=auto
+go mod download
+stderr '^go: module rsc.io/needall@v0.0.1 requires go >= 1.23; switching to go1.23.9$'
 cmp go.mod go.mod.final
 
 
@@ -101,6 +100,8 @@ go 1.21
 -- example/go.mod.final --
 module example
 
-go 1.21
+go 1.23
+
+toolchain go1.23.9
 
 require rsc.io/needall v0.0.1