mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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>
This commit is contained in:
parent
f93186fb44
commit
0a95856b95
7 changed files with 47 additions and 45 deletions
|
|
@ -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.
|
// Allow looking up modules for import paths when outside of a module.
|
||||||
// 'go get' is expected to do this, unlike other commands.
|
// '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
|
// 'go get' no longer builds or installs packages, so there's nothing to do
|
||||||
// if there's no go.mod file.
|
// if there's no go.mod file.
|
||||||
|
|
|
||||||
|
|
@ -29,10 +29,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type ImportMissingError struct {
|
type ImportMissingError struct {
|
||||||
Path string
|
Path string
|
||||||
Module module.Version
|
Module module.Version
|
||||||
QueryErr error
|
QueryErr error
|
||||||
modContainingCWD module.Version
|
modContainingCWD module.Version
|
||||||
|
allowMissingModuleImports bool
|
||||||
|
|
||||||
// modRoot is dependent on the value of ImportingMainModule and should be
|
// modRoot is dependent on the value of ImportingMainModule and should be
|
||||||
// kept in sync.
|
// kept in sync.
|
||||||
|
|
@ -70,7 +71,7 @@ func (e *ImportMissingError) Error() string {
|
||||||
if e.QueryErr != nil && !errors.Is(e.QueryErr, ErrNoModRoot) {
|
if e.QueryErr != nil && !errors.Is(e.QueryErr, ErrNoModRoot) {
|
||||||
return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr)
|
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
|
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 {
|
if len(mods) == 0 {
|
||||||
return module.Version{}, "", "", nil, &ImportMissingError{
|
return module.Version{}, "", "", nil, &ImportMissingError{
|
||||||
Path: path,
|
Path: path,
|
||||||
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
|
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
|
||||||
|
allowMissingModuleImports: loaderstate.allowMissingModuleImports,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -494,10 +496,11 @@ func importFromModules(loaderstate *State, ctx context.Context, path string, rs
|
||||||
queryErr = NewNoMainModulesError(loaderstate)
|
queryErr = NewNoMainModulesError(loaderstate)
|
||||||
}
|
}
|
||||||
return module.Version{}, "", "", nil, &ImportMissingError{
|
return module.Version{}, "", "", nil, &ImportMissingError{
|
||||||
Path: path,
|
Path: path,
|
||||||
QueryErr: queryErr,
|
QueryErr: queryErr,
|
||||||
isStd: pathIsStd,
|
isStd: pathIsStd,
|
||||||
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
|
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 {
|
} else if ok {
|
||||||
if cfg.BuildMod == "readonly" {
|
if cfg.BuildMod == "readonly" {
|
||||||
return module.Version{}, &ImportMissingError{
|
return module.Version{}, &ImportMissingError{
|
||||||
Path: path,
|
Path: path,
|
||||||
replaced: m,
|
replaced: m,
|
||||||
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
|
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
|
||||||
|
allowMissingModuleImports: loaderstate.allowMissingModuleImports,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return m, nil
|
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.
|
// Instead of trying QueryPattern, report an ImportMissingError immediately.
|
||||||
return module.Version{}, &ImportMissingError{
|
return module.Version{}, &ImportMissingError{
|
||||||
Path: path,
|
Path: path,
|
||||||
isStd: true,
|
isStd: true,
|
||||||
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
|
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
|
// 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 module. If readonly mode was enabled explicitly, include that in
|
||||||
// the error message.
|
// 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)
|
queryErr = fmt.Errorf("import lookup disabled by -mod=%s\n\t(%s)", cfg.BuildMod, cfg.BuildModReason)
|
||||||
}
|
}
|
||||||
return module.Version{}, &ImportMissingError{
|
return module.Version{}, &ImportMissingError{
|
||||||
Path: path,
|
Path: path,
|
||||||
QueryErr: queryErr,
|
QueryErr: queryErr,
|
||||||
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
|
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
|
// Return "cannot find module providing package […]" instead of whatever
|
||||||
// low-level error QueryPattern produced.
|
// low-level error QueryPattern produced.
|
||||||
return module.Version{}, &ImportMissingError{
|
return module.Version{}, &ImportMissingError{
|
||||||
Path: path,
|
Path: path,
|
||||||
QueryErr: err,
|
QueryErr: err,
|
||||||
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
|
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
|
||||||
|
allowMissingModuleImports: loaderstate.allowMissingModuleImports,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return module.Version{}, err
|
return module.Version{}, err
|
||||||
|
|
@ -670,10 +677,11 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
|
||||||
return c.Mod, nil
|
return c.Mod, nil
|
||||||
}
|
}
|
||||||
return module.Version{}, &ImportMissingError{
|
return module.Version{}, &ImportMissingError{
|
||||||
Path: path,
|
Path: path,
|
||||||
Module: candidates[0].Mod,
|
Module: candidates[0].Mod,
|
||||||
newMissingVersion: candidate0MissingVersion,
|
newMissingVersion: candidate0MissingVersion,
|
||||||
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
|
modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
|
||||||
|
allowMissingModuleImports: loaderstate.allowMissingModuleImports,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,16 +58,11 @@ var importTests = []struct {
|
||||||
func TestQueryImport(t *testing.T) {
|
func TestQueryImport(t *testing.T) {
|
||||||
loaderstate := NewState()
|
loaderstate := NewState()
|
||||||
loaderstate.RootMode = NoRoot
|
loaderstate.RootMode = NoRoot
|
||||||
|
loaderstate.AllowMissingModuleImports()
|
||||||
|
|
||||||
testenv.MustHaveExternalNetwork(t)
|
testenv.MustHaveExternalNetwork(t)
|
||||||
testenv.MustHaveExecPath(t, "git")
|
testenv.MustHaveExecPath(t, "git")
|
||||||
|
|
||||||
oldAllowMissingModuleImports := allowMissingModuleImports
|
|
||||||
defer func() {
|
|
||||||
allowMissingModuleImports = oldAllowMissingModuleImports
|
|
||||||
}()
|
|
||||||
allowMissingModuleImports = true
|
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
rs := LoadModFile(loaderstate, ctx)
|
rs := LoadModFile(loaderstate, ctx)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,8 +38,6 @@ import (
|
||||||
//
|
//
|
||||||
// TODO(#40775): See if these can be plumbed as explicit parameters.
|
// TODO(#40775): See if these can be plumbed as explicit parameters.
|
||||||
var (
|
var (
|
||||||
allowMissingModuleImports bool
|
|
||||||
|
|
||||||
// ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions
|
// ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions
|
||||||
// from updating go.mod and go.sum or reporting errors when updates are
|
// 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
|
// 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 {
|
type State struct {
|
||||||
initialized bool
|
initialized bool
|
||||||
|
allowMissingModuleImports bool
|
||||||
|
|
||||||
// ForceUseModules may be set to force modules to be enabled when
|
// ForceUseModules may be set to force modules to be enabled when
|
||||||
// GO111MODULE=auto or to report an error when GO111MODULE=off.
|
// 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,
|
// This function affects the default cfg.BuildMod when outside of a module,
|
||||||
// so it can only be called prior to Init.
|
// so it can only be called prior to Init.
|
||||||
func AllowMissingModuleImports(loaderstate *State) {
|
func (s *State) AllowMissingModuleImports() {
|
||||||
if loaderstate.initialized {
|
if s.initialized {
|
||||||
panic("AllowMissingModuleImports after Init")
|
panic("AllowMissingModuleImports after Init")
|
||||||
}
|
}
|
||||||
allowMissingModuleImports = true
|
s.allowMissingModuleImports = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// makeMainModules creates a MainModuleSet and associated variables according to
|
// makeMainModules creates a MainModuleSet and associated variables according to
|
||||||
|
|
@ -1553,7 +1552,7 @@ func setDefaultBuildMod(loaderstate *State) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if loaderstate.modRoots == nil {
|
if loaderstate.modRoots == nil {
|
||||||
if allowMissingModuleImports {
|
if loaderstate.allowMissingModuleImports {
|
||||||
cfg.BuildMod = "mod"
|
cfg.BuildMod = "mod"
|
||||||
} else {
|
} else {
|
||||||
cfg.BuildMod = "readonly"
|
cfg.BuildMod = "readonly"
|
||||||
|
|
|
||||||
|
|
@ -1184,7 +1184,7 @@ func loadFromRoots(loaderstate *State, ctx context.Context, params loaderParams)
|
||||||
continue
|
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.
|
// We've loaded as much as we can without resolving missing imports.
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
// for -race and -msan.
|
// for -race and -msan.
|
||||||
moduleLoaderState.ForceUseModules = true
|
moduleLoaderState.ForceUseModules = true
|
||||||
moduleLoaderState.RootMode = modload.NoRoot
|
moduleLoaderState.RootMode = modload.NoRoot
|
||||||
modload.AllowMissingModuleImports(moduleLoaderState)
|
moduleLoaderState.AllowMissingModuleImports()
|
||||||
modload.Init(moduleLoaderState)
|
modload.Init(moduleLoaderState)
|
||||||
} else {
|
} else {
|
||||||
modload.InitWorkfile(moduleLoaderState)
|
modload.InitWorkfile(moduleLoaderState)
|
||||||
|
|
|
||||||
|
|
@ -863,7 +863,7 @@ func InstallPackages(loaderstate *modload.State, ctx context.Context, patterns [
|
||||||
func installOutsideModule(loaderstate *modload.State, ctx context.Context, args []string) {
|
func installOutsideModule(loaderstate *modload.State, ctx context.Context, args []string) {
|
||||||
loaderstate.ForceUseModules = true
|
loaderstate.ForceUseModules = true
|
||||||
loaderstate.RootMode = modload.NoRoot
|
loaderstate.RootMode = modload.NoRoot
|
||||||
modload.AllowMissingModuleImports(loaderstate)
|
loaderstate.AllowMissingModuleImports()
|
||||||
modload.Init(loaderstate)
|
modload.Init(loaderstate)
|
||||||
BuildInit(loaderstate)
|
BuildInit(loaderstate)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue