Skip to content

Commit 0a95856

Browse files
committed
cmd/go: eliminate additional global variable
Move global variable to a field on the State type. Change-Id: I1edd32e1d28ce814bcd75501098ee4b22227546b Reviewed-on: https://go-review.googlesource.com/c/go/+/716162 Reviewed-by: Michael Matloob <matloob@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@google.com>
1 parent f93186f commit 0a95856

File tree

7 files changed

+47
-45
lines changed

7 files changed

+47
-45
lines changed

src/cmd/go/internal/modget/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
308308

309309
// Allow looking up modules for import paths when outside of a module.
310310
// 'go get' is expected to do this, unlike other commands.
311-
modload.AllowMissingModuleImports(moduleLoaderState)
311+
moduleLoaderState.AllowMissingModuleImports()
312312

313313
// 'go get' no longer builds or installs packages, so there's nothing to do
314314
// if there's no go.mod file.

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

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ import (
2929
)
3030

3131
type ImportMissingError struct {
32-
Path string
33-
Module module.Version
34-
QueryErr error
35-
modContainingCWD module.Version
32+
Path string
33+
Module module.Version
34+
QueryErr error
35+
modContainingCWD module.Version
36+
allowMissingModuleImports bool
3637

3738
// modRoot is dependent on the value of ImportingMainModule and should be
3839
// kept in sync.
@@ -70,7 +71,7 @@ func (e *ImportMissingError) Error() string {
7071
if e.QueryErr != nil && !errors.Is(e.QueryErr, ErrNoModRoot) {
7172
return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr)
7273
}
73-
if cfg.BuildMod == "mod" || (cfg.BuildMod == "readonly" && allowMissingModuleImports) {
74+
if cfg.BuildMod == "mod" || (cfg.BuildMod == "readonly" && e.allowMissingModuleImports) {
7475
return "cannot find module providing package " + e.Path
7576
}
7677

@@ -373,8 +374,9 @@ func importFromModules(loaderstate *State, ctx context.Context, path string, rs
373374

374375
if len(mods) == 0 {
375376
return module.Version{}, "", "", nil, &ImportMissingError{
376-
Path: path,
377-
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
377+
Path: path,
378+
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
379+
allowMissingModuleImports: loaderstate.allowMissingModuleImports,
378380
}
379381
}
380382

@@ -494,10 +496,11 @@ func importFromModules(loaderstate *State, ctx context.Context, path string, rs
494496
queryErr = NewNoMainModulesError(loaderstate)
495497
}
496498
return module.Version{}, "", "", nil, &ImportMissingError{
497-
Path: path,
498-
QueryErr: queryErr,
499-
isStd: pathIsStd,
500-
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
499+
Path: path,
500+
QueryErr: queryErr,
501+
isStd: pathIsStd,
502+
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
503+
allowMissingModuleImports: loaderstate.allowMissingModuleImports,
501504
}
502505
}
503506

@@ -571,9 +574,10 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
571574
} else if ok {
572575
if cfg.BuildMod == "readonly" {
573576
return module.Version{}, &ImportMissingError{
574-
Path: path,
575-
replaced: m,
576-
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
577+
Path: path,
578+
replaced: m,
579+
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
580+
allowMissingModuleImports: loaderstate.allowMissingModuleImports,
577581
}
578582
}
579583
return m, nil
@@ -601,13 +605,14 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
601605
//
602606
// Instead of trying QueryPattern, report an ImportMissingError immediately.
603607
return module.Version{}, &ImportMissingError{
604-
Path: path,
605-
isStd: true,
606-
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
608+
Path: path,
609+
isStd: true,
610+
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
611+
allowMissingModuleImports: loaderstate.allowMissingModuleImports,
607612
}
608613
}
609614

610-
if (cfg.BuildMod == "readonly" || cfg.BuildMod == "vendor") && !allowMissingModuleImports {
615+
if (cfg.BuildMod == "readonly" || cfg.BuildMod == "vendor") && !loaderstate.allowMissingModuleImports {
611616
// In readonly mode, we can't write go.mod, so we shouldn't try to look up
612617
// the module. If readonly mode was enabled explicitly, include that in
613618
// the error message.
@@ -620,9 +625,10 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
620625
queryErr = fmt.Errorf("import lookup disabled by -mod=%s\n\t(%s)", cfg.BuildMod, cfg.BuildModReason)
621626
}
622627
return module.Version{}, &ImportMissingError{
623-
Path: path,
624-
QueryErr: queryErr,
625-
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
628+
Path: path,
629+
QueryErr: queryErr,
630+
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
631+
allowMissingModuleImports: loaderstate.allowMissingModuleImports,
626632
}
627633
}
628634

@@ -642,9 +648,10 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
642648
// Return "cannot find module providing package […]" instead of whatever
643649
// low-level error QueryPattern produced.
644650
return module.Version{}, &ImportMissingError{
645-
Path: path,
646-
QueryErr: err,
647-
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
651+
Path: path,
652+
QueryErr: err,
653+
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
654+
allowMissingModuleImports: loaderstate.allowMissingModuleImports,
648655
}
649656
} else {
650657
return module.Version{}, err
@@ -670,10 +677,11 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
670677
return c.Mod, nil
671678
}
672679
return module.Version{}, &ImportMissingError{
673-
Path: path,
674-
Module: candidates[0].Mod,
675-
newMissingVersion: candidate0MissingVersion,
676-
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
680+
Path: path,
681+
Module: candidates[0].Mod,
682+
newMissingVersion: candidate0MissingVersion,
683+
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
684+
allowMissingModuleImports: loaderstate.allowMissingModuleImports,
677685
}
678686
}
679687

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,11 @@ var importTests = []struct {
5858
func TestQueryImport(t *testing.T) {
5959
loaderstate := NewState()
6060
loaderstate.RootMode = NoRoot
61+
loaderstate.AllowMissingModuleImports()
6162

6263
testenv.MustHaveExternalNetwork(t)
6364
testenv.MustHaveExecPath(t, "git")
6465

65-
oldAllowMissingModuleImports := allowMissingModuleImports
66-
defer func() {
67-
allowMissingModuleImports = oldAllowMissingModuleImports
68-
}()
69-
allowMissingModuleImports = true
70-
7166
ctx := context.Background()
7267
rs := LoadModFile(loaderstate, ctx)
7368

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ import (
3838
//
3939
// TODO(#40775): See if these can be plumbed as explicit parameters.
4040
var (
41-
allowMissingModuleImports bool
42-
4341
// ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions
4442
// from updating go.mod and go.sum or reporting errors when updates are
4543
// needed. A package should set this if it would cause go.mod to be written
@@ -415,7 +413,8 @@ func (s *State) setState(new State) State {
415413
}
416414

417415
type State struct {
418-
initialized bool
416+
initialized bool
417+
allowMissingModuleImports bool
419418

420419
// ForceUseModules may be set to force modules to be enabled when
421420
// GO111MODULE=auto or to report an error when GO111MODULE=off.
@@ -1292,11 +1291,11 @@ func fixVersion(loaderstate *State, ctx context.Context, fixed *bool) modfile.Ve
12921291
//
12931292
// This function affects the default cfg.BuildMod when outside of a module,
12941293
// so it can only be called prior to Init.
1295-
func AllowMissingModuleImports(loaderstate *State) {
1296-
if loaderstate.initialized {
1294+
func (s *State) AllowMissingModuleImports() {
1295+
if s.initialized {
12971296
panic("AllowMissingModuleImports after Init")
12981297
}
1299-
allowMissingModuleImports = true
1298+
s.allowMissingModuleImports = true
13001299
}
13011300

13021301
// makeMainModules creates a MainModuleSet and associated variables according to
@@ -1553,7 +1552,7 @@ func setDefaultBuildMod(loaderstate *State) {
15531552
return
15541553
}
15551554
if loaderstate.modRoots == nil {
1556-
if allowMissingModuleImports {
1555+
if loaderstate.allowMissingModuleImports {
15571556
cfg.BuildMod = "mod"
15581557
} else {
15591558
cfg.BuildMod = "readonly"

src/cmd/go/internal/modload/load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ func loadFromRoots(loaderstate *State, ctx context.Context, params loaderParams)
11841184
continue
11851185
}
11861186

1187-
if !ld.ResolveMissingImports || (!HasModRoot(loaderstate) && !allowMissingModuleImports) {
1187+
if !ld.ResolveMissingImports || (!HasModRoot(loaderstate) && !loaderstate.allowMissingModuleImports) {
11881188
// We've loaded as much as we can without resolving missing imports.
11891189
break
11901190
}

src/cmd/go/internal/run/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
7979
// for -race and -msan.
8080
moduleLoaderState.ForceUseModules = true
8181
moduleLoaderState.RootMode = modload.NoRoot
82-
modload.AllowMissingModuleImports(moduleLoaderState)
82+
moduleLoaderState.AllowMissingModuleImports()
8383
modload.Init(moduleLoaderState)
8484
} else {
8585
modload.InitWorkfile(moduleLoaderState)

src/cmd/go/internal/work/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ func InstallPackages(loaderstate *modload.State, ctx context.Context, patterns [
863863
func installOutsideModule(loaderstate *modload.State, ctx context.Context, args []string) {
864864
loaderstate.ForceUseModules = true
865865
loaderstate.RootMode = modload.NoRoot
866-
modload.AllowMissingModuleImports(loaderstate)
866+
loaderstate.AllowMissingModuleImports()
867867
modload.Init(loaderstate)
868868
BuildInit(loaderstate)
869869

0 commit comments

Comments
 (0)