]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: eliminate additional global variable
authorIan Alexander <jitsu@google.com>
Mon, 27 Oct 2025 17:03:32 +0000 (13:03 -0400)
committerIan Alexander <jitsu@google.com>
Mon, 3 Nov 2025 20:20:15 +0000 (12:20 -0800)
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>
src/cmd/go/internal/modget/get.go
src/cmd/go/internal/modload/import.go
src/cmd/go/internal/modload/import_test.go
src/cmd/go/internal/modload/init.go
src/cmd/go/internal/modload/load.go
src/cmd/go/internal/run/run.go
src/cmd/go/internal/work/build.go

index 329fbaec040fc9d451b9defcf93ba8de6c8846be..d6c1c4b3744e87f8242719ee44f0982a7ffffa07 100644 (file)
@@ -308,7 +308,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
 
        // Allow looking up modules for import paths when outside of a module.
        // 'go get' is expected to do this, unlike other commands.
-       modload.AllowMissingModuleImports(moduleLoaderState)
+       moduleLoaderState.AllowMissingModuleImports()
 
        // 'go get' no longer builds or installs packages, so there's nothing to do
        // if there's no go.mod file.
index c6b56c35d4b1291f940167f25d25d00edfc8efe1..6dd48260d9f660bd7176f6240b789b2050aa3b21 100644 (file)
@@ -29,10 +29,11 @@ import (
 )
 
 type ImportMissingError struct {
-       Path             string
-       Module           module.Version
-       QueryErr         error
-       modContainingCWD module.Version
+       Path                      string
+       Module                    module.Version
+       QueryErr                  error
+       modContainingCWD          module.Version
+       allowMissingModuleImports bool
 
        // modRoot is dependent on the value of ImportingMainModule and should be
        // kept in sync.
@@ -70,7 +71,7 @@ func (e *ImportMissingError) Error() string {
                if e.QueryErr != nil && !errors.Is(e.QueryErr, ErrNoModRoot) {
                        return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr)
                }
-               if cfg.BuildMod == "mod" || (cfg.BuildMod == "readonly" && allowMissingModuleImports) {
+               if cfg.BuildMod == "mod" || (cfg.BuildMod == "readonly" && e.allowMissingModuleImports) {
                        return "cannot find module providing package " + e.Path
                }
 
@@ -373,8 +374,9 @@ func importFromModules(loaderstate *State, ctx context.Context, path string, rs
 
                if len(mods) == 0 {
                        return module.Version{}, "", "", nil, &ImportMissingError{
-                               Path:             path,
-                               modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+                               Path:                      path,
+                               modContainingCWD:          loaderstate.MainModules.ModContainingCWD(),
+                               allowMissingModuleImports: loaderstate.allowMissingModuleImports,
                        }
                }
 
@@ -494,10 +496,11 @@ func importFromModules(loaderstate *State, ctx context.Context, path string, rs
                                queryErr = NewNoMainModulesError(loaderstate)
                        }
                        return module.Version{}, "", "", nil, &ImportMissingError{
-                               Path:             path,
-                               QueryErr:         queryErr,
-                               isStd:            pathIsStd,
-                               modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+                               Path:                      path,
+                               QueryErr:                  queryErr,
+                               isStd:                     pathIsStd,
+                               modContainingCWD:          loaderstate.MainModules.ModContainingCWD(),
+                               allowMissingModuleImports: loaderstate.allowMissingModuleImports,
                        }
                }
 
@@ -571,9 +574,10 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
                } else if ok {
                        if cfg.BuildMod == "readonly" {
                                return module.Version{}, &ImportMissingError{
-                                       Path:             path,
-                                       replaced:         m,
-                                       modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+                                       Path:                      path,
+                                       replaced:                  m,
+                                       modContainingCWD:          loaderstate.MainModules.ModContainingCWD(),
+                                       allowMissingModuleImports: loaderstate.allowMissingModuleImports,
                                }
                        }
                        return m, nil
@@ -601,13 +605,14 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
                //
                // Instead of trying QueryPattern, report an ImportMissingError immediately.
                return module.Version{}, &ImportMissingError{
-                       Path:             path,
-                       isStd:            true,
-                       modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+                       Path:                      path,
+                       isStd:                     true,
+                       modContainingCWD:          loaderstate.MainModules.ModContainingCWD(),
+                       allowMissingModuleImports: loaderstate.allowMissingModuleImports,
                }
        }
 
-       if (cfg.BuildMod == "readonly" || cfg.BuildMod == "vendor") && !allowMissingModuleImports {
+       if (cfg.BuildMod == "readonly" || cfg.BuildMod == "vendor") && !loaderstate.allowMissingModuleImports {
                // In readonly mode, we can't write go.mod, so we shouldn't try to look up
                // the module. If readonly mode was enabled explicitly, include that in
                // the error message.
@@ -620,9 +625,10 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
                        queryErr = fmt.Errorf("import lookup disabled by -mod=%s\n\t(%s)", cfg.BuildMod, cfg.BuildModReason)
                }
                return module.Version{}, &ImportMissingError{
-                       Path:             path,
-                       QueryErr:         queryErr,
-                       modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+                       Path:                      path,
+                       QueryErr:                  queryErr,
+                       modContainingCWD:          loaderstate.MainModules.ModContainingCWD(),
+                       allowMissingModuleImports: loaderstate.allowMissingModuleImports,
                }
        }
 
@@ -642,9 +648,10 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
                        // Return "cannot find module providing package […]" instead of whatever
                        // low-level error QueryPattern produced.
                        return module.Version{}, &ImportMissingError{
-                               Path:             path,
-                               QueryErr:         err,
-                               modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+                               Path:                      path,
+                               QueryErr:                  err,
+                               modContainingCWD:          loaderstate.MainModules.ModContainingCWD(),
+                               allowMissingModuleImports: loaderstate.allowMissingModuleImports,
                        }
                } else {
                        return module.Version{}, err
@@ -670,10 +677,11 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
                return c.Mod, nil
        }
        return module.Version{}, &ImportMissingError{
-               Path:              path,
-               Module:            candidates[0].Mod,
-               newMissingVersion: candidate0MissingVersion,
-               modContainingCWD:  loaderstate.MainModules.ModContainingCWD(),
+               Path:                      path,
+               Module:                    candidates[0].Mod,
+               newMissingVersion:         candidate0MissingVersion,
+               modContainingCWD:          loaderstate.MainModules.ModContainingCWD(),
+               allowMissingModuleImports: loaderstate.allowMissingModuleImports,
        }
 }
 
index 0716675a91d2bd6f2f78c9c7c422ae118d094424..820fb87b5928f2992e5c603bc7a27428caf73927 100644 (file)
@@ -58,16 +58,11 @@ var importTests = []struct {
 func TestQueryImport(t *testing.T) {
        loaderstate := NewState()
        loaderstate.RootMode = NoRoot
+       loaderstate.AllowMissingModuleImports()
 
        testenv.MustHaveExternalNetwork(t)
        testenv.MustHaveExecPath(t, "git")
 
-       oldAllowMissingModuleImports := allowMissingModuleImports
-       defer func() {
-               allowMissingModuleImports = oldAllowMissingModuleImports
-       }()
-       allowMissingModuleImports = true
-
        ctx := context.Background()
        rs := LoadModFile(loaderstate, ctx)
 
index 9abfac971573b3a221c883661bd43b33dd3b03f1..c2d0a64c8b270c81369983f108ed2c7775f49171 100644 (file)
@@ -38,8 +38,6 @@ import (
 //
 // TODO(#40775): See if these can be plumbed as explicit parameters.
 var (
-       allowMissingModuleImports bool
-
        // ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions
        // from updating go.mod and go.sum or reporting errors when updates are
        // 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 {
 }
 
 type State struct {
-       initialized bool
+       initialized               bool
+       allowMissingModuleImports bool
 
        // ForceUseModules may be set to force modules to be enabled when
        // 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
 //
 // This function affects the default cfg.BuildMod when outside of a module,
 // so it can only be called prior to Init.
-func AllowMissingModuleImports(loaderstate *State) {
-       if loaderstate.initialized {
+func (s *State) AllowMissingModuleImports() {
+       if s.initialized {
                panic("AllowMissingModuleImports after Init")
        }
-       allowMissingModuleImports = true
+       s.allowMissingModuleImports = true
 }
 
 // makeMainModules creates a MainModuleSet and associated variables according to
@@ -1553,7 +1552,7 @@ func setDefaultBuildMod(loaderstate *State) {
                return
        }
        if loaderstate.modRoots == nil {
-               if allowMissingModuleImports {
+               if loaderstate.allowMissingModuleImports {
                        cfg.BuildMod = "mod"
                } else {
                        cfg.BuildMod = "readonly"
index 065d3a78163a21da559d66395d577781dd86d7c5..4da0c9ed4b458d2736cced4d660078c1f098abf5 100644 (file)
@@ -1184,7 +1184,7 @@ func loadFromRoots(loaderstate *State, ctx context.Context, params loaderParams)
                        continue
                }
 
-               if !ld.ResolveMissingImports || (!HasModRoot(loaderstate) && !allowMissingModuleImports) {
+               if !ld.ResolveMissingImports || (!HasModRoot(loaderstate) && !loaderstate.allowMissingModuleImports) {
                        // We've loaded as much as we can without resolving missing imports.
                        break
                }
index f821b37f292bfd688d19cd15c3614a4fa66ef58e..6c4f1a9220122a98f68e2dfb75300b1493fc5622 100644 (file)
@@ -79,7 +79,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
                // for -race and -msan.
                moduleLoaderState.ForceUseModules = true
                moduleLoaderState.RootMode = modload.NoRoot
-               modload.AllowMissingModuleImports(moduleLoaderState)
+               moduleLoaderState.AllowMissingModuleImports()
                modload.Init(moduleLoaderState)
        } else {
                modload.InitWorkfile(moduleLoaderState)
index 7ca95cbe3f9286642f502434aba71812c0c4e411..9e0bb41d7235de7dae47dd540580e43ed174eb7b 100644 (file)
@@ -863,7 +863,7 @@ func InstallPackages(loaderstate *modload.State, ctx context.Context, patterns [
 func installOutsideModule(loaderstate *modload.State, ctx context.Context, args []string) {
        loaderstate.ForceUseModules = true
        loaderstate.RootMode = modload.NoRoot
-       modload.AllowMissingModuleImports(loaderstate)
+       loaderstate.AllowMissingModuleImports()
        modload.Init(loaderstate)
        BuildInit(loaderstate)