]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/toolchain: avoid importing modcmd
authorBryan C. Mills <bcmills@google.com>
Fri, 26 May 2023 18:48:22 +0000 (14:48 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 1 Jun 2023 15:54:37 +0000 (15:54 +0000)
modcmd is a high-level command, but toolchain is a low-level building
block. A dependency from toolchain on modcmd makes it very difficult
to call from other lower-level packages without creating an import
cycle.

Instead, use modfetch.Download in place of modcmd.DownloadModule.

For #57001.

Change-Id: I9694706d7225b269f26dc68814894613a3329abb
Reviewed-on: https://go-review.googlesource.com/c/go/+/499316
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/go/internal/toolchain/toolchain.go

index ab03fbe4ff1ae859a62391055c735d83d8ba4221..757ab6977dac0b72a109a49e824d74ec764177ac 100644 (file)
@@ -7,6 +7,7 @@ package toolchain
 
 import (
        "context"
+       "errors"
        "fmt"
        "go/build"
        "io/fs"
@@ -22,7 +23,6 @@ import (
        "cmd/go/internal/base"
        "cmd/go/internal/cfg"
        "cmd/go/internal/gover"
-       "cmd/go/internal/modcmd"
        "cmd/go/internal/modfetch"
        "cmd/go/internal/modload"
        "cmd/go/internal/run"
@@ -410,22 +410,21 @@ func SwitchTo(gotoolchain string) {
        // Download and unpack toolchain module into module cache.
        // Note that multiple go commands might be doing this at the same time,
        // and that's OK: the module cache handles that case correctly.
-       m := &modcmd.ModuleJSON{
+       m := module.Version{
                Path:    gotoolchainModule,
                Version: gotoolchainVersion + "-" + gotoolchain + "." + runtime.GOOS + "-" + runtime.GOARCH,
        }
-       modcmd.DownloadModule(context.Background(), m)
-       if m.Error != "" {
-               if strings.Contains(m.Error, ".info: 404") {
+       dir, err := modfetch.Download(context.Background(), m)
+       if err != nil {
+               if errors.Is(err, fs.ErrNotExist) {
                        base.Fatalf("download %s for %s/%s: toolchain not available", gotoolchain, runtime.GOOS, runtime.GOARCH)
                }
-               base.Fatalf("download %s: %v", gotoolchain, m.Error)
+               base.Fatalf("download %s: %v", gotoolchain, err)
        }
 
        // On first use after download, set the execute bits on the commands
        // so that we can run them. Note that multiple go commands might be
        // doing this at the same time, but if so no harm done.
-       dir := m.Dir
        if runtime.GOOS != "windows" {
                info, err := os.Stat(filepath.Join(dir, "bin/go"))
                if err != nil {