From: Michael Matloob Date: Thu, 6 Jun 2024 16:51:33 +0000 (-0400) Subject: cmd/go/internal/modget: print a fatal error if -d=false X-Git-Tag: go1.23rc1~71 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=2f6ba0c294e8ab202666e34d833286eecaa26d2b;p=gostls13.git cmd/go/internal/modget: print a fatal error if -d=false Between Go 1.18 and Go 1.22 go get printed a fatal error if -d was explicitly set to false. That behavior was reverted in CL 572176, when we made the -d flag a no-op, but it would make it easier to remove the -d flag in the future if we continue to print a fatal error if -d is explicitly set to false. This change brings back the fatal error for -d=false while keeping the warning printed for -d=true. For #43684 Change-Id: I38ae3a3619d408c0237ff485ddee4403b8188abd Reviewed-on: https://go-review.googlesource.com/c/go/+/591135 LUCI-TryBot-Result: Go LUCI Reviewed-by: Sam Thanawalla --- diff --git a/src/cmd/go/internal/modget/get.go b/src/cmd/go/internal/modget/get.go index 2d37d24a93..9a02fcdd96 100644 --- a/src/cmd/go/internal/modget/get.go +++ b/src/cmd/go/internal/modget/get.go @@ -32,6 +32,7 @@ import ( "path/filepath" "runtime" "sort" + "strconv" "strings" "sync" @@ -208,7 +209,7 @@ variable for future go command invocations. } var ( - getD = CmdGet.Flag.Bool("d", false, "") + getD dFlag getF = CmdGet.Flag.Bool("f", false, "") getFix = CmdGet.Flag.Bool("fix", false, "") getM = CmdGet.Flag.Bool("m", false, "") @@ -242,9 +243,32 @@ func (v *upgradeFlag) Set(s string) error { func (v *upgradeFlag) String() string { return "" } +// dFlag is a custom flag.Value for the deprecated -d flag +// which will be used to provide warnings or errors if -d +// is provided. +type dFlag struct { + value bool + set bool +} + +func (v *dFlag) IsBoolFlag() bool { return true } + +func (v *dFlag) Set(s string) error { + v.set = true + value, err := strconv.ParseBool(s) + if err != nil { + err = errors.New("parse error") + } + v.value = value + return err +} + +func (b *dFlag) String() string { return "" } + func init() { work.AddBuildFlags(CmdGet, work.OmitModFlag) CmdGet.Run = runGet // break init loop + CmdGet.Flag.Var(&getD, "d", "") CmdGet.Flag.Var(&getU, "u", "") } @@ -255,8 +279,11 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) { default: base.Fatalf("go: unknown upgrade flag -u=%s", getU.rawVersion) } - if *getD { - fmt.Fprintf(os.Stderr, "go: -d flag is a no-op\n") + if getD.set { + if !getD.value { + base.Fatalf("go: -d flag may not be set to false") + } + fmt.Fprintf(os.Stderr, "go: -d flag is deprecated. -d=true is a no-op\n") } if *getF { fmt.Fprintf(os.Stderr, "go: -f flag is a no-op\n")