@@ -8,10 +8,10 @@ package toolchain
8
8
import (
9
9
"context"
10
10
"errors"
11
- "flag"
12
11
"fmt"
13
12
"go/build"
14
13
"io/fs"
14
+ "log"
15
15
"os"
16
16
"path/filepath"
17
17
"runtime"
@@ -20,12 +20,10 @@ import (
20
20
21
21
"cmd/go/internal/base"
22
22
"cmd/go/internal/cfg"
23
- "cmd/go/internal/cmdflag"
24
23
"cmd/go/internal/gover"
25
24
"cmd/go/internal/modfetch"
26
25
"cmd/go/internal/modload"
27
26
"cmd/go/internal/run"
28
- "cmd/go/internal/work"
29
27
30
28
"golang.org/x/mod/module"
31
29
)
@@ -87,6 +85,9 @@ func FilterEnv(env []string) []string {
87
85
// It must be called early in startup.
88
86
// See https://go.dev/doc/toolchain#select.
89
87
func Select () {
88
+ log .SetPrefix ("go: " )
89
+ defer log .SetPrefix ("" )
90
+
90
91
if ! modload .WillBeEnabled () {
91
92
return
92
93
}
@@ -132,15 +133,15 @@ func Select() {
132
133
v := gover .FromToolchain (min )
133
134
if v == "" {
134
135
if plus {
135
- base .Fatalf ("go: invalid GOTOOLCHAIN %q: invalid minimum toolchain %q" , gotoolchain , min )
136
+ base .Fatalf ("invalid GOTOOLCHAIN %q: invalid minimum toolchain %q" , gotoolchain , min )
136
137
}
137
- base .Fatalf ("go: invalid GOTOOLCHAIN %q" , gotoolchain )
138
+ base .Fatalf ("invalid GOTOOLCHAIN %q" , gotoolchain )
138
139
}
139
140
minToolchain = min
140
141
minVers = v
141
142
}
142
143
if plus && suffix != "auto" && suffix != "path" {
143
- base .Fatalf ("go: invalid GOTOOLCHAIN %q: only version suffixes are +auto and +path" , gotoolchain )
144
+ base .Fatalf ("invalid GOTOOLCHAIN %q: only version suffixes are +auto and +path" , gotoolchain )
144
145
}
145
146
mode = suffix
146
147
}
@@ -171,7 +172,7 @@ func Select() {
171
172
// has a suffix like "go1.21.1-foo" and toolchain is "go1.21.1".)
172
173
toolVers := gover .FromToolchain (toolchain )
173
174
if toolVers == "" || (! strings .HasPrefix (toolchain , "go" ) && ! strings .Contains (toolchain , "-go" )) {
174
- base .Fatalf ("go: invalid toolchain %q in %s" , toolchain , base .ShortPath (file ))
175
+ base .Fatalf ("invalid toolchain %q in %s" , toolchain , base .ShortPath (file ))
175
176
}
176
177
if gover .Compare (toolVers , minVers ) > 0 {
177
178
gotoolchain = toolchain
@@ -193,7 +194,7 @@ func Select() {
193
194
// so that we have initialized gover.Startup for use in error messages.
194
195
if target := os .Getenv (targetEnv ); target != "" && TestVersionSwitch != "loop" {
195
196
if gover .LocalToolchain () != target {
196
- base .Fatalf ("go: toolchain %v invoked to provide %v" , gover .LocalToolchain (), target )
197
+ base .Fatalf ("toolchain %v invoked to provide %v" , gover .LocalToolchain (), target )
197
198
}
198
199
os .Unsetenv (targetEnv )
199
200
@@ -224,7 +225,7 @@ func Select() {
224
225
// We want to disallow mistakes / bad ideas like GOTOOLCHAIN=bash,
225
226
// since we will find that in the path lookup.
226
227
if ! strings .HasPrefix (gotoolchain , "go1" ) && ! strings .Contains (gotoolchain , "-go1" ) {
227
- base .Fatalf ("go: invalid GOTOOLCHAIN %q" , gotoolchain )
228
+ base .Fatalf ("invalid GOTOOLCHAIN %q" , gotoolchain )
228
229
}
229
230
230
231
Exec (gotoolchain )
@@ -243,14 +244,16 @@ var TestVersionSwitch string
243
244
// as a source of Go toolchains. Otherwise Exec tries the PATH but then downloads
244
245
// a toolchain if necessary.
245
246
func Exec (gotoolchain string ) {
247
+ log .SetPrefix ("go: " )
248
+
246
249
writeBits = sysWriteBits ()
247
250
248
251
count , _ := strconv .Atoi (os .Getenv (countEnv ))
249
252
if count >= maxSwitch - 10 {
250
253
fmt .Fprintf (os .Stderr , "go: switching from go%v to %v [depth %d]\n " , gover .Local (), gotoolchain , count )
251
254
}
252
255
if count >= maxSwitch {
253
- base .Fatalf ("go: too many toolchain switches" )
256
+ base .Fatalf ("too many toolchain switches" )
254
257
}
255
258
os .Setenv (countEnv , fmt .Sprint (count + 1 ))
256
259
@@ -273,7 +276,7 @@ func Exec(gotoolchain string) {
273
276
case "loop" , "mismatch" :
274
277
exe , err := os .Executable ()
275
278
if err != nil {
276
- base .Fatal ( err )
279
+ base .Fatalf ( "%v" , err )
277
280
}
278
281
execGoToolchain (gotoolchain , os .Getenv ("GOROOT" ), exe )
279
282
}
@@ -288,7 +291,7 @@ func Exec(gotoolchain string) {
288
291
// GOTOOLCHAIN=auto looks in PATH and then falls back to download.
289
292
// GOTOOLCHAIN=path only looks in PATH.
290
293
if pathOnly {
291
- base .Fatalf ("go: cannot find %q in PATH" , gotoolchain )
294
+ base .Fatalf ("cannot find %q in PATH" , gotoolchain )
292
295
}
293
296
294
297
// Set up modules without an explicit go.mod, to download distribution.
@@ -307,9 +310,9 @@ func Exec(gotoolchain string) {
307
310
dir , err := modfetch .Download (context .Background (), m )
308
311
if err != nil {
309
312
if errors .Is (err , fs .ErrNotExist ) {
310
- base .Fatalf ("go: download %s for %s/%s: toolchain not available" , gotoolchain , runtime .GOOS , runtime .GOARCH )
313
+ base .Fatalf ("download %s for %s/%s: toolchain not available" , gotoolchain , runtime .GOOS , runtime .GOARCH )
311
314
}
312
- base .Fatalf ("go: download %s: %v" , gotoolchain , err )
315
+ base .Fatalf ("download %s: %v" , gotoolchain , err )
313
316
}
314
317
315
318
// On first use after download, set the execute bits on the commands
@@ -318,7 +321,7 @@ func Exec(gotoolchain string) {
318
321
if runtime .GOOS != "windows" {
319
322
info , err := os .Stat (filepath .Join (dir , "bin/go" ))
320
323
if err != nil {
321
- base .Fatalf ("go: download %s: %v" , gotoolchain , err )
324
+ base .Fatalf ("download %s: %v" , gotoolchain , err )
322
325
}
323
326
if info .Mode ()& 0111 == 0 {
324
327
// allowExec sets the exec permission bits on all files found in dir.
@@ -339,7 +342,7 @@ func Exec(gotoolchain string) {
339
342
return nil
340
343
})
341
344
if err != nil {
342
- base .Fatalf ("go: download %s: %v" , gotoolchain , err )
345
+ base .Fatalf ("download %s: %v" , gotoolchain , err )
343
346
}
344
347
}
345
348
@@ -381,7 +384,7 @@ func Exec(gotoolchain string) {
381
384
err = raceSafeCopy (srcUGoMod , srcGoMod )
382
385
}
383
386
if err != nil {
384
- base .Fatalf ("go: download %s: %v" , gotoolchain , err )
387
+ base .Fatalf ("download %s: %v" , gotoolchain , err )
385
388
}
386
389
}
387
390
@@ -472,7 +475,7 @@ func modGoToolchain() (file, goVers, toolchain string) {
472
475
473
476
data , err := os .ReadFile (file )
474
477
if err != nil {
475
- base .Fatal ( err )
478
+ base .Fatalf ( "%v" , err )
476
479
}
477
480
return file , gover .GoModLookup (data , "go" ), gover .GoModLookup (data , "toolchain" )
478
481
}
@@ -489,7 +492,6 @@ func goInstallVersion() bool {
489
492
490
493
// Check for pkg@version.
491
494
var arg string
492
- var cmdFlags * flag.FlagSet
493
495
switch os .Args [1 ] {
494
496
default :
495
497
return false
@@ -498,15 +500,13 @@ func goInstallVersion() bool {
498
500
// across a toolchain switch. To make that work, assume the pkg@version
499
501
// is the last argument and skip the flag parsing.
500
502
arg = os .Args [len (os .Args )- 1 ]
501
- cmdFlags = & work .CmdInstall .Flag
502
503
case "run" :
503
504
// For run, the pkg@version can be anywhere on the command line,
504
505
// because it is preceded by run flags and followed by arguments to the
505
506
// program being run. To handle that precisely, we have to interpret the
506
507
// flags a little bit, to know whether each flag takes an optional argument.
507
508
// We can still allow unknown flags as long as they have an explicit =value.
508
509
args := os .Args [2 :]
509
- cmdFlags = & run .CmdRun .Flag
510
510
for i := 0 ; i < len (args ); i ++ {
511
511
a := args [i ]
512
512
if ! strings .HasPrefix (a , "-" ) {
@@ -554,20 +554,6 @@ func goInstallVersion() bool {
554
554
return false
555
555
}
556
556
557
- // Make a best effort to parse flags so that module flags like -modcacherw
558
- // will take effect (see https://go.dev/issue/64282).
559
- args := os .Args [2 :]
560
- for len (args ) > 0 {
561
- var err error
562
- _ , args , err = cmdflag .ParseOne (cmdFlags , args )
563
- if errors .Is (err , cmdflag .ErrFlagTerminator ) {
564
- break
565
- }
566
- // Ignore all other errors: they may be new flags — or updated syntax for
567
- // existing flags — intended for a newer Go toolchain.
568
- }
569
- base .SetFromGOFLAGS (cmdFlags , true )
570
-
571
557
// It would be correct to simply return true here, bypassing use
572
558
// of the current go.mod or go.work, and let "go run" or "go install"
573
559
// do the rest, including a toolchain switch.
0 commit comments