Skip to content

Commit 3e1e13c

Browse files
author
Jay Conrod
committed
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 <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 0b0d004 commit 3e1e13c

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

src/cmd/go/internal/modload/import.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (e *ImportMissingError) Error() string {
5858
if e.QueryErr != nil {
5959
return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr)
6060
}
61-
if cfg.BuildMod == "mod" {
61+
if cfg.BuildMod == "mod" || (cfg.BuildMod == "readonly" && allowMissingModuleImports) {
6262
return "cannot find module providing package " + e.Path
6363
}
6464

@@ -365,7 +365,7 @@ func queryImport(ctx context.Context, path string) (module.Version, error) {
365365
return module.Version{}, &ImportMissingError{Path: path, isStd: true}
366366
}
367367

368-
if cfg.BuildMod == "readonly" {
368+
if cfg.BuildMod == "readonly" && !allowMissingModuleImports {
369369
// In readonly mode, we can't write go.mod, so we shouldn't try to look up
370370
// the module. If readonly mode was enabled explicitly, include that in
371371
// the error message.
@@ -547,7 +547,7 @@ func fetch(ctx context.Context, mod module.Version, needSum bool) (dir string, i
547547
mod = r
548548
}
549549

550-
if cfg.BuildMod == "readonly" && needSum && !modfetch.HaveSum(mod) {
550+
if HasModRoot() && cfg.BuildMod == "readonly" && needSum && !modfetch.HaveSum(mod) {
551551
return "", false, module.VersionError(mod, &sumMissingError{})
552552
}
553553

src/cmd/go/internal/modload/import_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,15 @@ var importTests = []struct {
5858
func TestQueryImport(t *testing.T) {
5959
testenv.MustHaveExternalNetwork(t)
6060
testenv.MustHaveExecPath(t, "git")
61-
defer func(old bool) {
62-
allowMissingModuleImports = old
63-
}(allowMissingModuleImports)
64-
AllowMissingModuleImports()
61+
62+
oldAllowMissingModuleImports := allowMissingModuleImports
63+
oldRootMode := RootMode
64+
defer func() {
65+
allowMissingModuleImports = oldAllowMissingModuleImports
66+
RootMode = oldRootMode
67+
}()
68+
allowMissingModuleImports = true
69+
RootMode = NoRoot
6570

6671
ctx := context.Background()
6772

src/cmd/go/internal/modload/init.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ func Init() {
202202
}
203203

204204
// We're in module mode. Set any global variables that need to be set.
205+
cfg.ModulesEnabled = true
206+
setDefaultBuildMod()
205207
list := filepath.SplitList(cfg.BuildContext.GOPATH)
206208
if len(list) == 0 || list[0] == "" {
207209
base.Fatalf("missing $GOPATH")
@@ -211,8 +213,6 @@ func Init() {
211213
base.Fatalf("$GOPATH/go.mod exists but should not")
212214
}
213215

214-
cfg.ModulesEnabled = true
215-
216216
if modRoot == "" {
217217
// We're in module mode, but not inside a module.
218218
//
@@ -348,8 +348,8 @@ func die() {
348348
// ensuring requirements are consistent. WriteGoMod should be called later to
349349
// write changes out to disk or report errors in readonly mode.
350350
//
351-
// As a side-effect, LoadModFile sets a default for cfg.BuildMod if it does not
352-
// already have an explicit value.
351+
// As a side-effect, LoadModFile may change cfg.BuildMod to "vendor" if
352+
// -mod wasn't set explicitly and automatic vendoring should be enabled.
353353
func LoadModFile(ctx context.Context) {
354354
if len(buildList) > 0 {
355355
return
@@ -387,7 +387,7 @@ func LoadModFile(ctx context.Context) {
387387
base.Fatalf("go: %v", err)
388388
}
389389

390-
setDefaultBuildMod()
390+
setDefaultBuildMod() // possibly enable automatic vendoring
391391
modFileToBuildList()
392392
if cfg.BuildMod == "vendor" {
393393
readVendorList()
@@ -586,8 +586,8 @@ func modFileToBuildList() {
586586
buildList = list
587587
}
588588

589-
// setDefaultBuildMod sets a default value for cfg.BuildMod
590-
// if it is currently empty.
589+
// setDefaultBuildMod sets a default value for cfg.BuildMod if the -mod flag
590+
// wasn't provided. setDefaultBuildMod may be called multiple times.
591591
func setDefaultBuildMod() {
592592
if cfg.BuildModExplicit {
593593
// Don't override an explicit '-mod=' argument.
@@ -608,7 +608,7 @@ func setDefaultBuildMod() {
608608

609609
if fi, err := fsys.Stat(filepath.Join(modRoot, "vendor")); err == nil && fi.IsDir() {
610610
modGo := "unspecified"
611-
if index.goVersionV != "" {
611+
if index != nil && index.goVersionV != "" {
612612
if semver.Compare(index.goVersionV, "v1.14") >= 0 {
613613
// The Go version is at least 1.14, and a vendor directory exists.
614614
// Set -mod=vendor by default.

src/cmd/go/internal/modload/modfile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func goModSummary(m module.Version) (*modFileSummary, error) {
446446
if actual.Path == "" {
447447
actual = m
448448
}
449-
if cfg.BuildMod == "readonly" && actual.Version != "" {
449+
if HasModRoot() && cfg.BuildMod == "readonly" && actual.Version != "" {
450450
key := module.Version{Path: actual.Path, Version: actual.Version + "/go.mod"}
451451
if !modfetch.HaveSum(key) {
452452
suggestion := fmt.Sprintf("; try 'go mod download %s' to add it", m.Path)

src/cmd/go/testdata/script/mod_install_pkg_version.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ stdout '^\tmod\texample.com/cmd\tv1.0.0\t'
175175
go install example.com/cmd/[email protected]
176176
go version -m $GOPATH/bin/a$GOEXE
177177
stdout '^\tmod\texample.com/cmd\tv1.9.0\t'
178+
env GO111MODULE=
179+
180+
# 'go install pkg@version' succeeds when -mod=readonly is set explicitly.
181+
# Verifies #43278.
182+
go install -mod=readonly example.com/cmd/[email protected]
178183

179184
-- m/go.mod --
180185
module m

0 commit comments

Comments
 (0)