mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/go: refactor usage of RootMode
This commit refactors usage of the global variable `RootMode` to the
global LoaderState variable of the same name.
This commit is part of the overall effort to eliminate global
modloader state.
[git-generate]
cd src/cmd/go/internal/modload
rf 'mv State.rootMode State.RootMode'
for dir in load modcmd run tool toolchain work ; do
cd ../${dir}
rf 'ex {
import "cmd/go/internal/modload";
modload.RootMode -> modload.LoaderState.RootMode
}'
done
cd ../modload
rf 'ex { RootMode -> LoaderState.RootMode }'
rf 'add State.ForceUseModules \
// RootMode determines whether a module root is needed.'
rf 'rm RootMode'
Change-Id: Ib5e513ee570dfc3b01cc974fe32944e5e391fd82
Reviewed-on: https://go-review.googlesource.com/c/go/+/698058
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
parent
f86ddb54b5
commit
2e52060084
13 changed files with 31 additions and 32 deletions
|
|
@ -50,7 +50,7 @@ func defaultGODEBUG(p *Package, directives, testDirectives, xtestDirectives []bu
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
goVersion := modload.MainModules.GoVersion()
|
goVersion := modload.MainModules.GoVersion()
|
||||||
if modload.RootMode == modload.NoRoot && p.Module != nil {
|
if modload.LoaderState.RootMode == modload.NoRoot && p.Module != nil {
|
||||||
// This is go install pkg@version or go run pkg@version.
|
// This is go install pkg@version or go run pkg@version.
|
||||||
// Use the Go version from the package.
|
// Use the Go version from the package.
|
||||||
// If there isn't one, then assume Go 1.20,
|
// If there isn't one, then assume Go 1.20,
|
||||||
|
|
|
||||||
|
|
@ -3351,7 +3351,7 @@ func PackagesAndErrorsOutsideModule(ctx context.Context, opts PackageOpts, args
|
||||||
if !modload.LoaderState.ForceUseModules {
|
if !modload.LoaderState.ForceUseModules {
|
||||||
panic("modload.ForceUseModules must be true")
|
panic("modload.ForceUseModules must be true")
|
||||||
}
|
}
|
||||||
if modload.RootMode != modload.NoRoot {
|
if modload.LoaderState.RootMode != modload.NoRoot {
|
||||||
panic("modload.RootMode must be NoRoot")
|
panic("modload.RootMode must be NoRoot")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ func runGraph(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
base.Fatalf("go: 'go mod graph' accepts no arguments")
|
base.Fatalf("go: 'go mod graph' accepts no arguments")
|
||||||
}
|
}
|
||||||
modload.LoaderState.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NeedRoot
|
modload.LoaderState.RootMode = modload.NeedRoot
|
||||||
|
|
||||||
goVersion := graphGo.String()
|
goVersion := graphGo.String()
|
||||||
if goVersion != "" && gover.Compare(gover.Local(), goVersion) < 0 {
|
if goVersion != "" && gover.Compare(gover.Local(), goVersion) < 0 {
|
||||||
|
|
|
||||||
|
|
@ -120,7 +120,7 @@ func runTidy(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
// that are in 'all' but outside of the main module, we must explicitly
|
// that are in 'all' but outside of the main module, we must explicitly
|
||||||
// request that their test dependencies be included.
|
// request that their test dependencies be included.
|
||||||
modload.LoaderState.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NeedRoot
|
modload.LoaderState.RootMode = modload.NeedRoot
|
||||||
|
|
||||||
goVersion := tidyGo.String()
|
goVersion := tidyGo.String()
|
||||||
if goVersion != "" && gover.Compare(gover.Local(), goVersion) < 0 {
|
if goVersion != "" && gover.Compare(gover.Local(), goVersion) < 0 {
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string)
|
||||||
base.Fatalf("go: 'go mod vendor' accepts no arguments")
|
base.Fatalf("go: 'go mod vendor' accepts no arguments")
|
||||||
}
|
}
|
||||||
modload.LoaderState.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NeedRoot
|
modload.LoaderState.RootMode = modload.NeedRoot
|
||||||
|
|
||||||
loadOpts := modload.PackageOpts{
|
loadOpts := modload.PackageOpts{
|
||||||
Tags: imports.AnyTags(),
|
Tags: imports.AnyTags(),
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ func runVerify(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
base.Fatalf("go: verify takes no arguments")
|
base.Fatalf("go: verify takes no arguments")
|
||||||
}
|
}
|
||||||
modload.LoaderState.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NeedRoot
|
modload.LoaderState.RootMode = modload.NeedRoot
|
||||||
|
|
||||||
// Only verify up to GOMAXPROCS zips at once.
|
// Only verify up to GOMAXPROCS zips at once.
|
||||||
type token struct{}
|
type token struct{}
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ func init() {
|
||||||
func runWhy(ctx context.Context, cmd *base.Command, args []string) {
|
func runWhy(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
modload.InitWorkfile()
|
modload.InitWorkfile()
|
||||||
modload.LoaderState.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NeedRoot
|
modload.LoaderState.RootMode = modload.NeedRoot
|
||||||
modload.ExplicitWriteGoMod = true // don't write go.mod in ListModules
|
modload.ExplicitWriteGoMod = true // don't write go.mod in ListModules
|
||||||
|
|
||||||
loadOpts := modload.PackageOpts{
|
loadOpts := modload.PackageOpts{
|
||||||
|
|
|
||||||
|
|
@ -60,13 +60,13 @@ func TestQueryImport(t *testing.T) {
|
||||||
testenv.MustHaveExecPath(t, "git")
|
testenv.MustHaveExecPath(t, "git")
|
||||||
|
|
||||||
oldAllowMissingModuleImports := allowMissingModuleImports
|
oldAllowMissingModuleImports := allowMissingModuleImports
|
||||||
oldRootMode := RootMode
|
oldRootMode := LoaderState.RootMode
|
||||||
defer func() {
|
defer func() {
|
||||||
allowMissingModuleImports = oldAllowMissingModuleImports
|
allowMissingModuleImports = oldAllowMissingModuleImports
|
||||||
RootMode = oldRootMode
|
LoaderState.RootMode = oldRootMode
|
||||||
}()
|
}()
|
||||||
allowMissingModuleImports = true
|
allowMissingModuleImports = true
|
||||||
RootMode = NoRoot
|
LoaderState.RootMode = NoRoot
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
rs := LoadModFile(ctx)
|
rs := LoadModFile(ctx)
|
||||||
|
|
|
||||||
|
|
@ -38,9 +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 (
|
||||||
// RootMode determines whether a module root is needed.
|
|
||||||
RootMode Root
|
|
||||||
|
|
||||||
allowMissingModuleImports bool
|
allowMissingModuleImports bool
|
||||||
|
|
||||||
// ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions
|
// ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions
|
||||||
|
|
@ -370,7 +367,7 @@ func InitWorkfile() {
|
||||||
// It is exported mainly for Go toolchain switching, which must process
|
// It is exported mainly for Go toolchain switching, which must process
|
||||||
// the go.work very early at startup.
|
// the go.work very early at startup.
|
||||||
func FindGoWork(wd string) string {
|
func FindGoWork(wd string) string {
|
||||||
if RootMode == NoRoot {
|
if LoaderState.RootMode == NoRoot {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -403,7 +400,7 @@ func setState(s State) State {
|
||||||
oldState := State{
|
oldState := State{
|
||||||
initialized: LoaderState.initialized,
|
initialized: LoaderState.initialized,
|
||||||
ForceUseModules: LoaderState.ForceUseModules,
|
ForceUseModules: LoaderState.ForceUseModules,
|
||||||
rootMode: RootMode,
|
RootMode: LoaderState.RootMode,
|
||||||
modRoots: modRoots,
|
modRoots: modRoots,
|
||||||
modulesEnabled: cfg.ModulesEnabled,
|
modulesEnabled: cfg.ModulesEnabled,
|
||||||
mainModules: MainModules,
|
mainModules: MainModules,
|
||||||
|
|
@ -411,7 +408,7 @@ func setState(s State) State {
|
||||||
}
|
}
|
||||||
LoaderState.initialized = s.initialized
|
LoaderState.initialized = s.initialized
|
||||||
LoaderState.ForceUseModules = s.ForceUseModules
|
LoaderState.ForceUseModules = s.ForceUseModules
|
||||||
RootMode = s.rootMode
|
LoaderState.RootMode = s.RootMode
|
||||||
modRoots = s.modRoots
|
modRoots = s.modRoots
|
||||||
cfg.ModulesEnabled = s.modulesEnabled
|
cfg.ModulesEnabled = s.modulesEnabled
|
||||||
MainModules = s.mainModules
|
MainModules = s.mainModules
|
||||||
|
|
@ -430,13 +427,15 @@ type State struct {
|
||||||
// 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.
|
||||||
ForceUseModules bool
|
ForceUseModules bool
|
||||||
rootMode Root
|
|
||||||
modRoots []string
|
// RootMode determines whether a module root is needed.
|
||||||
modulesEnabled bool
|
RootMode Root
|
||||||
mainModules *MainModuleSet
|
modRoots []string
|
||||||
requirements *Requirements
|
modulesEnabled bool
|
||||||
workFilePath string
|
mainModules *MainModuleSet
|
||||||
modfetchState modfetch.State
|
requirements *Requirements
|
||||||
|
workFilePath string
|
||||||
|
modfetchState modfetch.State
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewState() *State { return &State{} }
|
func NewState() *State { return &State{} }
|
||||||
|
|
@ -495,7 +494,7 @@ func Init() {
|
||||||
if modRoots != nil {
|
if modRoots != nil {
|
||||||
// modRoot set before Init was called ("go mod init" does this).
|
// modRoot set before Init was called ("go mod init" does this).
|
||||||
// No need to search for go.mod.
|
// No need to search for go.mod.
|
||||||
} else if RootMode == NoRoot {
|
} else if LoaderState.RootMode == NoRoot {
|
||||||
if cfg.ModFile != "" && !base.InGOFLAGS("-modfile") {
|
if cfg.ModFile != "" && !base.InGOFLAGS("-modfile") {
|
||||||
base.Fatalf("go: -modfile cannot be used with commands that ignore the current module")
|
base.Fatalf("go: -modfile cannot be used with commands that ignore the current module")
|
||||||
}
|
}
|
||||||
|
|
@ -510,7 +509,7 @@ func Init() {
|
||||||
if cfg.ModFile != "" {
|
if cfg.ModFile != "" {
|
||||||
base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.")
|
base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.")
|
||||||
}
|
}
|
||||||
if RootMode == NeedRoot {
|
if LoaderState.RootMode == NeedRoot {
|
||||||
base.Fatal(ErrNoModRoot)
|
base.Fatal(ErrNoModRoot)
|
||||||
}
|
}
|
||||||
if !mustUseModules {
|
if !mustUseModules {
|
||||||
|
|
@ -525,7 +524,7 @@ func Init() {
|
||||||
// It's a bit of a peculiar thing to disallow but quite mysterious
|
// It's a bit of a peculiar thing to disallow but quite mysterious
|
||||||
// when it happens. See golang.org/issue/26708.
|
// when it happens. See golang.org/issue/26708.
|
||||||
fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in system temp root %v\n", os.TempDir())
|
fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in system temp root %v\n", os.TempDir())
|
||||||
if RootMode == NeedRoot {
|
if LoaderState.RootMode == NeedRoot {
|
||||||
base.Fatal(ErrNoModRoot)
|
base.Fatal(ErrNoModRoot)
|
||||||
}
|
}
|
||||||
if !mustUseModules {
|
if !mustUseModules {
|
||||||
|
|
@ -547,7 +546,7 @@ func Init() {
|
||||||
gopath = list[0]
|
gopath = list[0]
|
||||||
if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil {
|
if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil {
|
||||||
fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in $GOPATH %v\n", gopath)
|
fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in $GOPATH %v\n", gopath)
|
||||||
if RootMode == NeedRoot {
|
if LoaderState.RootMode == NeedRoot {
|
||||||
base.Fatal(ErrNoModRoot)
|
base.Fatal(ErrNoModRoot)
|
||||||
}
|
}
|
||||||
if !mustUseModules {
|
if !mustUseModules {
|
||||||
|
|
|
||||||
|
|
@ -77,7 +77,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
// before loading packages, since it affects package locations, e.g.,
|
// before loading packages, since it affects package locations, e.g.,
|
||||||
// for -race and -msan.
|
// for -race and -msan.
|
||||||
modload.LoaderState.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NoRoot
|
modload.LoaderState.RootMode = modload.NoRoot
|
||||||
modload.AllowMissingModuleImports()
|
modload.AllowMissingModuleImports()
|
||||||
modload.Init()
|
modload.Init()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -308,7 +308,7 @@ func buildAndRunBuiltinTool(ctx context.Context, toolName, tool string, args []s
|
||||||
// Ignore go.mod and go.work: we don't need them, and we want to be able
|
// Ignore go.mod and go.work: we don't need them, and we want to be able
|
||||||
// to run the tool even if there's an issue with the module or workspace the
|
// to run the tool even if there's an issue with the module or workspace the
|
||||||
// user happens to be in.
|
// user happens to be in.
|
||||||
modload.RootMode = modload.NoRoot
|
modload.LoaderState.RootMode = modload.NoRoot
|
||||||
|
|
||||||
runFunc := func(b *work.Builder, ctx context.Context, a *work.Action) error {
|
runFunc := func(b *work.Builder, ctx context.Context, a *work.Action) error {
|
||||||
cmdline := str.StringList(builtTool(a), a.Args)
|
cmdline := str.StringList(builtTool(a), a.Args)
|
||||||
|
|
|
||||||
|
|
@ -354,7 +354,7 @@ func Exec(gotoolchain string) {
|
||||||
// Set up modules without an explicit go.mod, to download distribution.
|
// Set up modules without an explicit go.mod, to download distribution.
|
||||||
modload.Reset()
|
modload.Reset()
|
||||||
modload.LoaderState.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NoRoot
|
modload.LoaderState.RootMode = modload.NoRoot
|
||||||
modload.Init()
|
modload.Init()
|
||||||
|
|
||||||
// Download and unpack toolchain module into module cache.
|
// Download and unpack toolchain module into module cache.
|
||||||
|
|
@ -693,7 +693,7 @@ func maybeSwitchForGoInstallVersion(minVers string) {
|
||||||
|
|
||||||
// Set up modules without an explicit go.mod, to download go.mod.
|
// Set up modules without an explicit go.mod, to download go.mod.
|
||||||
modload.LoaderState.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NoRoot
|
modload.LoaderState.RootMode = modload.NoRoot
|
||||||
modload.Init()
|
modload.Init()
|
||||||
defer modload.Reset()
|
defer modload.Reset()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -860,7 +860,7 @@ func InstallPackages(ctx context.Context, patterns []string, pkgs []*load.Packag
|
||||||
// See golang.org/issue/40276 for details and rationale.
|
// See golang.org/issue/40276 for details and rationale.
|
||||||
func installOutsideModule(ctx context.Context, args []string) {
|
func installOutsideModule(ctx context.Context, args []string) {
|
||||||
modload.LoaderState.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NoRoot
|
modload.LoaderState.RootMode = modload.NoRoot
|
||||||
modload.AllowMissingModuleImports()
|
modload.AllowMissingModuleImports()
|
||||||
modload.Init()
|
modload.Init()
|
||||||
BuildInit()
|
BuildInit()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue