cmd/go: refactor usage of ForceUseModules

This commit refactors usage of the global variable `ForceUseModules`
to the global LoaderState field 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.forceUseModules State.ForceUseModules'
rf 'ex { ForceUseModules -> LoaderState.ForceUseModules }'
for dir in load modcmd modget run toolchain work workcmd ; do
  cd ../${dir}
  rf 'ex {
    import "cmd/go/internal/modload";
    modload.ForceUseModules -> modload.LoaderState.ForceUseModules
  }'
done
cd ../modload
rf 'add State.initialized \
// ForceUseModules may be set to force modules to be enabled when\
// GO111MODULE=auto or to report an error when GO111MODULE=off.'
rf 'rm ForceUseModules'

Change-Id: Ibdecfd273ff672516c9eb86279e5dfc6cdecb2ea
Reviewed-on: https://go-review.googlesource.com/c/go/+/698057
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Ian Alexander 2025-08-20 19:21:56 -04:00
parent c938051dd0
commit f86ddb54b5
16 changed files with 26 additions and 27 deletions

View file

@ -3348,7 +3348,7 @@ func GoFilesPackage(ctx context.Context, opts PackageOpts, gofiles []string) *Pa
// would cause it to be interpreted differently if it were the main module
// (replace, exclude).
func PackagesAndErrorsOutsideModule(ctx context.Context, opts PackageOpts, args []string) ([]*Package, error) {
if !modload.ForceUseModules {
if !modload.LoaderState.ForceUseModules {
panic("modload.ForceUseModules must be true")
}
if modload.RootMode != modload.NoRoot {

View file

@ -112,7 +112,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
modload.InitWorkfile()
// Check whether modules are enabled and whether we're in a module.
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.ExplicitWriteGoMod = true
haveExplicitArgs := len(args) > 0

View file

@ -57,7 +57,7 @@ func runGraph(ctx context.Context, cmd *base.Command, args []string) {
if len(args) > 0 {
base.Fatalf("go: 'go mod graph' accepts no arguments")
}
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.RootMode = modload.NeedRoot
goVersion := graphGo.String()

View file

@ -43,6 +43,6 @@ func runInit(ctx context.Context, cmd *base.Command, args []string) {
modPath = args[0]
}
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.CreateModFile(ctx, modPath) // does all the hard work
}

View file

@ -119,7 +119,7 @@ func runTidy(ctx context.Context, cmd *base.Command, args []string) {
// those packages. In order to make 'go test' reproducible for the packages
// that are in 'all' but outside of the main module, we must explicitly
// request that their test dependencies be included.
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.RootMode = modload.NeedRoot
goVersion := tidyGo.String()

View file

@ -77,7 +77,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string)
if len(args) != 0 {
base.Fatalf("go: 'go mod vendor' accepts no arguments")
}
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.RootMode = modload.NeedRoot
loadOpts := modload.PackageOpts{

View file

@ -50,7 +50,7 @@ func runVerify(ctx context.Context, cmd *base.Command, args []string) {
// NOTE(rsc): Could take a module pattern.
base.Fatalf("go: verify takes no arguments")
}
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.RootMode = modload.NeedRoot
// Only verify up to GOMAXPROCS zips at once.

View file

@ -64,7 +64,7 @@ func init() {
func runWhy(ctx context.Context, cmd *base.Command, args []string) {
modload.InitWorkfile()
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.RootMode = modload.NeedRoot
modload.ExplicitWriteGoMod = true // don't write go.mod in ListModules

View file

@ -298,7 +298,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
base.Fatalf("go: -insecure flag is no longer supported; use GOINSECURE instead")
}
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
// Do not allow any updating of go.mod until we've applied
// all the requested changes and checked that the result matches

View file

@ -41,10 +41,6 @@ var (
// RootMode determines whether a module root is needed.
RootMode Root
// ForceUseModules may be set to force modules to be enabled when
// GO111MODULE=auto or to report an error when GO111MODULE=off.
ForceUseModules bool
allowMissingModuleImports bool
// ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions
@ -96,7 +92,7 @@ func EnterWorkspace(ctx context.Context) (exit func(), err error) {
// Reset the state to a clean state.
oldstate := setState(State{})
ForceUseModules = true
LoaderState.ForceUseModules = true
// Load in workspace mode.
InitWorkfile()
@ -406,7 +402,7 @@ func Reset() {
func setState(s State) State {
oldState := State{
initialized: LoaderState.initialized,
forceUseModules: ForceUseModules,
ForceUseModules: LoaderState.ForceUseModules,
rootMode: RootMode,
modRoots: modRoots,
modulesEnabled: cfg.ModulesEnabled,
@ -414,7 +410,7 @@ func setState(s State) State {
requirements: requirements,
}
LoaderState.initialized = s.initialized
ForceUseModules = s.forceUseModules
LoaderState.ForceUseModules = s.ForceUseModules
RootMode = s.rootMode
modRoots = s.modRoots
cfg.ModulesEnabled = s.modulesEnabled
@ -430,7 +426,10 @@ func setState(s State) State {
type State struct {
initialized bool
forceUseModules bool
// ForceUseModules may be set to force modules to be enabled when
// GO111MODULE=auto or to report an error when GO111MODULE=off.
ForceUseModules bool
rootMode Root
modRoots []string
modulesEnabled bool
@ -465,11 +464,11 @@ func Init() {
default:
base.Fatalf("go: unknown environment setting GO111MODULE=%s", env)
case "auto":
mustUseModules = ForceUseModules
mustUseModules = LoaderState.ForceUseModules
case "on", "":
mustUseModules = true
case "off":
if ForceUseModules {
if LoaderState.ForceUseModules {
base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
}
mustUseModules = false

View file

@ -76,7 +76,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
// This must be done before modload.Init, but we need to call work.BuildInit
// before loading packages, since it affects package locations, e.g.,
// for -race and -msan.
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.RootMode = modload.NoRoot
modload.AllowMissingModuleImports()
modload.Init()

View file

@ -353,7 +353,7 @@ func Exec(gotoolchain string) {
// Set up modules without an explicit go.mod, to download distribution.
modload.Reset()
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.RootMode = modload.NoRoot
modload.Init()
@ -692,7 +692,7 @@ func maybeSwitchForGoInstallVersion(minVers string) {
// command lines if we add new flags in the future.
// Set up modules without an explicit go.mod, to download go.mod.
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.RootMode = modload.NoRoot
modload.Init()
defer modload.Reset()

View file

@ -859,7 +859,7 @@ func InstallPackages(ctx context.Context, patterns []string, pkgs []*load.Packag
//
// See golang.org/issue/40276 for details and rationale.
func installOutsideModule(ctx context.Context, args []string) {
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.RootMode = modload.NoRoot
modload.AllowMissingModuleImports()
modload.Init()

View file

@ -46,7 +46,7 @@ func init() {
func runInit(ctx context.Context, cmd *base.Command, args []string) {
modload.InitWorkfile()
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
gowork := modload.WorkFilePath()
if gowork == "" {

View file

@ -48,7 +48,7 @@ func init() {
}
func runSync(ctx context.Context, cmd *base.Command, args []string) {
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.InitWorkfile()
if modload.WorkFilePath() == "" {
base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using GOWORK environment variable)")

View file

@ -61,7 +61,7 @@ func init() {
}
func runUse(ctx context.Context, cmd *base.Command, args []string) {
modload.ForceUseModules = true
modload.LoaderState.ForceUseModules = true
modload.InitWorkfile()
gowork := modload.WorkFilePath()
if gowork == "" {