diff options
author | Jay Conrod <jayconrod@google.com> | 2021-07-20 14:46:40 -0700 |
---|---|---|
committer | Jay Conrod <jayconrod@google.com> | 2021-08-13 00:19:50 +0000 |
commit | 4be75faa3ee79a273ff82d4f5b7f838ef3642f9d (patch) | |
tree | 3da569506b191359f7931a1435a3f2b3939a2605 /src/cmd/go/internal/modload/init.go | |
parent | 1fffeddfe9c977510d855277da57e0564700d6c3 (diff) | |
download | go-4be75faa3ee79a273ff82d4f5b7f838ef3642f9d.tar.gz go-4be75faa3ee79a273ff82d4f5b7f838ef3642f9d.zip |
cmd/go: make fewer 'go mod' commands update go.mod
'go mod graph', 'go mod vendor', 'go mod verify', and 'go mod why'
will no longer edit go.mod or go.sum.
'go mod graph', 'go mod verify', and 'go mod why' may still fetch
files and look up packages as if they were able to update
go.mod. They're useful for debugging and should still work when go.mod
is a little broken.
This is implemented in modload.setDefaultBuildMod based on command
name for now. Super gross. Sorry. This should be fixed with a larger
refactoring for #40775.
Fixes golang/go#45551
Change-Id: If5f225937180d32e9a5dd252c78d988042bbdedf
Reviewed-on: https://go-review.googlesource.com/c/go/+/336151
Trust: Jay Conrod <jayconrod@google.com>
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-on: https://go-review.googlesource.com/c/go/+/341933
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/go/internal/modload/init.go')
-rw-r--r-- | src/cmd/go/internal/modload/init.go | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index 45f724d5e3..d5f9d10422 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -686,12 +686,25 @@ func setDefaultBuildMod() { return } - if cfg.CmdName == "get" || strings.HasPrefix(cfg.CmdName, "mod ") { - // 'get' and 'go mod' commands may update go.mod automatically. - // TODO(jayconrod): should this narrower? Should 'go mod download' or - // 'go mod graph' update go.mod by default? + // TODO(#40775): commands should pass in the module mode as an option + // to modload functions instead of relying on an implicit setting + // based on command name. + switch cfg.CmdName { + case "get", "mod download", "mod init", "mod tidy": + // These commands are intended to update go.mod and go.sum. cfg.BuildMod = "mod" return + case "mod graph", "mod verify", "mod why": + // These commands should not update go.mod or go.sum, but they should be + // able to fetch modules not in go.sum and should not report errors if + // go.mod is inconsistent. They're useful for debugging, and they need + // to work in buggy situations. + cfg.BuildMod = "mod" + allowWriteGoMod = false + return + case "mod vendor": + cfg.BuildMod = "readonly" + return } if modRoot == "" { if allowMissingModuleImports { |