Source file src/cmd/go/internal/toolchain/select.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package toolchain implements dynamic switching of Go toolchains.
     6  package toolchain
     7  
     8  import (
     9  	"bytes"
    10  	"context"
    11  	"errors"
    12  	"flag"
    13  	"fmt"
    14  	"go/build"
    15  	"internal/godebug"
    16  	"io"
    17  	"io/fs"
    18  	"log"
    19  	"os"
    20  	"path/filepath"
    21  	"runtime"
    22  	"strconv"
    23  	"strings"
    24  
    25  	"cmd/go/internal/base"
    26  	"cmd/go/internal/cfg"
    27  	"cmd/go/internal/gover"
    28  	"cmd/go/internal/modfetch"
    29  	"cmd/go/internal/modload"
    30  	"cmd/go/internal/run"
    31  	"cmd/go/internal/work"
    32  	"cmd/internal/pathcache"
    33  	"cmd/internal/telemetry/counter"
    34  
    35  	"golang.org/x/mod/module"
    36  )
    37  
    38  const (
    39  	// We download golang.org/toolchain version v0.0.1-<gotoolchain>.<goos>-<goarch>.
    40  	// If the 0.0.1 indicates anything at all, its the version of the toolchain packaging:
    41  	// if for some reason we needed to change the way toolchains are packaged into
    42  	// module zip files in a future version of Go, we could switch to v0.0.2 and then
    43  	// older versions expecting the old format could use v0.0.1 and newer versions
    44  	// would use v0.0.2. Of course, then we'd also have to publish two of each
    45  	// module zip file. It's not likely we'll ever need to change this.
    46  	gotoolchainModule  = "golang.org/toolchain"
    47  	gotoolchainVersion = "v0.0.1"
    48  
    49  	// targetEnv is a special environment variable set to the expected
    50  	// toolchain version during the toolchain switch by the parent
    51  	// process and cleared in the child process. When set, that indicates
    52  	// to the child to confirm that it provides the expected toolchain version.
    53  	targetEnv = "GOTOOLCHAIN_INTERNAL_SWITCH_VERSION"
    54  
    55  	// countEnv is a special environment variable
    56  	// that is incremented during each toolchain switch, to detect loops.
    57  	// It is cleared before invoking programs in 'go run', 'go test', 'go generate', and 'go tool'
    58  	// by invoking them in an environment filtered with FilterEnv,
    59  	// so user programs should not see this in their environment.
    60  	countEnv = "GOTOOLCHAIN_INTERNAL_SWITCH_COUNT"
    61  
    62  	// maxSwitch is the maximum toolchain switching depth.
    63  	// Most uses should never see more than three.
    64  	// (Perhaps one for the initial GOTOOLCHAIN dispatch,
    65  	// a second for go get doing an upgrade, and a third if
    66  	// for some reason the chosen upgrade version is too small
    67  	// by a little.)
    68  	// When the count reaches maxSwitch - 10, we start logging
    69  	// the switched versions for debugging before crashing with
    70  	// a fatal error upon reaching maxSwitch.
    71  	// That should be enough to see the repetition.
    72  	maxSwitch = 100
    73  )
    74  
    75  // FilterEnv returns a copy of env with internal GOTOOLCHAIN environment
    76  // variables filtered out.
    77  func FilterEnv(env []string) []string {
    78  	// Note: Don't need to filter out targetEnv because Switch does that.
    79  	var out []string
    80  	for _, e := range env {
    81  		if strings.HasPrefix(e, countEnv+"=") {
    82  			continue
    83  		}
    84  		out = append(out, e)
    85  	}
    86  	return out
    87  }
    88  
    89  var counterErrorsInvalidToolchainInFile = counter.New("go/errors:invalid-toolchain-in-file")
    90  var toolchainTrace = godebug.New("#toolchaintrace").Value() == "1"
    91  
    92  // Select invokes a different Go toolchain if directed by
    93  // the GOTOOLCHAIN environment variable or the user's configuration
    94  // or go.mod file.
    95  // It must be called early in startup.
    96  // See https://go.dev/doc/toolchain#select.
    97  func Select() {
    98  	log.SetPrefix("go: ")
    99  	defer log.SetPrefix("")
   100  
   101  	if !modload.WillBeEnabled() {
   102  		return
   103  	}
   104  
   105  	// As a special case, let "go env GOTOOLCHAIN" and "go env -w GOTOOLCHAIN=..."
   106  	// be handled by the local toolchain, since an older toolchain may not understand it.
   107  	// This provides an easy way out of "go env -w GOTOOLCHAIN=go1.19" and makes
   108  	// sure that "go env GOTOOLCHAIN" always prints the local go command's interpretation of it.
   109  	// We look for these specific command lines in order to avoid mishandling
   110  	//
   111  	//	GOTOOLCHAIN=go1.999 go env -newflag GOTOOLCHAIN
   112  	//
   113  	// where -newflag is a flag known to Go 1.999 but not known to us.
   114  	if (len(os.Args) == 3 && os.Args[1] == "env" && os.Args[2] == "GOTOOLCHAIN") ||
   115  		(len(os.Args) == 4 && os.Args[1] == "env" && os.Args[2] == "-w" && strings.HasPrefix(os.Args[3], "GOTOOLCHAIN=")) {
   116  		return
   117  	}
   118  
   119  	// As a special case, let "go env GOMOD" and "go env GOWORK" be handled by
   120  	// the local toolchain. Users expect to be able to look up GOMOD and GOWORK
   121  	// since the go.mod and go.work file need to be determined to determine
   122  	// the minimum toolchain. See issue #61455.
   123  	if len(os.Args) == 3 && os.Args[1] == "env" && (os.Args[2] == "GOMOD" || os.Args[2] == "GOWORK") {
   124  		return
   125  	}
   126  
   127  	// Interpret GOTOOLCHAIN to select the Go toolchain to run.
   128  	gotoolchain := cfg.Getenv("GOTOOLCHAIN")
   129  	gover.Startup.GOTOOLCHAIN = gotoolchain
   130  	if gotoolchain == "" {
   131  		// cfg.Getenv should fall back to $GOROOT/go.env,
   132  		// so this should not happen, unless a packager
   133  		// has deleted the GOTOOLCHAIN line from go.env.
   134  		// It can also happen if GOROOT is missing or broken,
   135  		// in which case best to let the go command keep running
   136  		// and diagnose the problem.
   137  		return
   138  	}
   139  
   140  	// Note: minToolchain is what https://go.dev/doc/toolchain#select calls the default toolchain.
   141  	minToolchain := gover.LocalToolchain()
   142  	minVers := gover.Local()
   143  	var mode string
   144  	var toolchainTraceBuffer bytes.Buffer
   145  	if gotoolchain == "auto" {
   146  		mode = "auto"
   147  	} else if gotoolchain == "path" {
   148  		mode = "path"
   149  	} else {
   150  		min, suffix, plus := strings.Cut(gotoolchain, "+") // go1.2.3+auto
   151  		if min != "local" {
   152  			v := gover.FromToolchain(min)
   153  			if v == "" {
   154  				if plus {
   155  					base.Fatalf("invalid GOTOOLCHAIN %q: invalid minimum toolchain %q", gotoolchain, min)
   156  				}
   157  				base.Fatalf("invalid GOTOOLCHAIN %q", gotoolchain)
   158  			}
   159  			minToolchain = min
   160  			minVers = v
   161  		}
   162  		if plus && suffix != "auto" && suffix != "path" {
   163  			base.Fatalf("invalid GOTOOLCHAIN %q: only version suffixes are +auto and +path", gotoolchain)
   164  		}
   165  		mode = suffix
   166  		if toolchainTrace {
   167  			fmt.Fprintf(&toolchainTraceBuffer, "go: default toolchain set to %s from GOTOOLCHAIN=%s\n", minToolchain, gotoolchain)
   168  		}
   169  	}
   170  
   171  	gotoolchain = minToolchain
   172  	if (mode == "auto" || mode == "path") && !goInstallVersion() {
   173  		// Read go.mod to find new minimum and suggested toolchain.
   174  		file, goVers, toolchain := modGoToolchain()
   175  		gover.Startup.AutoFile = file
   176  		if toolchain == "default" {
   177  			// "default" means always use the default toolchain,
   178  			// which is already set, so nothing to do here.
   179  			// Note that if we have Go 1.21 installed originally,
   180  			// GOTOOLCHAIN=go1.30.0+auto or GOTOOLCHAIN=go1.30.0,
   181  			// and the go.mod  says "toolchain default", we use Go 1.30, not Go 1.21.
   182  			// That is, default overrides the "auto" part of the calculation
   183  			// but not the minimum that the user has set.
   184  			// Of course, if the go.mod also says "go 1.35", using Go 1.30
   185  			// will provoke an error about the toolchain being too old.
   186  			// That's what people who use toolchain default want:
   187  			// only ever use the toolchain configured by the user
   188  			// (including its environment and go env -w file).
   189  			gover.Startup.AutoToolchain = toolchain
   190  		} else {
   191  			if toolchain != "" {
   192  				// Accept toolchain only if it is > our min.
   193  				// (If it is equal, then min satisfies it anyway: that can matter if min
   194  				// has a suffix like "go1.21.1-foo" and toolchain is "go1.21.1".)
   195  				toolVers := gover.FromToolchain(toolchain)
   196  				if toolVers == "" || (!strings.HasPrefix(toolchain, "go") && !strings.Contains(toolchain, "-go")) {
   197  					counterErrorsInvalidToolchainInFile.Inc()
   198  					base.Fatalf("invalid toolchain %q in %s", toolchain, base.ShortPath(file))
   199  				}
   200  				if gover.Compare(toolVers, minVers) > 0 {
   201  					if toolchainTrace {
   202  						modeFormat := mode
   203  						if strings.Contains(cfg.Getenv("GOTOOLCHAIN"), "+") { // go1.2.3+auto
   204  							modeFormat = fmt.Sprintf("<name>+%s", mode)
   205  						}
   206  						fmt.Fprintf(&toolchainTraceBuffer, "go: upgrading toolchain to %s (required by toolchain line in %s; upgrade allowed by GOTOOLCHAIN=%s)\n", toolchain, base.ShortPath(file), modeFormat)
   207  					}
   208  					gotoolchain = toolchain
   209  					minVers = toolVers
   210  					gover.Startup.AutoToolchain = toolchain
   211  				}
   212  			}
   213  			if gover.Compare(goVers, minVers) > 0 {
   214  				gotoolchain = "go" + goVers
   215  				// Starting with Go 1.21, the first released version has a .0 patch version suffix.
   216  				// Don't try to download a language version (sans patch component), such as go1.22.
   217  				// Instead, use the first toolchain of that language version, such as 1.22.0.
   218  				// See golang.org/issue/62278.
   219  				if gover.IsLang(goVers) && gover.Compare(goVers, "1.21") >= 0 {
   220  					gotoolchain += ".0"
   221  				}
   222  				gover.Startup.AutoGoVersion = goVers
   223  				gover.Startup.AutoToolchain = "" // in case we are overriding it for being too old
   224  				if toolchainTrace {
   225  					modeFormat := mode
   226  					if strings.Contains(cfg.Getenv("GOTOOLCHAIN"), "+") { // go1.2.3+auto
   227  						modeFormat = fmt.Sprintf("<name>+%s", mode)
   228  					}
   229  					fmt.Fprintf(&toolchainTraceBuffer, "go: upgrading toolchain to %s (required by go line in %s; upgrade allowed by GOTOOLCHAIN=%s)\n", gotoolchain, base.ShortPath(file), modeFormat)
   230  				}
   231  			}
   232  		}
   233  	}
   234  
   235  	// If we are invoked as a target toolchain, confirm that
   236  	// we provide the expected version and then run.
   237  	// This check is delayed until after the handling of auto and path
   238  	// so that we have initialized gover.Startup for use in error messages.
   239  	if target := os.Getenv(targetEnv); target != "" && TestVersionSwitch != "loop" {
   240  		if gover.LocalToolchain() != target {
   241  			base.Fatalf("toolchain %v invoked to provide %v", gover.LocalToolchain(), target)
   242  		}
   243  		os.Unsetenv(targetEnv)
   244  
   245  		// Note: It is tempting to check that if gotoolchain != "local"
   246  		// then target == gotoolchain here, as a sanity check that
   247  		// the child has made the same version determination as the parent.
   248  		// This turns out not always to be the case. Specifically, if we are
   249  		// running Go 1.21 with GOTOOLCHAIN=go1.22+auto, which invokes
   250  		// Go 1.22, then 'go get go@1.23.0' or 'go get needs_go_1_23'
   251  		// will invoke Go 1.23, but as the Go 1.23 child the reason for that
   252  		// will not be apparent here: it will look like we should be using Go 1.22.
   253  		// We rely on the targetEnv being set to know not to downgrade.
   254  		// A longer term problem with the sanity check is that the exact details
   255  		// may change over time: there may be other reasons that a future Go
   256  		// version might invoke an older one, and the older one won't know why.
   257  		// Best to just accept that we were invoked to provide a specific toolchain
   258  		// (which we just checked) and leave it at that.
   259  		return
   260  	}
   261  
   262  	if toolchainTrace {
   263  		// Flush toolchain tracing buffer only in the parent process (targetEnv is unset).
   264  		io.Copy(os.Stderr, &toolchainTraceBuffer)
   265  	}
   266  
   267  	if gotoolchain == "local" || gotoolchain == gover.LocalToolchain() {
   268  		// Let the current binary handle the command.
   269  		if toolchainTrace {
   270  			fmt.Fprintf(os.Stderr, "go: using local toolchain %s\n", gover.LocalToolchain())
   271  		}
   272  		return
   273  	}
   274  
   275  	// Minimal sanity check of GOTOOLCHAIN setting before search.
   276  	// We want to allow things like go1.20.3 but also gccgo-go1.20.3.
   277  	// We want to disallow mistakes / bad ideas like GOTOOLCHAIN=bash,
   278  	// since we will find that in the path lookup.
   279  	if !strings.HasPrefix(gotoolchain, "go1") && !strings.Contains(gotoolchain, "-go1") {
   280  		base.Fatalf("invalid GOTOOLCHAIN %q", gotoolchain)
   281  	}
   282  
   283  	counterSelectExec.Inc()
   284  	Exec(gotoolchain)
   285  }
   286  
   287  var counterSelectExec = counter.New("go/toolchain/select-exec")
   288  
   289  // TestVersionSwitch is set in the test go binary to the value in $TESTGO_VERSION_SWITCH.
   290  // Valid settings are:
   291  //
   292  //	"switch" - simulate version switches by reinvoking the test go binary with a different TESTGO_VERSION.
   293  //	"mismatch" - like "switch" but forget to set TESTGO_VERSION, so it looks like we invoked a mismatched toolchain
   294  //	"loop" - like "mismatch" but forget the target check, causing a toolchain switching loop
   295  var TestVersionSwitch string
   296  
   297  // Exec invokes the specified Go toolchain or else prints an error and exits the process.
   298  // If $GOTOOLCHAIN is set to path or min+path, Exec only considers the PATH
   299  // as a source of Go toolchains. Otherwise Exec tries the PATH but then downloads
   300  // a toolchain if necessary.
   301  func Exec(gotoolchain string) {
   302  	log.SetPrefix("go: ")
   303  
   304  	writeBits = sysWriteBits()
   305  
   306  	count, _ := strconv.Atoi(os.Getenv(countEnv))
   307  	if count >= maxSwitch-10 {
   308  		fmt.Fprintf(os.Stderr, "go: switching from go%v to %v [depth %d]\n", gover.Local(), gotoolchain, count)
   309  	}
   310  	if count >= maxSwitch {
   311  		base.Fatalf("too many toolchain switches")
   312  	}
   313  	os.Setenv(countEnv, fmt.Sprint(count+1))
   314  
   315  	env := cfg.Getenv("GOTOOLCHAIN")
   316  	pathOnly := env == "path" || strings.HasSuffix(env, "+path")
   317  
   318  	// For testing, if TESTGO_VERSION is already in use
   319  	// (only happens in the cmd/go test binary)
   320  	// and TESTGO_VERSION_SWITCH=switch is set,
   321  	// "switch" toolchains by changing TESTGO_VERSION
   322  	// and reinvoking the current binary.
   323  	// The special cases =loop and =mismatch skip the
   324  	// setting of TESTGO_VERSION so that it looks like we
   325  	// accidentally invoked the wrong toolchain,
   326  	// to test detection of that failure mode.
   327  	switch TestVersionSwitch {
   328  	case "switch":
   329  		os.Setenv("TESTGO_VERSION", gotoolchain)
   330  		fallthrough
   331  	case "loop", "mismatch":
   332  		exe, err := os.Executable()
   333  		if err != nil {
   334  			base.Fatalf("%v", err)
   335  		}
   336  		execGoToolchain(gotoolchain, os.Getenv("GOROOT"), exe)
   337  	}
   338  
   339  	// Look in PATH for the toolchain before we download one.
   340  	// This allows custom toolchains as well as reuse of toolchains
   341  	// already installed using go install golang.org/dl/go1.2.3@latest.
   342  	if exe, err := pathcache.LookPath(gotoolchain); err == nil {
   343  		execGoToolchain(gotoolchain, "", exe)
   344  	}
   345  
   346  	// GOTOOLCHAIN=auto looks in PATH and then falls back to download.
   347  	// GOTOOLCHAIN=path only looks in PATH.
   348  	if pathOnly {
   349  		base.Fatalf("cannot find %q in PATH", gotoolchain)
   350  	}
   351  
   352  	// Set up modules without an explicit go.mod, to download distribution.
   353  	modload.Reset()
   354  	modload.ForceUseModules = true
   355  	modload.RootMode = modload.NoRoot
   356  	modload.Init()
   357  
   358  	// Download and unpack toolchain module into module cache.
   359  	// Note that multiple go commands might be doing this at the same time,
   360  	// and that's OK: the module cache handles that case correctly.
   361  	m := module.Version{
   362  		Path:    gotoolchainModule,
   363  		Version: gotoolchainVersion + "-" + gotoolchain + "." + runtime.GOOS + "-" + runtime.GOARCH,
   364  	}
   365  	dir, err := modfetch.Download(context.Background(), m)
   366  	if err != nil {
   367  		if errors.Is(err, fs.ErrNotExist) {
   368  			toolVers := gover.FromToolchain(gotoolchain)
   369  			if gover.IsLang(toolVers) && gover.Compare(toolVers, "1.21") >= 0 {
   370  				base.Fatalf("invalid toolchain: %s is a language version but not a toolchain version (%s.x)", gotoolchain, gotoolchain)
   371  			}
   372  			base.Fatalf("download %s for %s/%s: toolchain not available", gotoolchain, runtime.GOOS, runtime.GOARCH)
   373  		}
   374  		base.Fatalf("download %s: %v", gotoolchain, err)
   375  	}
   376  
   377  	// On first use after download, set the execute bits on the commands
   378  	// so that we can run them. Note that multiple go commands might be
   379  	// doing this at the same time, but if so no harm done.
   380  	if runtime.GOOS != "windows" {
   381  		info, err := os.Stat(filepath.Join(dir, "bin/go"))
   382  		if err != nil {
   383  			base.Fatalf("download %s: %v", gotoolchain, err)
   384  		}
   385  		if info.Mode()&0111 == 0 {
   386  			// allowExec sets the exec permission bits on all files found in dir if pattern is the empty string,
   387  			// or only those files that match the pattern if it's non-empty.
   388  			allowExec := func(dir, pattern string) {
   389  				err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
   390  					if err != nil {
   391  						return err
   392  					}
   393  					if !d.IsDir() {
   394  						if pattern != "" {
   395  							if matched, _ := filepath.Match(pattern, d.Name()); !matched {
   396  								// Skip file.
   397  								return nil
   398  							}
   399  						}
   400  						info, err := os.Stat(path)
   401  						if err != nil {
   402  							return err
   403  						}
   404  						if err := os.Chmod(path, info.Mode()&0777|0111); err != nil {
   405  							return err
   406  						}
   407  					}
   408  					return nil
   409  				})
   410  				if err != nil {
   411  					base.Fatalf("download %s: %v", gotoolchain, err)
   412  				}
   413  			}
   414  
   415  			// Set the bits in pkg/tool before bin/go.
   416  			// If we are racing with another go command and do bin/go first,
   417  			// then the check of bin/go above might succeed, the other go command
   418  			// would skip its own mode-setting, and then the go command might
   419  			// try to run a tool before we get to setting the bits on pkg/tool.
   420  			// Setting pkg/tool and lib before bin/go avoids that ordering problem.
   421  			// The only other tool the go command invokes is gofmt,
   422  			// so we set that one explicitly before handling bin (which will include bin/go).
   423  			allowExec(filepath.Join(dir, "pkg/tool"), "")
   424  			allowExec(filepath.Join(dir, "lib"), "go_?*_?*_exec")
   425  			allowExec(filepath.Join(dir, "bin/gofmt"), "")
   426  			allowExec(filepath.Join(dir, "bin"), "")
   427  		}
   428  	}
   429  
   430  	srcUGoMod := filepath.Join(dir, "src/_go.mod")
   431  	srcGoMod := filepath.Join(dir, "src/go.mod")
   432  	if size(srcGoMod) != size(srcUGoMod) {
   433  		err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
   434  			if err != nil {
   435  				return err
   436  			}
   437  			if path == srcUGoMod {
   438  				// Leave for last, in case we are racing with another go command.
   439  				return nil
   440  			}
   441  			if pdir, name := filepath.Split(path); name == "_go.mod" {
   442  				if err := raceSafeCopy(path, pdir+"go.mod"); err != nil {
   443  					return err
   444  				}
   445  			}
   446  			return nil
   447  		})
   448  		// Handle src/go.mod; this is the signal to other racing go commands
   449  		// that everything is okay and they can skip this step.
   450  		if err == nil {
   451  			err = raceSafeCopy(srcUGoMod, srcGoMod)
   452  		}
   453  		if err != nil {
   454  			base.Fatalf("download %s: %v", gotoolchain, err)
   455  		}
   456  	}
   457  
   458  	// Reinvoke the go command.
   459  	execGoToolchain(gotoolchain, dir, filepath.Join(dir, "bin/go"))
   460  }
   461  
   462  func size(path string) int64 {
   463  	info, err := os.Stat(path)
   464  	if err != nil {
   465  		return -1
   466  	}
   467  	return info.Size()
   468  }
   469  
   470  var writeBits fs.FileMode
   471  
   472  // raceSafeCopy copies the file old to the file new, being careful to ensure
   473  // that if multiple go commands call raceSafeCopy(old, new) at the same time,
   474  // they don't interfere with each other: both will succeed and return and
   475  // later observe the correct content in new. Like in the build cache, we arrange
   476  // this by opening new without truncation and then writing the content.
   477  // Both go commands can do this simultaneously and will write the same thing
   478  // (old never changes content).
   479  func raceSafeCopy(old, new string) error {
   480  	oldInfo, err := os.Stat(old)
   481  	if err != nil {
   482  		return err
   483  	}
   484  	newInfo, err := os.Stat(new)
   485  	if err == nil && newInfo.Size() == oldInfo.Size() {
   486  		return nil
   487  	}
   488  	data, err := os.ReadFile(old)
   489  	if err != nil {
   490  		return err
   491  	}
   492  	// The module cache has unwritable directories by default.
   493  	// Restore the user write bit in the directory so we can create
   494  	// the new go.mod file. We clear it again at the end on a
   495  	// best-effort basis (ignoring failures).
   496  	dir := filepath.Dir(old)
   497  	info, err := os.Stat(dir)
   498  	if err != nil {
   499  		return err
   500  	}
   501  	if err := os.Chmod(dir, info.Mode()|writeBits); err != nil {
   502  		return err
   503  	}
   504  	defer os.Chmod(dir, info.Mode())
   505  	// Note: create the file writable, so that a racing go command
   506  	// doesn't get an error before we store the actual data.
   507  	f, err := os.OpenFile(new, os.O_CREATE|os.O_WRONLY, writeBits&^0o111)
   508  	if err != nil {
   509  		// If OpenFile failed because a racing go command completed our work
   510  		// (and then OpenFile failed because the directory or file is now read-only),
   511  		// count that as a success.
   512  		if size(old) == size(new) {
   513  			return nil
   514  		}
   515  		return err
   516  	}
   517  	defer os.Chmod(new, oldInfo.Mode())
   518  	if _, err := f.Write(data); err != nil {
   519  		f.Close()
   520  		return err
   521  	}
   522  	return f.Close()
   523  }
   524  
   525  // modGoToolchain finds the enclosing go.work or go.mod file
   526  // and returns the go version and toolchain lines from the file.
   527  // The toolchain line overrides the version line
   528  func modGoToolchain() (file, goVers, toolchain string) {
   529  	wd := base.UncachedCwd()
   530  	file = modload.FindGoWork(wd)
   531  	// $GOWORK can be set to a file that does not yet exist, if we are running 'go work init'.
   532  	// Do not try to load the file in that case
   533  	if _, err := os.Stat(file); err != nil {
   534  		file = ""
   535  	}
   536  	if file == "" {
   537  		file = modload.FindGoMod(wd)
   538  	}
   539  	if file == "" {
   540  		return "", "", ""
   541  	}
   542  
   543  	data, err := os.ReadFile(file)
   544  	if err != nil {
   545  		base.Fatalf("%v", err)
   546  	}
   547  	return file, gover.GoModLookup(data, "go"), gover.GoModLookup(data, "toolchain")
   548  }
   549  
   550  // goInstallVersion reports whether the command line is go install m@v or go run m@v.
   551  // If so, Select must not read the go.mod or go.work file in "auto" or "path" mode.
   552  func goInstallVersion() bool {
   553  	// Note: We assume there are no flags between 'go' and 'install' or 'run'.
   554  	// During testing there are some debugging flags that are accepted
   555  	// in that position, but in production go binaries there are not.
   556  	if len(os.Args) < 3 {
   557  		return false
   558  	}
   559  
   560  	var cmdFlags *flag.FlagSet
   561  	switch os.Args[1] {
   562  	default:
   563  		// Command doesn't support a pkg@version as the main module.
   564  		return false
   565  	case "install":
   566  		cmdFlags = &work.CmdInstall.Flag
   567  	case "run":
   568  		cmdFlags = &run.CmdRun.Flag
   569  	}
   570  
   571  	// The modcachrw flag is unique, in that it affects how we fetch the
   572  	// requested module to even figure out what toolchain it needs.
   573  	// We need to actually set it before we check the toolchain version.
   574  	// (See https://go.dev/issue/64282.)
   575  	modcacherwFlag := cmdFlags.Lookup("modcacherw")
   576  	if modcacherwFlag == nil {
   577  		base.Fatalf("internal error: modcacherw flag not registered for command")
   578  	}
   579  	modcacherwVal, ok := modcacherwFlag.Value.(interface {
   580  		IsBoolFlag() bool
   581  		flag.Value
   582  	})
   583  	if !ok || !modcacherwVal.IsBoolFlag() {
   584  		base.Fatalf("internal error: modcacherw is not a boolean flag")
   585  	}
   586  
   587  	// Make a best effort to parse the command's args to find the pkg@version
   588  	// argument and the -modcacherw flag.
   589  	var (
   590  		pkgArg         string
   591  		modcacherwSeen bool
   592  	)
   593  	for args := os.Args[2:]; len(args) > 0; {
   594  		a := args[0]
   595  		args = args[1:]
   596  		if a == "--" {
   597  			if len(args) == 0 {
   598  				return false
   599  			}
   600  			pkgArg = args[0]
   601  			break
   602  		}
   603  
   604  		a, ok := strings.CutPrefix(a, "-")
   605  		if !ok {
   606  			// Not a flag argument. Must be a package.
   607  			pkgArg = a
   608  			break
   609  		}
   610  		a = strings.TrimPrefix(a, "-") // Treat --flag as -flag.
   611  
   612  		name, val, hasEq := strings.Cut(a, "=")
   613  
   614  		if name == "modcacherw" {
   615  			if !hasEq {
   616  				val = "true"
   617  			}
   618  			if err := modcacherwVal.Set(val); err != nil {
   619  				return false
   620  			}
   621  			modcacherwSeen = true
   622  			continue
   623  		}
   624  
   625  		if hasEq {
   626  			// Already has a value; don't bother parsing it.
   627  			continue
   628  		}
   629  
   630  		f := run.CmdRun.Flag.Lookup(a)
   631  		if f == nil {
   632  			// We don't know whether this flag is a boolean.
   633  			if os.Args[1] == "run" {
   634  				// We don't know where to find the pkg@version argument.
   635  				// For run, the pkg@version can be anywhere on the command line,
   636  				// because it is preceded by run flags and followed by arguments to the
   637  				// program being run. Since we don't know whether this flag takes
   638  				// an argument, we can't reliably identify the end of the run flags.
   639  				// Just give up and let the user clarify using the "=" form..
   640  				return false
   641  			}
   642  
   643  			// We would like to let 'go install -newflag pkg@version' work even
   644  			// across a toolchain switch. To make that work, assume by default that
   645  			// the pkg@version is the last argument and skip the remaining args unless
   646  			// we spot a plausible "-modcacherw" flag.
   647  			for len(args) > 0 {
   648  				a := args[0]
   649  				name, _, _ := strings.Cut(a, "=")
   650  				if name == "-modcacherw" || name == "--modcacherw" {
   651  					break
   652  				}
   653  				if len(args) == 1 && !strings.HasPrefix(a, "-") {
   654  					pkgArg = a
   655  				}
   656  				args = args[1:]
   657  			}
   658  			continue
   659  		}
   660  
   661  		if bf, ok := f.Value.(interface{ IsBoolFlag() bool }); !ok || !bf.IsBoolFlag() {
   662  			// The next arg is the value for this flag. Skip it.
   663  			args = args[1:]
   664  			continue
   665  		}
   666  	}
   667  
   668  	if !strings.Contains(pkgArg, "@") || build.IsLocalImport(pkgArg) || filepath.IsAbs(pkgArg) {
   669  		return false
   670  	}
   671  	path, version, _ := strings.Cut(pkgArg, "@")
   672  	if path == "" || version == "" || gover.IsToolchain(path) {
   673  		return false
   674  	}
   675  
   676  	if !modcacherwSeen && base.InGOFLAGS("-modcacherw") {
   677  		fs := flag.NewFlagSet("goInstallVersion", flag.ExitOnError)
   678  		fs.Var(modcacherwVal, "modcacherw", modcacherwFlag.Usage)
   679  		base.SetFromGOFLAGS(fs)
   680  	}
   681  
   682  	// It would be correct to simply return true here, bypassing use
   683  	// of the current go.mod or go.work, and let "go run" or "go install"
   684  	// do the rest, including a toolchain switch.
   685  	// Our goal instead is, since we have gone to the trouble of handling
   686  	// unknown flags to some degree, to run the switch now, so that
   687  	// these commands can switch to a newer toolchain directed by the
   688  	// go.mod which may actually understand the flag.
   689  	// This was brought up during the go.dev/issue/57001 proposal discussion
   690  	// and may end up being common in self-contained "go install" or "go run"
   691  	// command lines if we add new flags in the future.
   692  
   693  	// Set up modules without an explicit go.mod, to download go.mod.
   694  	modload.ForceUseModules = true
   695  	modload.RootMode = modload.NoRoot
   696  	modload.Init()
   697  	defer modload.Reset()
   698  
   699  	// See internal/load.PackagesAndErrorsOutsideModule
   700  	ctx := context.Background()
   701  	allowed := modload.CheckAllowed
   702  	if modload.IsRevisionQuery(path, version) {
   703  		// Don't check for retractions if a specific revision is requested.
   704  		allowed = nil
   705  	}
   706  	noneSelected := func(path string) (version string) { return "none" }
   707  	_, err := modload.QueryPackages(ctx, path, version, noneSelected, allowed)
   708  	if errors.Is(err, gover.ErrTooNew) {
   709  		// Run early switch, same one go install or go run would eventually do,
   710  		// if it understood all the command-line flags.
   711  		SwitchOrFatal(ctx, err)
   712  	}
   713  
   714  	return true // pkg@version found
   715  }
   716  

View as plain text