]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modget: print a fatal error if -d=false
authorMichael Matloob <matloob@golang.org>
Thu, 6 Jun 2024 16:51:33 +0000 (12:51 -0400)
committerMichael Matloob <matloob@golang.org>
Thu, 6 Jun 2024 19:50:03 +0000 (19:50 +0000)
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Sam Thanawalla <samthanawalla@google.com>
src/cmd/go/internal/modget/get.go

index 2d37d24a93db1c709a46f9bdea85cf8bd81706a9..9a02fcdd9620224c368759b533848a6ce44037f6 100644 (file)
@@ -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")