Debian Codegraph @

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

raw
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" {
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 minVers = goVers
216 // Starting with Go 1.21, the first released version has a .0 patch version suffix.
217 // Don't try to download a language version (sans patch component), such as go1.22.
218 // Instead, use the first toolchain of that language version, such as 1.22.0.
219 // See golang.org/issue/62278.
220 if gover.IsLang(goVers) && gover.Compare(goVers, "1.21") >= 0 {
221 gotoolchain += ".0"
222 }
223 gover.Startup.AutoGoVersion = goVers
224 gover.Startup.AutoToolchain = "" // in case we are overriding it for being too old
225 if toolchainTrace {
226 modeFormat := mode
227 if strings.Contains(cfg.Getenv("GOTOOLCHAIN"), "+") { // go1.2.3+auto
228 modeFormat = fmt.Sprintf("<name>+%s", mode)
229 }
230 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)
231 }
232 }
233 }
234 maybeSwitchForGoInstallVersion(minVers)
235 }
236
237 // If we are invoked as a target toolchain, confirm that
238 // we provide the expected version and then run.
239 // This check is delayed until after the handling of auto and path
240 // so that we have initialized gover.Startup for use in error messages.
241 if target := os.Getenv(targetEnv); target != "" && TestVersionSwitch != "loop" {
242 if gover.LocalToolchain() != target {
243 base.Fatalf("toolchain %v invoked to provide %v", gover.LocalToolchain(), target)
244 }
245 os.Unsetenv(targetEnv)
246
247 // Note: It is tempting to check that if gotoolchain != "local"
248 // then target == gotoolchain here, as a sanity check that
249 // the child has made the same version determination as the parent.
250 // This turns out not always to be the case. Specifically, if we are
251 // running Go 1.21 with GOTOOLCHAIN=go1.22+auto, which invokes
252 // Go 1.22, then 'go get [email protected]' or 'go get needs_go_1_23'
253 // will invoke Go 1.23, but as the Go 1.23 child the reason for that
254 // will not be apparent here: it will look like we should be using Go 1.22.
255 // We rely on the targetEnv being set to know not to downgrade.
256 // A longer term problem with the sanity check is that the exact details
257 // may change over time: there may be other reasons that a future Go
258 // version might invoke an older one, and the older one won't know why.
259 // Best to just accept that we were invoked to provide a specific toolchain
260 // (which we just checked) and leave it at that.
261 return
262 }
263
264 if toolchainTrace {
265 // Flush toolchain tracing buffer only in the parent process (targetEnv is unset).
266 io.Copy(os.Stderr, &toolchainTraceBuffer)
267 }
268
269 if gotoolchain == "local" || gotoolchain == gover.LocalToolchain() {
270 // Let the current binary handle the command.
271 if toolchainTrace {
272 fmt.Fprintf(os.Stderr, "go: using local toolchain %s\n", gover.LocalToolchain())
273 }
274 return
275 }
276
277 // Minimal sanity check of GOTOOLCHAIN setting before search.
278 // We want to allow things like go1.20.3 but also gccgo-go1.20.3.
279 // We want to disallow mistakes / bad ideas like GOTOOLCHAIN=bash,
280 // since we will find that in the path lookup.
281 if !strings.HasPrefix(gotoolchain, "go1") && !strings.Contains(gotoolchain, "-go1") {
282 base.Fatalf("invalid GOTOOLCHAIN %q", gotoolchain)
283 }
284
285 counterSelectExec.Inc()
286 Exec(gotoolchain)
287 }
288
289 var counterSelectExec = counter.New("go/toolchain/select-exec")
290
291 // TestVersionSwitch is set in the test go binary to the value in $TESTGO_VERSION_SWITCH.
292 // Valid settings are:
293 //
294 // "switch" - simulate version switches by reinvoking the test go binary with a different TESTGO_VERSION.
295 // "mismatch" - like "switch" but forget to set TESTGO_VERSION, so it looks like we invoked a mismatched toolchain
296 // "loop" - like "mismatch" but forget the target check, causing a toolchain switching loop
297 var TestVersionSwitch string
298
299 // Exec invokes the specified Go toolchain or else prints an error and exits the process.
300 // If $GOTOOLCHAIN is set to path or min+path, Exec only considers the PATH
301 // as a source of Go toolchains. Otherwise Exec tries the PATH but then downloads
302 // a toolchain if necessary.
303 func Exec(gotoolchain string) {
304 log.SetPrefix("go: ")
305
306 writeBits = sysWriteBits()
307
308 count, _ := strconv.Atoi(os.Getenv(countEnv))
309 if count >= maxSwitch-10 {
310 fmt.Fprintf(os.Stderr, "go: switching from go%v to %v [depth %d]\n", gover.Local(), gotoolchain, count)
311 }
312 if count >= maxSwitch {
313 base.Fatalf("too many toolchain switches")
314 }
315 os.Setenv(countEnv, fmt.Sprint(count+1))
316
317 env := cfg.Getenv("GOTOOLCHAIN")
318 pathOnly := env == "path" || strings.HasSuffix(env, "+path")
319
320 // For testing, if TESTGO_VERSION is already in use
321 // (only happens in the cmd/go test binary)
322 // and TESTGO_VERSION_SWITCH=switch is set,
323 // "switch" toolchains by changing TESTGO_VERSION
324 // and reinvoking the current binary.
325 // The special cases =loop and =mismatch skip the
326 // setting of TESTGO_VERSION so that it looks like we
327 // accidentally invoked the wrong toolchain,
328 // to test detection of that failure mode.
329 switch TestVersionSwitch {
330 case "switch":
331 os.Setenv("TESTGO_VERSION", gotoolchain)
332 fallthrough
333 case "loop", "mismatch":
334 exe, err := os.Executable()
335 if err != nil {
336 base.Fatalf("%v", err)
337 }
338 execGoToolchain(gotoolchain, os.Getenv("GOROOT"), exe)
339 }
340
341 // Look in PATH for the toolchain before we download one.
342 // This allows custom toolchains as well as reuse of toolchains
343 // already installed using go install golang.org/dl/go1.2.3@latest.
344 if exe, err := pathcache.LookPath(gotoolchain); err == nil {
345 execGoToolchain(gotoolchain, "", exe)
346 }
347
348 // GOTOOLCHAIN=auto looks in PATH and then falls back to download.
349 // GOTOOLCHAIN=path only looks in PATH.
350 if pathOnly {
351 base.Fatalf("cannot find %q in PATH", gotoolchain)
352 }
353
354 // Set up modules without an explicit go.mod, to download distribution.
355 modload.Reset()
356 modload.ForceUseModules = true
357 modload.RootMode = modload.NoRoot
358 modload.Init()
359
360 // Download and unpack toolchain module into module cache.
361 // Note that multiple go commands might be doing this at the same time,
362 // and that's OK: the module cache handles that case correctly.
363 m := module.Version{
364 Path: gotoolchainModule,
365 Version: gotoolchainVersion + "-" + gotoolchain + "." + runtime.GOOS + "-" + runtime.GOARCH,
366 }
367 dir, err := modfetch.Download(context.Background(), m)
368 if err != nil {
369 if errors.Is(err, fs.ErrNotExist) {
370 toolVers := gover.FromToolchain(gotoolchain)
371 if gover.IsLang(toolVers) && gover.Compare(toolVers, "1.21") >= 0 {
372 base.Fatalf("invalid toolchain: %s is a language version but not a toolchain version (%s.x)", gotoolchain, gotoolchain)
373 }
374 base.Fatalf("download %s for %s/%s: toolchain not available", gotoolchain, runtime.GOOS, runtime.GOARCH)
375 }
376 base.Fatalf("download %s: %v", gotoolchain, err)
377 }
378
379 // On first use after download, set the execute bits on the commands
380 // so that we can run them. Note that multiple go commands might be
381 // doing this at the same time, but if so no harm done.
382 if runtime.GOOS != "windows" {
383 info, err := os.Stat(filepath.Join(dir, "bin/go"))
384 if err != nil {
385 base.Fatalf("download %s: %v", gotoolchain, err)
386 }
387 if info.Mode()&0111 == 0 {
388 // allowExec sets the exec permission bits on all files found in dir if pattern is the empty string,
389 // or only those files that match the pattern if it's non-empty.
390 allowExec := func(dir, pattern string) {
391 err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
392 if err != nil {
393 return err
394 }
395 if !d.IsDir() {
396 if pattern != "" {
397 if matched, _ := filepath.Match(pattern, d.Name()); !matched {
398 // Skip file.
399 return nil
400 }
401 }
402 info, err := os.Stat(path)
403 if err != nil {
404 return err
405 }
406 if err := os.Chmod(path, info.Mode()&0777|0111); err != nil {
407 return err
408 }
409 }
410 return nil
411 })
412 if err != nil {
413 base.Fatalf("download %s: %v", gotoolchain, err)
414 }
415 }
416
417 // Set the bits in pkg/tool before bin/go.
418 // If we are racing with another go command and do bin/go first,
419 // then the check of bin/go above might succeed, the other go command
420 // would skip its own mode-setting, and then the go command might
421 // try to run a tool before we get to setting the bits on pkg/tool.
422 // Setting pkg/tool and lib before bin/go avoids that ordering problem.
423 // The only other tool the go command invokes is gofmt,
424 // so we set that one explicitly before handling bin (which will include bin/go).
425 allowExec(filepath.Join(dir, "pkg/tool"), "")
426 allowExec(filepath.Join(dir, "lib"), "go_?*_?*_exec")
427 allowExec(filepath.Join(dir, "bin/gofmt"), "")
428 allowExec(filepath.Join(dir, "bin"), "")
429 }
430 }
431
432 srcUGoMod := filepath.Join(dir, "src/_go.mod")
433 srcGoMod := filepath.Join(dir, "src/go.mod")
434 if size(srcGoMod) != size(srcUGoMod) {
435 err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
436 if err != nil {
437 return err
438 }
439 if path == srcUGoMod {
440 // Leave for last, in case we are racing with another go command.
441 return nil
442 }
443 if pdir, name := filepath.Split(path); name == "_go.mod" {
444 if err := raceSafeCopy(path, pdir+"go.mod"); err != nil {
445 return err
446 }
447 }
448 return nil
449 })
450 // Handle src/go.mod; this is the signal to other racing go commands
451 // that everything is okay and they can skip this step.
452 if err == nil {
453 err = raceSafeCopy(srcUGoMod, srcGoMod)
454 }
455 if err != nil {
456 base.Fatalf("download %s: %v", gotoolchain, err)
457 }
458 }
459
460 // Reinvoke the go command.
461 execGoToolchain(gotoolchain, dir, filepath.Join(dir, "bin/go"))
462 }
463
464 func size(path string) int64 {
465 info, err := os.Stat(path)
466 if err != nil {
467 return -1
468 }
469 return info.Size()
470 }
471
472 var writeBits fs.FileMode
473
474 // raceSafeCopy copies the file old to the file new, being careful to ensure
475 // that if multiple go commands call raceSafeCopy(old, new) at the same time,
476 // they don't interfere with each other: both will succeed and return and
477 // later observe the correct content in new. Like in the build cache, we arrange
478 // this by opening new without truncation and then writing the content.
479 // Both go commands can do this simultaneously and will write the same thing
480 // (old never changes content).
481 func raceSafeCopy(old, new string) error {
482 oldInfo, err := os.Stat(old)
483 if err != nil {
484 return err
485 }
486 newInfo, err := os.Stat(new)
487 if err == nil && newInfo.Size() == oldInfo.Size() {
488 return nil
489 }
490 data, err := os.ReadFile(old)
491 if err != nil {
492 return err
493 }
494 // The module cache has unwritable directories by default.
495 // Restore the user write bit in the directory so we can create
496 // the new go.mod file. We clear it again at the end on a
497 // best-effort basis (ignoring failures).
498 dir := filepath.Dir(old)
499 info, err := os.Stat(dir)
500 if err != nil {
501 return err
502 }
503 if err := os.Chmod(dir, info.Mode()|writeBits); err != nil {
504 return err
505 }
506 defer os.Chmod(dir, info.Mode())
507 // Note: create the file writable, so that a racing go command
508 // doesn't get an error before we store the actual data.
509 f, err := os.OpenFile(new, os.O_CREATE|os.O_WRONLY, writeBits&^0o111)
510 if err != nil {
511 // If OpenFile failed because a racing go command completed our work
512 // (and then OpenFile failed because the directory or file is now read-only),
513 // count that as a success.
514 if size(old) == size(new) {
515 return nil
516 }
517 return err
518 }
519 defer os.Chmod(new, oldInfo.Mode())
520 if _, err := f.Write(data); err != nil {
521 f.Close()
522 return err
523 }
524 return f.Close()
525 }
526
527 // modGoToolchain finds the enclosing go.work or go.mod file
528 // and returns the go version and toolchain lines from the file.
529 // The toolchain line overrides the version line
530 func modGoToolchain() (file, goVers, toolchain string) {
531 wd := base.UncachedCwd()
532 file = modload.FindGoWork(wd)
533 // $GOWORK can be set to a file that does not yet exist, if we are running 'go work init'.
534 // Do not try to load the file in that case
535 if _, err := os.Stat(file); err != nil {
536 file = ""
537 }
538 if file == "" {
539 file = modload.FindGoMod(wd)
540 }
541 if file == "" {
542 return "", "", ""
543 }
544
545 data, err := os.ReadFile(file)
546 if err != nil {
547 base.Fatalf("%v", err)
548 }
549 return file, gover.GoModLookup(data, "go"), gover.GoModLookup(data, "toolchain")
550 }
551
552 // maybeSwitchForGoInstallVersion reports whether the command line is go install m@v or go run m@v.
553 // If so, switch to the go version required to build m@v if it's higher than minVers.
554 func maybeSwitchForGoInstallVersion(minVers string) {
555 // Note: We assume there are no flags between 'go' and 'install' or 'run'.
556 // During testing there are some debugging flags that are accepted
557 // in that position, but in production go binaries there are not.
558 if len(os.Args) < 3 {
559 return
560 }
561
562 var cmdFlags *flag.FlagSet
563 switch os.Args[1] {
564 default:
565 // Command doesn't support a pkg@version as the main module.
566 return
567 case "install":
568 cmdFlags = &work.CmdInstall.Flag
569 case "run":
570 cmdFlags = &run.CmdRun.Flag
571 }
572
573 // The modcachrw flag is unique, in that it affects how we fetch the
574 // requested module to even figure out what toolchain it needs.
575 // We need to actually set it before we check the toolchain version.
576 // (See https://go.dev/issue/64282.)
577 modcacherwFlag := cmdFlags.Lookup("modcacherw")
578 if modcacherwFlag == nil {
579 base.Fatalf("internal error: modcacherw flag not registered for command")
580 }
581 modcacherwVal, ok := modcacherwFlag.Value.(interface {
582 IsBoolFlag() bool
583 flag.Value
584 })
585 if !ok || !modcacherwVal.IsBoolFlag() {
586 base.Fatalf("internal error: modcacherw is not a boolean flag")
587 }
588
589 // Make a best effort to parse the command's args to find the pkg@version
590 // argument and the -modcacherw flag.
591 var (
592 pkgArg string
593 modcacherwSeen bool
594 )
595 for args := os.Args[2:]; len(args) > 0; {
596 a := args[0]
597 args = args[1:]
598 if a == "--" {
599 if len(args) == 0 {
600 return
601 }
602 pkgArg = args[0]
603 break
604 }
605
606 a, ok := strings.CutPrefix(a, "-")
607 if !ok {
608 // Not a flag argument. Must be a package.
609 pkgArg = a
610 break
611 }
612 a = strings.TrimPrefix(a, "-") // Treat --flag as -flag.
613
614 name, val, hasEq := strings.Cut(a, "=")
615
616 if name == "modcacherw" {
617 if !hasEq {
618 val = "true"
619 }
620 if err := modcacherwVal.Set(val); err != nil {
621 return
622 }
623 modcacherwSeen = true
624 continue
625 }
626
627 if hasEq {
628 // Already has a value; don't bother parsing it.
629 continue
630 }
631
632 f := run.CmdRun.Flag.Lookup(a)
633 if f == nil {
634 // We don't know whether this flag is a boolean.
635 if os.Args[1] == "run" {
636 // We don't know where to find the pkg@version argument.
637 // For run, the pkg@version can be anywhere on the command line,
638 // because it is preceded by run flags and followed by arguments to the
639 // program being run. Since we don't know whether this flag takes
640 // an argument, we can't reliably identify the end of the run flags.
641 // Just give up and let the user clarify using the "=" form.
642 return
643 }
644
645 // We would like to let 'go install -newflag pkg@version' work even
646 // across a toolchain switch. To make that work, assume by default that
647 // the pkg@version is the last argument and skip the remaining args unless
648 // we spot a plausible "-modcacherw" flag.
649 for len(args) > 0 {
650 a := args[0]
651 name, _, _ := strings.Cut(a, "=")
652 if name == "-modcacherw" || name == "--modcacherw" {
653 break
654 }
655 if len(args) == 1 && !strings.HasPrefix(a, "-") {
656 pkgArg = a
657 }
658 args = args[1:]
659 }
660 continue
661 }
662
663 if bf, ok := f.Value.(interface{ IsBoolFlag() bool }); !ok || !bf.IsBoolFlag() {
664 // The next arg is the value for this flag. Skip it.
665 args = args[1:]
666 continue
667 }
668 }
669
670 if !strings.Contains(pkgArg, "@") || build.IsLocalImport(pkgArg) || filepath.IsAbs(pkgArg) {
671 return
672 }
673 path, version, _, err := modload.ParsePathVersion(pkgArg)
674 if err != nil {
675 base.Fatalf("go: %v", err)
676 }
677 if path == "" || version == "" || gover.IsToolchain(path) {
678 return
679 }
680
681 if !modcacherwSeen && base.InGOFLAGS("-modcacherw") {
682 fs := flag.NewFlagSet("goInstallVersion", flag.ExitOnError)
683 fs.Var(modcacherwVal, "modcacherw", modcacherwFlag.Usage)
684 base.SetFromGOFLAGS(fs)
685 }
686
687 // It would be correct to do nothing here, and let "go run" or "go install"
688 // do the toolchain switch.
689 // Our goal instead is, since we have gone to the trouble of handling
690 // unknown flags to some degree, to run the switch now, so that
691 // these commands can switch to a newer toolchain directed by the
692 // go.mod which may actually understand the flag.
693 // This was brought up during the go.dev/issue/57001 proposal discussion
694 // and may end up being common in self-contained "go install" or "go run"
695 // command lines if we add new flags in the future.
696
697 // Set up modules without an explicit go.mod, to download go.mod.
698 modload.ForceUseModules = true
699 modload.RootMode = modload.NoRoot
700 modload.Init()
701 defer modload.Reset()
702
703 // See internal/load.PackagesAndErrorsOutsideModule
704 ctx := context.Background()
705 allowed := modload.CheckAllowed
706 if modload.IsRevisionQuery(path, version) {
707 // Don't check for retractions if a specific revision is requested.
708 allowed = nil
709 }
710 noneSelected := func(path string) (version string) { return "none" }
711 _, err = modload.QueryPackages(ctx, path, version, noneSelected, allowed)
712 if errors.Is(err, gover.ErrTooNew) {
713 // Run early switch, same one go install or go run would eventually do,
714 // if it understood all the command-line flags.
715 var s Switcher
716 s.Error(err)
717 if s.TooNew != nil && gover.Compare(s.TooNew.GoVersion, minVers) > 0 {
718 SwitchOrFatal(ctx, err)
719 }
720 }
721 }