aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modload/init.go
diff options
context:
space:
mode:
authorJay Conrod <jayconrod@google.com>2020-12-21 18:06:35 -0500
committerJay Conrod <jayconrod@google.com>2021-01-05 23:31:36 +0000
commit3e1e13ce6d1271f49f3d8ee359689145a6995bad (patch)
treee5e545f6517ecb15ad231e6dcb9fecb1eb4506a8 /src/cmd/go/internal/modload/init.go
parent0b0d004983b5f06d7e8ae2084fc7d6612f1aa869 (diff)
downloadgo-3e1e13ce6d1271f49f3d8ee359689145a6995bad.tar.gz
go-3e1e13ce6d1271f49f3d8ee359689145a6995bad.zip
cmd/go: set cfg.BuildMod to "readonly" by default with no module root
modload.Init now sets the default value for -mod if it wasn't set explicitly. This happens before go.mod is loaded, so modload.LoadModFile sets the default value again in order to enable automatic vendoring. Previously, cfg.BuildMod wasn't set at all if LoadModFile wasn't called, as is the case for commands that run outside of a module root. This problem only affected 'go install pkg@version' since other commands are either forbidden in module mode or run with -mod=mod (like 'go get' and 'go mod' subcommands). This change also suppresses "missing sum" errors when -mod=readonly is enabled and there is no module root. Fixes #43278 Related #40278 Change-Id: I6071cc42bc5e24d0d7e84556e5bfd8e368e0019d Reviewed-on: https://go-review.googlesource.com/c/go/+/279490 Run-TryBot: Jay Conrod <jayconrod@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Jay Conrod <jayconrod@google.com> 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.go16
1 files changed, 8 insertions, 8 deletions
diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go
index 445ebb262f..b0acb7b25d 100644
--- a/src/cmd/go/internal/modload/init.go
+++ b/src/cmd/go/internal/modload/init.go
@@ -202,6 +202,8 @@ func Init() {
}
// We're in module mode. Set any global variables that need to be set.
+ cfg.ModulesEnabled = true
+ setDefaultBuildMod()
list := filepath.SplitList(cfg.BuildContext.GOPATH)
if len(list) == 0 || list[0] == "" {
base.Fatalf("missing $GOPATH")
@@ -211,8 +213,6 @@ func Init() {
base.Fatalf("$GOPATH/go.mod exists but should not")
}
- cfg.ModulesEnabled = true
-
if modRoot == "" {
// We're in module mode, but not inside a module.
//
@@ -348,8 +348,8 @@ func die() {
// ensuring requirements are consistent. WriteGoMod should be called later to
// write changes out to disk or report errors in readonly mode.
//
-// As a side-effect, LoadModFile sets a default for cfg.BuildMod if it does not
-// already have an explicit value.
+// As a side-effect, LoadModFile may change cfg.BuildMod to "vendor" if
+// -mod wasn't set explicitly and automatic vendoring should be enabled.
func LoadModFile(ctx context.Context) {
if len(buildList) > 0 {
return
@@ -387,7 +387,7 @@ func LoadModFile(ctx context.Context) {
base.Fatalf("go: %v", err)
}
- setDefaultBuildMod()
+ setDefaultBuildMod() // possibly enable automatic vendoring
modFileToBuildList()
if cfg.BuildMod == "vendor" {
readVendorList()
@@ -586,8 +586,8 @@ func modFileToBuildList() {
buildList = list
}
-// setDefaultBuildMod sets a default value for cfg.BuildMod
-// if it is currently empty.
+// setDefaultBuildMod sets a default value for cfg.BuildMod if the -mod flag
+// wasn't provided. setDefaultBuildMod may be called multiple times.
func setDefaultBuildMod() {
if cfg.BuildModExplicit {
// Don't override an explicit '-mod=' argument.
@@ -608,7 +608,7 @@ func setDefaultBuildMod() {
if fi, err := fsys.Stat(filepath.Join(modRoot, "vendor")); err == nil && fi.IsDir() {
modGo := "unspecified"
- if index.goVersionV != "" {
+ if index != nil && index.goVersionV != "" {
if semver.Compare(index.goVersionV, "v1.14") >= 0 {
// The Go version is at least 1.14, and a vendor directory exists.
// Set -mod=vendor by default.