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 <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Bryan C. Mills 2023-05-26 14:48:22 -04:00 committed by Gopher Robot
parent 5f08963b2c
commit 0a87fdc06e
1 changed files with 6 additions and 7 deletions

View 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 {