From: Bryan C. Mills Date: Fri, 26 May 2023 18:48:22 +0000 (-0400) Subject: cmd/go/internal/toolchain: avoid importing modcmd X-Git-Tag: go1.21rc1~141 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=0a87fdc06e9c4ae73b31cd08c26ed32c8a80923b;p=gostls13.git cmd/go/internal/toolchain: avoid importing modcmd 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 Auto-Submit: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Russ Cox --- diff --git a/src/cmd/go/internal/toolchain/toolchain.go b/src/cmd/go/internal/toolchain/toolchain.go index ab03fbe4ff..757ab6977d 100644 --- a/src/cmd/go/internal/toolchain/toolchain.go +++ b/src/cmd/go/internal/toolchain/toolchain.go @@ -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 {