From 6145a80608087b309251b4edf1a612739331bdee Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 2 Oct 2019 10:51:09 -0400 Subject: [PATCH] cmd/go: remove the -mod flag from 'go get' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 'GOFLAGS=-mod=vendor' currently causes 'go get' to always fail unless the '-mod' flag is explicitly overwritten. Moreover, as of CL 198319 we plan to set -mod=vendor by default if a vendor directory is present, so all users with vendor directories will be affected — not just those who set 'GOFLAGS' explicitly. Similarly, an explicit '-mod=readonly' argument to 'go get' is currently ignored as a special case, but the fact that it is ignored (rather than rejected) can be very surprising. Rather than adding more special cases, we should remove the '-mod' flag from 'go get' entirely. Fixes #30345 Fixes #32502 Updates #33848 Change-Id: Iecd3233ca3ef580ca3a66bd5e6ee8d86d4cbd8a7 Reviewed-on: https://go-review.googlesource.com/c/go/+/198438 Run-TryBot: Bryan C. Mills TryBot-Result: Gobot Gobot Reviewed-by: Jay Conrod --- doc/go1.14.html | 7 +++++++ src/cmd/go/internal/clean/clean.go | 2 +- src/cmd/go/internal/generate/generate.go | 2 +- src/cmd/go/internal/get/get.go | 2 +- src/cmd/go/internal/list/list.go | 2 +- src/cmd/go/internal/modget/get.go | 12 +----------- src/cmd/go/internal/run/run.go | 2 +- src/cmd/go/internal/test/testflag.go | 2 +- src/cmd/go/internal/vet/vetflag.go | 2 +- src/cmd/go/internal/work/build.go | 17 +++++++++++++---- .../go/testdata/script/mod_getmode_vendor.txt | 2 +- 11 files changed, 29 insertions(+), 23 deletions(-) diff --git a/doc/go1.14.html b/doc/go1.14.html index e68cca56df1..322481c9e36 100644 --- a/doc/go1.14.html +++ b/doc/go1.14.html @@ -58,6 +58,13 @@ TODO graphic characters and spaces.

+

+ The go get subcommand no longer accepts + the -mod flag. Previously, the flag's setting either + was ignored or + caused the build to fail. +

+

Runtime

diff --git a/src/cmd/go/internal/clean/clean.go b/src/cmd/go/internal/clean/clean.go index f7d80ff6dc8..5f4bf4e6c8e 100644 --- a/src/cmd/go/internal/clean/clean.go +++ b/src/cmd/go/internal/clean/clean.go @@ -102,7 +102,7 @@ func init() { // mentioned explicitly in the docs but they // are part of the build flags. - work.AddBuildFlags(CmdClean) + work.AddBuildFlags(CmdClean, work.DefaultBuildFlags) } func runClean(cmd *base.Command, args []string) { diff --git a/src/cmd/go/internal/generate/generate.go b/src/cmd/go/internal/generate/generate.go index f2ae80e5dc6..198ca1c1b94 100644 --- a/src/cmd/go/internal/generate/generate.go +++ b/src/cmd/go/internal/generate/generate.go @@ -149,7 +149,7 @@ var ( ) func init() { - work.AddBuildFlags(CmdGenerate) + work.AddBuildFlags(CmdGenerate, work.DefaultBuildFlags) CmdGenerate.Flag.StringVar(&generateRunFlag, "run", "", "") } diff --git a/src/cmd/go/internal/get/get.go b/src/cmd/go/internal/get/get.go index e4945fe1440..44fd316f353 100644 --- a/src/cmd/go/internal/get/get.go +++ b/src/cmd/go/internal/get/get.go @@ -108,7 +108,7 @@ var ( ) func init() { - work.AddBuildFlags(CmdGet) + work.AddBuildFlags(CmdGet, work.OmitModFlag) CmdGet.Run = runGet // break init loop CmdGet.Flag.BoolVar(&Insecure, "insecure", Insecure, "") } diff --git a/src/cmd/go/internal/list/list.go b/src/cmd/go/internal/list/list.go index a5f1abe64ae..67819939e60 100644 --- a/src/cmd/go/internal/list/list.go +++ b/src/cmd/go/internal/list/list.go @@ -287,7 +287,7 @@ For more about modules, see 'go help modules'. func init() { CmdList.Run = runList // break init cycle - work.AddBuildFlags(CmdList) + work.AddBuildFlags(CmdList, work.DefaultBuildFlags) } var ( diff --git a/src/cmd/go/internal/modget/get.go b/src/cmd/go/internal/modget/get.go index 1cae311c4c1..ced5abcc71b 100644 --- a/src/cmd/go/internal/modget/get.go +++ b/src/cmd/go/internal/modget/get.go @@ -7,7 +7,6 @@ package modget import ( "cmd/go/internal/base" - "cmd/go/internal/cfg" "cmd/go/internal/get" "cmd/go/internal/imports" "cmd/go/internal/load" @@ -199,7 +198,7 @@ func (v *upgradeFlag) Set(s string) error { func (v *upgradeFlag) String() string { return "" } func init() { - work.AddBuildFlags(CmdGet) + work.AddBuildFlags(CmdGet, work.OmitModFlag) CmdGet.Run = runGet // break init loop CmdGet.Flag.BoolVar(&get.Insecure, "insecure", get.Insecure, "") CmdGet.Flag.Var(&getU, "u", "") @@ -256,11 +255,6 @@ type query struct { } func runGet(cmd *base.Command, args []string) { - // -mod=readonly has no effect on "go get". - if cfg.BuildMod == "readonly" { - cfg.BuildMod = "" - } - switch getU { case "", "upgrade", "patch": // ok @@ -278,10 +272,6 @@ func runGet(cmd *base.Command, args []string) { } modload.LoadTests = *getT - if cfg.BuildMod == "vendor" { - base.Fatalf("go get: disabled by -mod=%s", cfg.BuildMod) - } - buildList := modload.LoadBuildList() buildList = buildList[:len(buildList):len(buildList)] // copy on append versionByPath := make(map[string]string) diff --git a/src/cmd/go/internal/run/run.go b/src/cmd/go/internal/run/run.go index 71da5adc934..648a87f6654 100644 --- a/src/cmd/go/internal/run/run.go +++ b/src/cmd/go/internal/run/run.go @@ -49,7 +49,7 @@ See also: go build. func init() { CmdRun.Run = runRun // break init loop - work.AddBuildFlags(CmdRun) + work.AddBuildFlags(CmdRun, work.DefaultBuildFlags) CmdRun.Flag.Var((*base.StringsFlag)(&work.ExecCmd), "exec", "") } diff --git a/src/cmd/go/internal/test/testflag.go b/src/cmd/go/internal/test/testflag.go index 138e1f9d2a2..79dc5eb2a0c 100644 --- a/src/cmd/go/internal/test/testflag.go +++ b/src/cmd/go/internal/test/testflag.go @@ -65,7 +65,7 @@ var testFlagDefn = []*cmdflag.Defn{ func init() { cmdflag.AddKnownFlags("test", testFlagDefn) var cmd base.Command - work.AddBuildFlags(&cmd) + work.AddBuildFlags(&cmd, work.DefaultBuildFlags) cmd.Flag.VisitAll(func(f *flag.Flag) { if f.Name == "v" { // test overrides the build -v flag diff --git a/src/cmd/go/internal/vet/vetflag.go b/src/cmd/go/internal/vet/vetflag.go index cbe7f8ce08c..7179f73cfcd 100644 --- a/src/cmd/go/internal/vet/vetflag.go +++ b/src/cmd/go/internal/vet/vetflag.go @@ -114,7 +114,7 @@ func vetFlags(usage func(), args []string) (passToVet, packageNames []string) { // Add build flags to vetFlagDefn. var cmd base.Command - work.AddBuildFlags(&cmd) + work.AddBuildFlags(&cmd, work.DefaultBuildFlags) // This flag declaration is a placeholder: // -vettool is actually parsed by the init function above. cmd.Flag.StringVar(new(string), "vettool", "", "path to vet tool binary") diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index 9d6fa0c25bf..1fc47a36c74 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -167,8 +167,8 @@ func init() { CmdInstall.Flag.BoolVar(&cfg.BuildI, "i", false, "") - AddBuildFlags(CmdBuild) - AddBuildFlags(CmdInstall) + AddBuildFlags(CmdBuild, DefaultBuildFlags) + AddBuildFlags(CmdInstall, DefaultBuildFlags) } // Note that flags consulted by other parts of the code @@ -216,9 +216,16 @@ func init() { } } +type BuildFlagMask int + +const ( + DefaultBuildFlags BuildFlagMask = 0 + OmitModFlag BuildFlagMask = 1 << iota +) + // addBuildFlags adds the flags common to the build, clean, get, // install, list, run, and test commands. -func AddBuildFlags(cmd *base.Command) { +func AddBuildFlags(cmd *base.Command, mask BuildFlagMask) { cmd.Flag.BoolVar(&cfg.BuildA, "a", false, "") cmd.Flag.BoolVar(&cfg.BuildN, "n", false, "") cmd.Flag.IntVar(&cfg.BuildP, "p", cfg.BuildP, "") @@ -230,7 +237,9 @@ func AddBuildFlags(cmd *base.Command) { cmd.Flag.StringVar(&cfg.BuildBuildmode, "buildmode", "default", "") cmd.Flag.Var(&load.BuildGcflags, "gcflags", "") cmd.Flag.Var(&load.BuildGccgoflags, "gccgoflags", "") - cmd.Flag.StringVar(&cfg.BuildMod, "mod", "", "") + if mask&OmitModFlag == 0 { + cmd.Flag.StringVar(&cfg.BuildMod, "mod", "", "") + } cmd.Flag.StringVar(&cfg.BuildContext.InstallSuffix, "installsuffix", "", "") cmd.Flag.Var(&load.BuildLdflags, "ldflags", "") cmd.Flag.BoolVar(&cfg.BuildLinkshared, "linkshared", false, "") diff --git a/src/cmd/go/testdata/script/mod_getmode_vendor.txt b/src/cmd/go/testdata/script/mod_getmode_vendor.txt index 7e1f6aa323e..c532cee9cbd 100644 --- a/src/cmd/go/testdata/script/mod_getmode_vendor.txt +++ b/src/cmd/go/testdata/script/mod_getmode_vendor.txt @@ -13,7 +13,7 @@ stdout '^golang.org/x/text v0.0.0.* .*vendor[\\/]golang.org[\\/]x[\\/]text$' ! go list -mod=vendor -m rsc.io/quote@latest stderr 'module lookup disabled by -mod=vendor' ! go get -mod=vendor -u -stderr 'go get: disabled by -mod=vendor' +stderr 'flag provided but not defined: -mod' -- go.mod -- module x