Skip to content

Commit 6e3df74

Browse files
author
Jay Conrod
committed
cmd/go: refactor -mod flag parsing
Keep track of whether the -mod flag was set explicitly. When -mod=readonly is the default, we'll want to adjust our error messages if it's set explicitly. Also, register the -mod, -modcacherw, and -modfile flags in functions in internal/base instead of internal/work. 'go mod' commands that don't load packages shouldn't depend on internal/work. For #40728 Change-Id: I272aea9e19908ba37e151baac4ea8630e90f241f Reviewed-on: https://go-review.googlesource.com/c/go/+/253744 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent b22af9b commit 6e3df74

File tree

14 files changed

+61
-43
lines changed

14 files changed

+61
-43
lines changed

src/cmd/go/internal/base/flag.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,42 @@ func (v *StringsFlag) String() string {
2828
return "<StringsFlag>"
2929
}
3030

31+
// explicitStringFlag is like a regular string flag, but it also tracks whether
32+
// the string was set explicitly to a non-empty value.
33+
type explicitStringFlag struct {
34+
value *string
35+
explicit *bool
36+
}
37+
38+
func (f explicitStringFlag) String() string {
39+
if f.value == nil {
40+
return ""
41+
}
42+
return *f.value
43+
}
44+
45+
func (f explicitStringFlag) Set(v string) error {
46+
*f.value = v
47+
if v != "" {
48+
*f.explicit = true
49+
}
50+
return nil
51+
}
52+
3153
// AddBuildFlagsNX adds the -n and -x build flags to the flag set.
3254
func AddBuildFlagsNX(flags *flag.FlagSet) {
3355
flags.BoolVar(&cfg.BuildN, "n", false, "")
3456
flags.BoolVar(&cfg.BuildX, "x", false, "")
3557
}
3658

37-
// AddLoadFlags adds the -mod build flag to the flag set.
38-
func AddLoadFlags(flags *flag.FlagSet) {
39-
flags.StringVar(&cfg.BuildMod, "mod", "", "")
59+
// AddModFlag adds the -mod build flag to the flag set.
60+
func AddModFlag(flags *flag.FlagSet) {
61+
flags.Var(explicitStringFlag{value: &cfg.BuildMod, explicit: &cfg.BuildModExplicit}, "mod", "")
62+
}
63+
64+
// AddModCommonFlags adds the module-related flags common to build commands
65+
// and 'go mod' subcommands.
66+
func AddModCommonFlags(flags *flag.FlagSet) {
67+
flags.BoolVar(&cfg.ModCacheRW, "modcacherw", false, "")
68+
flags.StringVar(&cfg.ModFile, "modfile", "", "")
4069
}

src/cmd/go/internal/cfg/cfg.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ var (
2727
BuildBuildmode string // -buildmode flag
2828
BuildContext = defaultContext()
2929
BuildMod string // -mod flag
30-
BuildModReason string // reason -mod flag is set, if set by default
30+
BuildModExplicit bool // whether -mod was set explicitly
31+
BuildModReason string // reason -mod was set, if set by default
3132
BuildI bool // -i flag
3233
BuildLinkshared bool // -linkshared flag
3334
BuildMSan bool // -msan flag

src/cmd/go/internal/fmtcmd/fmt.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323

2424
func init() {
2525
base.AddBuildFlagsNX(&CmdFmt.Flag)
26-
base.AddLoadFlags(&CmdFmt.Flag)
26+
base.AddModFlag(&CmdFmt.Flag)
27+
base.AddModCommonFlags(&CmdFmt.Flag)
2728
}
2829

2930
var CmdFmt = &base.Command{

src/cmd/go/internal/modcmd/download.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"cmd/go/internal/cfg"
1515
"cmd/go/internal/modfetch"
1616
"cmd/go/internal/modload"
17-
"cmd/go/internal/work"
1817

1918
"golang.org/x/mod/module"
2019
)
@@ -64,7 +63,7 @@ func init() {
6463

6564
// TODO(jayconrod): https://golang.org/issue/35849 Apply -x to other 'go mod' commands.
6665
cmdDownload.Flag.BoolVar(&cfg.BuildX, "x", false, "")
67-
work.AddModCommonFlags(cmdDownload)
66+
base.AddModCommonFlags(&cmdDownload.Flag)
6867
}
6968

7069
type moduleJSON struct {

src/cmd/go/internal/modcmd/edit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"cmd/go/internal/lockedfile"
2020
"cmd/go/internal/modfetch"
2121
"cmd/go/internal/modload"
22-
"cmd/go/internal/work"
2322

2423
"golang.org/x/mod/modfile"
2524
"golang.org/x/mod/module"
@@ -154,7 +153,7 @@ func init() {
154153
cmdEdit.Flag.Var(flagFunc(flagRetract), "retract", "")
155154
cmdEdit.Flag.Var(flagFunc(flagDropRetract), "dropretract", "")
156155

157-
work.AddModCommonFlags(cmdEdit)
156+
base.AddModCommonFlags(&cmdEdit.Flag)
158157
base.AddBuildFlagsNX(&cmdEdit.Flag)
159158
}
160159

src/cmd/go/internal/modcmd/graph.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"cmd/go/internal/base"
1616
"cmd/go/internal/cfg"
1717
"cmd/go/internal/modload"
18-
"cmd/go/internal/work"
1918

2019
"golang.org/x/mod/module"
2120
)
@@ -33,7 +32,7 @@ path@version, except for the main module, which has no @version suffix.
3332
}
3433

3534
func init() {
36-
work.AddModCommonFlags(cmdGraph)
35+
base.AddModCommonFlags(&cmdGraph.Flag)
3736
}
3837

3938
func runGraph(ctx context.Context, cmd *base.Command, args []string) {

src/cmd/go/internal/modcmd/init.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package modcmd
99
import (
1010
"cmd/go/internal/base"
1111
"cmd/go/internal/modload"
12-
"cmd/go/internal/work"
1312
"context"
1413
"os"
1514
"strings"
@@ -30,7 +29,7 @@ To override this guess, supply the module path as an argument.
3029
}
3130

3231
func init() {
33-
work.AddModCommonFlags(cmdInit)
32+
base.AddModCommonFlags(&cmdInit.Flag)
3433
}
3534

3635
func runInit(ctx context.Context, cmd *base.Command, args []string) {

src/cmd/go/internal/modcmd/tidy.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"cmd/go/internal/base"
1111
"cmd/go/internal/cfg"
1212
"cmd/go/internal/modload"
13-
"cmd/go/internal/work"
1413
"context"
1514
)
1615

@@ -32,7 +31,7 @@ to standard error.
3231
func init() {
3332
cmdTidy.Run = runTidy // break init cycle
3433
cmdTidy.Flag.BoolVar(&cfg.BuildV, "v", false, "")
35-
work.AddModCommonFlags(cmdTidy)
34+
base.AddModCommonFlags(&cmdTidy.Flag)
3635
}
3736

3837
func runTidy(ctx context.Context, cmd *base.Command, args []string) {

src/cmd/go/internal/modcmd/vendor.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"cmd/go/internal/cfg"
2020
"cmd/go/internal/imports"
2121
"cmd/go/internal/modload"
22-
"cmd/go/internal/work"
2322

2423
"golang.org/x/mod/module"
2524
"golang.org/x/mod/semver"
@@ -41,7 +40,7 @@ modules and packages to standard error.
4140

4241
func init() {
4342
cmdVendor.Flag.BoolVar(&cfg.BuildV, "v", false, "")
44-
work.AddModCommonFlags(cmdVendor)
43+
base.AddModCommonFlags(&cmdVendor.Flag)
4544
}
4645

4746
func runVendor(ctx context.Context, cmd *base.Command, args []string) {

src/cmd/go/internal/modcmd/verify.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"cmd/go/internal/cfg"
1818
"cmd/go/internal/modfetch"
1919
"cmd/go/internal/modload"
20-
"cmd/go/internal/work"
2120

2221
"golang.org/x/mod/module"
2322
"golang.org/x/mod/sumdb/dirhash"
@@ -38,7 +37,7 @@ non-zero status.
3837
}
3938

4039
func init() {
41-
work.AddModCommonFlags(cmdVerify)
40+
base.AddModCommonFlags(&cmdVerify.Flag)
4241
}
4342

4443
func runVerify(ctx context.Context, cmd *base.Command, args []string) {

0 commit comments

Comments
 (0)