Commit 8ba43eaa authored by Jay Conrod's avatar Jay Conrod

cmd/go: support -modcacherw in 'go mod' subcommands

The -modcacherw flag is now registered in work.AddModCommonFlags,
which is called from work.AddBuildFlags, where it was registered
before. 'go mod' subcommands register the flag by calling
work.AddModCommonFlags directly.

Also, build commands now exit with an error if -modcacherw is set
explicitly (not in GOFLAGS) in GOPATH mode.

Updates #31481

Change-Id: I461e59a51ed31b006fff4d5c57c2a866be0bbf38
Reviewed-on: https://go-review.googlesource.com/c/go/+/202563
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
parent 88186e5e
......@@ -33,7 +33,6 @@ var (
BuildN bool // -n flag
BuildO string // -o flag
BuildP = runtime.NumCPU() // -p flag
BuildModcacheRW bool // -modcacherw flag
BuildPkgdir string // -pkgdir flag
BuildRace bool // -race flag
BuildToolexec []string // -toolexec flag
......@@ -45,6 +44,8 @@ var (
BuildWork bool // -work flag
BuildX bool // -x flag
ModCacheRW bool // -modcacherw flag
CmdName string // "build", "install", "list", "mod tidy", etc.
DebugActiongraph string // -debug-actiongraph flag (undocumented, unstable)
......
......@@ -108,7 +108,7 @@ var (
)
func init() {
work.AddBuildFlags(CmdGet, work.OmitModFlag)
work.AddBuildFlags(CmdGet, work.OmitModFlag|work.OmitModCommonFlags)
CmdGet.Run = runGet // break init loop
CmdGet.Flag.BoolVar(&Insecure, "insecure", Insecure, "")
}
......
......@@ -14,6 +14,7 @@ import (
"cmd/go/internal/modload"
"cmd/go/internal/module"
"cmd/go/internal/par"
"cmd/go/internal/work"
)
var cmdDownload = &base.Command{
......@@ -53,6 +54,8 @@ var downloadJSON = cmdDownload.Flag.Bool("json", false, "")
func init() {
cmdDownload.Run = runDownload // break init cycle
work.AddModCommonFlags(cmdDownload)
}
type moduleJSON struct {
......
......@@ -20,6 +20,7 @@ import (
"cmd/go/internal/modfile"
"cmd/go/internal/modload"
"cmd/go/internal/module"
"cmd/go/internal/work"
)
var cmdEdit = &base.Command{
......@@ -130,6 +131,7 @@ func init() {
cmdEdit.Flag.Var(flagFunc(flagReplace), "replace", "")
cmdEdit.Flag.Var(flagFunc(flagDropExclude), "dropexclude", "")
work.AddModCommonFlags(cmdEdit)
base.AddBuildFlagsNX(&cmdEdit.Flag)
}
......
......@@ -16,6 +16,7 @@ import (
"cmd/go/internal/modload"
"cmd/go/internal/module"
"cmd/go/internal/par"
"cmd/go/internal/work"
)
var cmdGraph = &base.Command{
......@@ -30,6 +31,10 @@ path@version, except for the main module, which has no @version suffix.
Run: runGraph,
}
func init() {
work.AddModCommonFlags(cmdGraph)
}
func runGraph(cmd *base.Command, args []string) {
if len(args) > 0 {
base.Fatalf("go mod graph: graph takes no arguments")
......
......@@ -9,6 +9,7 @@ package modcmd
import (
"cmd/go/internal/base"
"cmd/go/internal/modload"
"cmd/go/internal/work"
"os"
"strings"
)
......@@ -27,6 +28,10 @@ To override this guess, supply the module path as an argument.
Run: runInit,
}
func init() {
work.AddModCommonFlags(cmdInit)
}
func runInit(cmd *base.Command, args []string) {
modload.CmdModInit = true
if len(args) > 1 {
......
......@@ -15,6 +15,7 @@ import (
"cmd/go/internal/modfetch"
"cmd/go/internal/modload"
"cmd/go/internal/module"
"cmd/go/internal/work"
)
var cmdTidy = &base.Command{
......@@ -35,6 +36,7 @@ to standard error.
func init() {
cmdTidy.Run = runTidy // break init cycle
cmdTidy.Flag.BoolVar(&cfg.BuildV, "v", false, "")
work.AddModCommonFlags(cmdTidy)
}
func runTidy(cmd *base.Command, args []string) {
......
......@@ -20,6 +20,7 @@ import (
"cmd/go/internal/modload"
"cmd/go/internal/module"
"cmd/go/internal/semver"
"cmd/go/internal/work"
)
var cmdVendor = &base.Command{
......@@ -38,6 +39,7 @@ modules and packages to standard error.
func init() {
cmdVendor.Flag.BoolVar(&cfg.BuildV, "v", false, "")
work.AddModCommonFlags(cmdVendor)
}
func runVendor(cmd *base.Command, args []string) {
......
......@@ -16,6 +16,7 @@ import (
"cmd/go/internal/modfetch"
"cmd/go/internal/modload"
"cmd/go/internal/module"
"cmd/go/internal/work"
)
var cmdVerify = &base.Command{
......@@ -32,6 +33,10 @@ non-zero status.
Run: runVerify,
}
func init() {
work.AddModCommonFlags(cmdVerify)
}
func runVerify(cmd *base.Command, args []string) {
if len(args) != 0 {
// NOTE(rsc): Could take a module pattern.
......
......@@ -8,6 +8,7 @@ import (
"cmd/go/internal/base"
"cmd/go/internal/modload"
"cmd/go/internal/module"
"cmd/go/internal/work"
"fmt"
"strings"
)
......@@ -54,6 +55,7 @@ var (
func init() {
cmdWhy.Run = runWhy // break init cycle
work.AddModCommonFlags(cmdWhy)
}
func runWhy(cmd *base.Command, args []string) {
......
......@@ -125,7 +125,7 @@ func download(mod module.Version, dir string) (err error) {
return err
}
if !cfg.BuildModcacheRW {
if !cfg.ModCacheRW {
// Make dir read-only only *after* renaming it.
// os.Rename was observed to fail for read-only directories on macOS.
makeDirsReadOnly(dir)
......
......@@ -224,9 +224,10 @@ type BuildFlagMask int
const (
DefaultBuildFlags BuildFlagMask = 0
OmitModFlag BuildFlagMask = 1 << iota
OmitModCommonFlags
)
// addBuildFlags adds the flags common to the build, clean, get,
// AddBuildFlags adds the flags common to the build, clean, get,
// install, list, run, and test commands.
func AddBuildFlags(cmd *base.Command, mask BuildFlagMask) {
cmd.Flag.BoolVar(&cfg.BuildA, "a", false, "")
......@@ -243,10 +244,12 @@ func AddBuildFlags(cmd *base.Command, mask BuildFlagMask) {
if mask&OmitModFlag == 0 {
cmd.Flag.StringVar(&cfg.BuildMod, "mod", "", "")
}
if mask&OmitModCommonFlags == 0 {
AddModCommonFlags(cmd)
}
cmd.Flag.StringVar(&cfg.BuildContext.InstallSuffix, "installsuffix", "", "")
cmd.Flag.Var(&load.BuildLdflags, "ldflags", "")
cmd.Flag.BoolVar(&cfg.BuildLinkshared, "linkshared", false, "")
cmd.Flag.BoolVar(&cfg.BuildModcacheRW, "modcacherw", false, "")
cmd.Flag.StringVar(&cfg.BuildPkgdir, "pkgdir", "", "")
cmd.Flag.BoolVar(&cfg.BuildRace, "race", false, "")
cmd.Flag.BoolVar(&cfg.BuildMSan, "msan", false, "")
......@@ -259,6 +262,12 @@ func AddBuildFlags(cmd *base.Command, mask BuildFlagMask) {
cmd.Flag.StringVar(&cfg.DebugActiongraph, "debug-actiongraph", "", "")
}
// AddModCommonFlags adds the module-related flags common to build commands
// and 'go mod' subcommands.
func AddModCommonFlags(cmd *base.Command) {
cmd.Flag.BoolVar(&cfg.ModCacheRW, "modcacherw", false, "")
}
// tagsFlag is the implementation of the -tags flag.
type tagsFlag []string
......
......@@ -248,12 +248,17 @@ func buildModeInit() {
case "":
// ok
case "readonly", "vendor", "mod":
if load.ModLookup == nil && !inGOFLAGS("-mod") {
if !cfg.ModulesEnabled && !inGOFLAGS("-mod") {
base.Fatalf("build flag -mod=%s only valid when using modules", cfg.BuildMod)
}
default:
base.Fatalf("-mod=%s not supported (can be '', 'mod', 'readonly', or 'vendor')", cfg.BuildMod)
}
if !cfg.ModulesEnabled {
if cfg.ModCacheRW && !inGOFLAGS("-modcacherw") {
base.Fatalf("build flag -modcacherw only valid when using modules")
}
}
}
func inGOFLAGS(flag string) bool {
......
......@@ -33,6 +33,16 @@ go get -d rsc.io/quote@latest
[!windows] [!root] ! cp $WORK/extraneous.txt $GOPATH/pkg/mod/rsc.io/quote@v1.5.2/extraneous_file.go
! exists $GOPATH/pkg/mod/rsc.io/quote@v1.5.2/extraneous_file.go
# Repeat part of the test with 'go mod download' instead of 'go get' to verify
# -modcacherw is supported on 'go mod' subcommands.
go clean -modcache
go mod download -modcacherw rsc.io/quote
cp $WORK/extraneous.txt $GOPATH/pkg/mod/rsc.io/quote@v1.5.2/extraneous_file.go
! go mod verify
[!root] ! cp $WORK/extraneous.txt $GOPATH/pkg/mod/rsc.io/quote@v1.5.2/go.mod
-- $WORK/extraneous.txt --
module oops
-- go.mod --
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment