Skip to content

Commit 52dbffe

Browse files
rscgopherbot
authored andcommitted
cmd/go/internal/toolchain: revert "make a best effort to parse 'go run' and 'go install' flags"
This caused other problems, and for the purposes of the Go 1.22 release, we can just roll back to the Go 1.21 behavior and then decide in January what the correct path forward is. Revert of CL 546635. Unfixes #64282. Fixes #64738. For #57001. This reverts commit e44b8b1. Change-Id: I78753c76dcd0bc6dbc90caa17f73248c42e5f64a Reviewed-on: https://go-review.googlesource.com/c/go/+/551215 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Russ Cox <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent 1d4b0b6 commit 52dbffe

File tree

10 files changed

+45
-91
lines changed

10 files changed

+45
-91
lines changed

src/cmd/go/internal/base/goflags.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type boolFlag interface {
8888
}
8989

9090
// SetFromGOFLAGS sets the flags in the given flag set using settings in $GOFLAGS.
91-
func SetFromGOFLAGS(flags *flag.FlagSet, ignoreErrors bool) {
91+
func SetFromGOFLAGS(flags *flag.FlagSet) {
9292
InitGOFLAGS()
9393

9494
// This loop is similar to flag.Parse except that it ignores
@@ -121,22 +121,22 @@ func SetFromGOFLAGS(flags *flag.FlagSet, ignoreErrors bool) {
121121

122122
if fb, ok := f.Value.(boolFlag); ok && fb.IsBoolFlag() {
123123
if hasValue {
124-
if err := flags.Set(f.Name, value); err != nil && !ignoreErrors {
124+
if err := flags.Set(f.Name, value); err != nil {
125125
fmt.Fprintf(flags.Output(), "go: invalid boolean value %q for flag %s (from %s): %v\n", value, name, where, err)
126126
flags.Usage()
127127
}
128128
} else {
129-
if err := flags.Set(f.Name, "true"); err != nil && !ignoreErrors {
129+
if err := flags.Set(f.Name, "true"); err != nil {
130130
fmt.Fprintf(flags.Output(), "go: invalid boolean flag %s (from %s): %v\n", name, where, err)
131131
flags.Usage()
132132
}
133133
}
134134
} else {
135-
if !hasValue && !ignoreErrors {
135+
if !hasValue {
136136
fmt.Fprintf(flags.Output(), "go: flag needs an argument: %s (from %s)\n", name, where)
137137
flags.Usage()
138138
}
139-
if err := flags.Set(f.Name, value); err != nil && !ignoreErrors {
139+
if err := flags.Set(f.Name, value); err != nil {
140140
fmt.Fprintf(flags.Output(), "go: invalid value %q for flag %s (from %s): %v\n", value, name, where, err)
141141
flags.Usage()
142142
}

src/cmd/go/internal/modfetch/fetch.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ func readGoSum(dst map[module.Version][]string, file string, data []byte) {
525525
// ignore malformed line so that go mod tidy can fix go.sum
526526
continue
527527
} else {
528-
base.Fatalf("go: malformed go.sum:\n%s:%d: wrong number of fields %v\n", file, lineno, len(f))
528+
base.Fatalf("malformed go.sum:\n%s:%d: wrong number of fields %v\n", file, lineno, len(f))
529529
}
530530
}
531531
if f[2] == emptyGoModHash {
@@ -574,32 +574,32 @@ func checkMod(ctx context.Context, mod module.Version) {
574574
// Do the file I/O before acquiring the go.sum lock.
575575
ziphash, err := CachePath(ctx, mod, "ziphash")
576576
if err != nil {
577-
base.Fatalf("go: verifying %v", module.VersionError(mod, err))
577+
base.Fatalf("verifying %v", module.VersionError(mod, err))
578578
}
579579
data, err := lockedfile.Read(ziphash)
580580
if err != nil {
581-
base.Fatalf("go: verifying %v", module.VersionError(mod, err))
581+
base.Fatalf("verifying %v", module.VersionError(mod, err))
582582
}
583583
data = bytes.TrimSpace(data)
584584
if !isValidSum(data) {
585585
// Recreate ziphash file from zip file and use that to check the mod sum.
586586
zip, err := CachePath(ctx, mod, "zip")
587587
if err != nil {
588-
base.Fatalf("go: verifying %v", module.VersionError(mod, err))
588+
base.Fatalf("verifying %v", module.VersionError(mod, err))
589589
}
590590
err = hashZip(mod, zip, ziphash)
591591
if err != nil {
592-
base.Fatalf("go: verifying %v", module.VersionError(mod, err))
592+
base.Fatalf("verifying %v", module.VersionError(mod, err))
593593
}
594594
return
595595
}
596596
h := string(data)
597597
if !strings.HasPrefix(h, "h1:") {
598-
base.Fatalf("go: verifying %v", module.VersionError(mod, fmt.Errorf("unexpected ziphash: %q", h)))
598+
base.Fatalf("verifying %v", module.VersionError(mod, fmt.Errorf("unexpected ziphash: %q", h)))
599599
}
600600

601601
if err := checkModSum(mod, h); err != nil {
602-
base.Fatal(err)
602+
base.Fatalf("%s", err)
603603
}
604604
}
605605

@@ -684,7 +684,7 @@ func haveModSumLocked(mod module.Version, h string) bool {
684684
return true
685685
}
686686
if strings.HasPrefix(vh, "h1:") {
687-
base.Fatalf("go: verifying %s@%s: checksum mismatch\n\tdownloaded: %v\n\t%s: %v"+goSumMismatch, mod.Path, mod.Version, h, sumFileName, vh)
687+
base.Fatalf("verifying %s@%s: checksum mismatch\n\tdownloaded: %v\n\t%s: %v"+goSumMismatch, mod.Path, mod.Version, h, sumFileName, vh)
688688
}
689689
}
690690
// Also check workspace sums.
@@ -696,7 +696,7 @@ func haveModSumLocked(mod module.Version, h string) bool {
696696
if h == vh {
697697
foundMatch = true
698698
} else if strings.HasPrefix(vh, "h1:") {
699-
base.Fatalf("go: verifying %s@%s: checksum mismatch\n\tdownloaded: %v\n\t%s: %v"+goSumMismatch, mod.Path, mod.Version, h, goSumFile, vh)
699+
base.Fatalf("verifying %s@%s: checksum mismatch\n\tdownloaded: %v\n\t%s: %v"+goSumMismatch, mod.Path, mod.Version, h, goSumFile, vh)
700700
}
701701
}
702702
}
@@ -895,7 +895,7 @@ func TrimGoSum(keep map[module.Version]bool) {
895895
defer goSum.mu.Unlock()
896896
inited, err := initGoSum()
897897
if err != nil {
898-
base.Fatal(err)
898+
base.Fatalf("%s", err)
899899
}
900900
if !inited {
901901
return

src/cmd/go/internal/test/testflag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (f *shuffleFlag) Set(value string) error {
222222
// go test fmt -custom-flag-for-fmt-test
223223
// go test -x math
224224
func testFlags(args []string) (packageNames, passToTest []string) {
225-
base.SetFromGOFLAGS(&CmdTest.Flag, false)
225+
base.SetFromGOFLAGS(&CmdTest.Flag)
226226
addFromGOFLAGS := map[string]bool{}
227227
CmdTest.Flag.Visit(func(f *flag.Flag) {
228228
if short := strings.TrimPrefix(f.Name, "test."); passFlagToTest[short] {

src/cmd/go/internal/toolchain/exec.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ func execGoToolchain(gotoolchain, dir, exe string) {
4444
if e.ProcessState.Exited() {
4545
os.Exit(e.ProcessState.ExitCode())
4646
}
47-
base.Fatalf("go: exec %s: %s", gotoolchain, e.ProcessState)
47+
base.Fatalf("exec %s: %s", gotoolchain, e.ProcessState)
4848
}
49-
base.Fatalf("go: exec %s: %s", exe, err)
49+
base.Fatalf("exec %s: %s", exe, err)
5050
}
5151
os.Exit(0)
5252
}
5353
err := syscall.Exec(exe, os.Args, os.Environ())
54-
base.Fatalf("go: exec %s: %v", gotoolchain, err)
54+
base.Fatalf("exec %s: %v", gotoolchain, err)
5555
}

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

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ package toolchain
88
import (
99
"context"
1010
"errors"
11-
"flag"
1211
"fmt"
1312
"go/build"
1413
"io/fs"
14+
"log"
1515
"os"
1616
"path/filepath"
1717
"runtime"
@@ -20,12 +20,10 @@ import (
2020

2121
"cmd/go/internal/base"
2222
"cmd/go/internal/cfg"
23-
"cmd/go/internal/cmdflag"
2423
"cmd/go/internal/gover"
2524
"cmd/go/internal/modfetch"
2625
"cmd/go/internal/modload"
2726
"cmd/go/internal/run"
28-
"cmd/go/internal/work"
2927

3028
"golang.org/x/mod/module"
3129
)
@@ -87,6 +85,9 @@ func FilterEnv(env []string) []string {
8785
// It must be called early in startup.
8886
// See https://go.dev/doc/toolchain#select.
8987
func Select() {
88+
log.SetPrefix("go: ")
89+
defer log.SetPrefix("")
90+
9091
if !modload.WillBeEnabled() {
9192
return
9293
}
@@ -132,15 +133,15 @@ func Select() {
132133
v := gover.FromToolchain(min)
133134
if v == "" {
134135
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)
136137
}
137-
base.Fatalf("go: invalid GOTOOLCHAIN %q", gotoolchain)
138+
base.Fatalf("invalid GOTOOLCHAIN %q", gotoolchain)
138139
}
139140
minToolchain = min
140141
minVers = v
141142
}
142143
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)
144145
}
145146
mode = suffix
146147
}
@@ -171,7 +172,7 @@ func Select() {
171172
// has a suffix like "go1.21.1-foo" and toolchain is "go1.21.1".)
172173
toolVers := gover.FromToolchain(toolchain)
173174
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))
175176
}
176177
if gover.Compare(toolVers, minVers) > 0 {
177178
gotoolchain = toolchain
@@ -193,7 +194,7 @@ func Select() {
193194
// so that we have initialized gover.Startup for use in error messages.
194195
if target := os.Getenv(targetEnv); target != "" && TestVersionSwitch != "loop" {
195196
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)
197198
}
198199
os.Unsetenv(targetEnv)
199200

@@ -224,7 +225,7 @@ func Select() {
224225
// We want to disallow mistakes / bad ideas like GOTOOLCHAIN=bash,
225226
// since we will find that in the path lookup.
226227
if !strings.HasPrefix(gotoolchain, "go1") && !strings.Contains(gotoolchain, "-go1") {
227-
base.Fatalf("go: invalid GOTOOLCHAIN %q", gotoolchain)
228+
base.Fatalf("invalid GOTOOLCHAIN %q", gotoolchain)
228229
}
229230

230231
Exec(gotoolchain)
@@ -243,14 +244,16 @@ var TestVersionSwitch string
243244
// as a source of Go toolchains. Otherwise Exec tries the PATH but then downloads
244245
// a toolchain if necessary.
245246
func Exec(gotoolchain string) {
247+
log.SetPrefix("go: ")
248+
246249
writeBits = sysWriteBits()
247250

248251
count, _ := strconv.Atoi(os.Getenv(countEnv))
249252
if count >= maxSwitch-10 {
250253
fmt.Fprintf(os.Stderr, "go: switching from go%v to %v [depth %d]\n", gover.Local(), gotoolchain, count)
251254
}
252255
if count >= maxSwitch {
253-
base.Fatalf("go: too many toolchain switches")
256+
base.Fatalf("too many toolchain switches")
254257
}
255258
os.Setenv(countEnv, fmt.Sprint(count+1))
256259

@@ -273,7 +276,7 @@ func Exec(gotoolchain string) {
273276
case "loop", "mismatch":
274277
exe, err := os.Executable()
275278
if err != nil {
276-
base.Fatal(err)
279+
base.Fatalf("%v", err)
277280
}
278281
execGoToolchain(gotoolchain, os.Getenv("GOROOT"), exe)
279282
}
@@ -288,7 +291,7 @@ func Exec(gotoolchain string) {
288291
// GOTOOLCHAIN=auto looks in PATH and then falls back to download.
289292
// GOTOOLCHAIN=path only looks in PATH.
290293
if pathOnly {
291-
base.Fatalf("go: cannot find %q in PATH", gotoolchain)
294+
base.Fatalf("cannot find %q in PATH", gotoolchain)
292295
}
293296

294297
// Set up modules without an explicit go.mod, to download distribution.
@@ -307,9 +310,9 @@ func Exec(gotoolchain string) {
307310
dir, err := modfetch.Download(context.Background(), m)
308311
if err != nil {
309312
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)
311314
}
312-
base.Fatalf("go: download %s: %v", gotoolchain, err)
315+
base.Fatalf("download %s: %v", gotoolchain, err)
313316
}
314317

315318
// On first use after download, set the execute bits on the commands
@@ -318,7 +321,7 @@ func Exec(gotoolchain string) {
318321
if runtime.GOOS != "windows" {
319322
info, err := os.Stat(filepath.Join(dir, "bin/go"))
320323
if err != nil {
321-
base.Fatalf("go: download %s: %v", gotoolchain, err)
324+
base.Fatalf("download %s: %v", gotoolchain, err)
322325
}
323326
if info.Mode()&0111 == 0 {
324327
// allowExec sets the exec permission bits on all files found in dir.
@@ -339,7 +342,7 @@ func Exec(gotoolchain string) {
339342
return nil
340343
})
341344
if err != nil {
342-
base.Fatalf("go: download %s: %v", gotoolchain, err)
345+
base.Fatalf("download %s: %v", gotoolchain, err)
343346
}
344347
}
345348

@@ -381,7 +384,7 @@ func Exec(gotoolchain string) {
381384
err = raceSafeCopy(srcUGoMod, srcGoMod)
382385
}
383386
if err != nil {
384-
base.Fatalf("go: download %s: %v", gotoolchain, err)
387+
base.Fatalf("download %s: %v", gotoolchain, err)
385388
}
386389
}
387390

@@ -472,7 +475,7 @@ func modGoToolchain() (file, goVers, toolchain string) {
472475

473476
data, err := os.ReadFile(file)
474477
if err != nil {
475-
base.Fatal(err)
478+
base.Fatalf("%v", err)
476479
}
477480
return file, gover.GoModLookup(data, "go"), gover.GoModLookup(data, "toolchain")
478481
}
@@ -489,7 +492,6 @@ func goInstallVersion() bool {
489492

490493
// Check for pkg@version.
491494
var arg string
492-
var cmdFlags *flag.FlagSet
493495
switch os.Args[1] {
494496
default:
495497
return false
@@ -498,15 +500,13 @@ func goInstallVersion() bool {
498500
// across a toolchain switch. To make that work, assume the pkg@version
499501
// is the last argument and skip the flag parsing.
500502
arg = os.Args[len(os.Args)-1]
501-
cmdFlags = &work.CmdInstall.Flag
502503
case "run":
503504
// For run, the pkg@version can be anywhere on the command line,
504505
// because it is preceded by run flags and followed by arguments to the
505506
// program being run. To handle that precisely, we have to interpret the
506507
// flags a little bit, to know whether each flag takes an optional argument.
507508
// We can still allow unknown flags as long as they have an explicit =value.
508509
args := os.Args[2:]
509-
cmdFlags = &run.CmdRun.Flag
510510
for i := 0; i < len(args); i++ {
511511
a := args[i]
512512
if !strings.HasPrefix(a, "-") {
@@ -554,20 +554,6 @@ func goInstallVersion() bool {
554554
return false
555555
}
556556

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-
571557
// It would be correct to simply return true here, bypassing use
572558
// of the current go.mod or go.work, and let "go run" or "go install"
573559
// do the rest, including a toolchain switch.

src/cmd/go/internal/vet/vetflag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func vetFlags(args []string) (passToVet, packageNames []string) {
116116

117117
// Record the set of vet tool flags set by GOFLAGS. We want to pass them to
118118
// the vet tool, but only if they aren't overridden by an explicit argument.
119-
base.SetFromGOFLAGS(&CmdVet.Flag, false)
119+
base.SetFromGOFLAGS(&CmdVet.Flag)
120120
addFromGOFLAGS := map[string]bool{}
121121
CmdVet.Flag.Visit(func(f *flag.Flag) {
122122
if isVetFlag[f.Name] {

src/cmd/go/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func invoke(cmd *base.Command, args []string) {
234234
if cmd.CustomFlags {
235235
args = args[1:]
236236
} else {
237-
base.SetFromGOFLAGS(&cmd.Flag, false)
237+
base.SetFromGOFLAGS(&cmd.Flag)
238238
cmd.Flag.Parse(args[1:])
239239
args = cmd.Flag.Args()
240240
}

src/cmd/go/testdata/script/install_modcacherw_issue64282.txt

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)