mirror of
https://github.com/golang/go.git
synced 2025-10-19 19:13:18 +00:00
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:
parent
c938051dd0
commit
f86ddb54b5
16 changed files with 26 additions and 27 deletions
|
@ -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
|
// would cause it to be interpreted differently if it were the main module
|
||||||
// (replace, exclude).
|
// (replace, exclude).
|
||||||
func PackagesAndErrorsOutsideModule(ctx context.Context, opts PackageOpts, args []string) ([]*Package, error) {
|
func PackagesAndErrorsOutsideModule(ctx context.Context, opts PackageOpts, args []string) ([]*Package, error) {
|
||||||
if !modload.ForceUseModules {
|
if !modload.LoaderState.ForceUseModules {
|
||||||
panic("modload.ForceUseModules must be true")
|
panic("modload.ForceUseModules must be true")
|
||||||
}
|
}
|
||||||
if modload.RootMode != modload.NoRoot {
|
if modload.RootMode != modload.NoRoot {
|
||||||
|
|
|
@ -112,7 +112,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
modload.InitWorkfile()
|
modload.InitWorkfile()
|
||||||
|
|
||||||
// Check whether modules are enabled and whether we're in a module.
|
// Check whether modules are enabled and whether we're in a module.
|
||||||
modload.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.ExplicitWriteGoMod = true
|
modload.ExplicitWriteGoMod = true
|
||||||
haveExplicitArgs := len(args) > 0
|
haveExplicitArgs := len(args) > 0
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,7 @@ func runGraph(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
base.Fatalf("go: 'go mod graph' accepts no arguments")
|
base.Fatalf("go: 'go mod graph' accepts no arguments")
|
||||||
}
|
}
|
||||||
modload.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NeedRoot
|
modload.RootMode = modload.NeedRoot
|
||||||
|
|
||||||
goVersion := graphGo.String()
|
goVersion := graphGo.String()
|
||||||
|
|
|
@ -43,6 +43,6 @@ func runInit(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
modPath = args[0]
|
modPath = args[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
modload.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.CreateModFile(ctx, modPath) // does all the hard work
|
modload.CreateModFile(ctx, modPath) // does all the hard work
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
// 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
|
// 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.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NeedRoot
|
modload.RootMode = modload.NeedRoot
|
||||||
|
|
||||||
goVersion := tidyGo.String()
|
goVersion := tidyGo.String()
|
||||||
|
|
|
@ -77,7 +77,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string)
|
||||||
if len(args) != 0 {
|
if len(args) != 0 {
|
||||||
base.Fatalf("go: 'go mod vendor' accepts no arguments")
|
base.Fatalf("go: 'go mod vendor' accepts no arguments")
|
||||||
}
|
}
|
||||||
modload.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NeedRoot
|
modload.RootMode = modload.NeedRoot
|
||||||
|
|
||||||
loadOpts := modload.PackageOpts{
|
loadOpts := modload.PackageOpts{
|
||||||
|
|
|
@ -50,7 +50,7 @@ func runVerify(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
// NOTE(rsc): Could take a module pattern.
|
// NOTE(rsc): Could take a module pattern.
|
||||||
base.Fatalf("go: verify takes no arguments")
|
base.Fatalf("go: verify takes no arguments")
|
||||||
}
|
}
|
||||||
modload.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NeedRoot
|
modload.RootMode = modload.NeedRoot
|
||||||
|
|
||||||
// Only verify up to GOMAXPROCS zips at once.
|
// Only verify up to GOMAXPROCS zips at once.
|
||||||
|
|
|
@ -64,7 +64,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.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NeedRoot
|
modload.RootMode = modload.NeedRoot
|
||||||
modload.ExplicitWriteGoMod = true // don't write go.mod in ListModules
|
modload.ExplicitWriteGoMod = true // don't write go.mod in ListModules
|
||||||
|
|
||||||
|
|
|
@ -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")
|
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
|
// Do not allow any updating of go.mod until we've applied
|
||||||
// all the requested changes and checked that the result matches
|
// all the requested changes and checked that the result matches
|
||||||
|
|
|
@ -41,10 +41,6 @@ var (
|
||||||
// RootMode determines whether a module root is needed.
|
// RootMode determines whether a module root is needed.
|
||||||
RootMode Root
|
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
|
allowMissingModuleImports bool
|
||||||
|
|
||||||
// ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions
|
// 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.
|
// Reset the state to a clean state.
|
||||||
oldstate := setState(State{})
|
oldstate := setState(State{})
|
||||||
ForceUseModules = true
|
LoaderState.ForceUseModules = true
|
||||||
|
|
||||||
// Load in workspace mode.
|
// Load in workspace mode.
|
||||||
InitWorkfile()
|
InitWorkfile()
|
||||||
|
@ -406,7 +402,7 @@ func Reset() {
|
||||||
func setState(s State) State {
|
func setState(s State) State {
|
||||||
oldState := State{
|
oldState := State{
|
||||||
initialized: LoaderState.initialized,
|
initialized: LoaderState.initialized,
|
||||||
forceUseModules: ForceUseModules,
|
ForceUseModules: LoaderState.ForceUseModules,
|
||||||
rootMode: RootMode,
|
rootMode: RootMode,
|
||||||
modRoots: modRoots,
|
modRoots: modRoots,
|
||||||
modulesEnabled: cfg.ModulesEnabled,
|
modulesEnabled: cfg.ModulesEnabled,
|
||||||
|
@ -414,7 +410,7 @@ func setState(s State) State {
|
||||||
requirements: requirements,
|
requirements: requirements,
|
||||||
}
|
}
|
||||||
LoaderState.initialized = s.initialized
|
LoaderState.initialized = s.initialized
|
||||||
ForceUseModules = s.forceUseModules
|
LoaderState.ForceUseModules = s.ForceUseModules
|
||||||
RootMode = s.rootMode
|
RootMode = s.rootMode
|
||||||
modRoots = s.modRoots
|
modRoots = s.modRoots
|
||||||
cfg.ModulesEnabled = s.modulesEnabled
|
cfg.ModulesEnabled = s.modulesEnabled
|
||||||
|
@ -429,8 +425,11 @@ func setState(s State) State {
|
||||||
}
|
}
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
initialized bool
|
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
|
rootMode Root
|
||||||
modRoots []string
|
modRoots []string
|
||||||
modulesEnabled bool
|
modulesEnabled bool
|
||||||
|
@ -465,11 +464,11 @@ func Init() {
|
||||||
default:
|
default:
|
||||||
base.Fatalf("go: unknown environment setting GO111MODULE=%s", env)
|
base.Fatalf("go: unknown environment setting GO111MODULE=%s", env)
|
||||||
case "auto":
|
case "auto":
|
||||||
mustUseModules = ForceUseModules
|
mustUseModules = LoaderState.ForceUseModules
|
||||||
case "on", "":
|
case "on", "":
|
||||||
mustUseModules = true
|
mustUseModules = true
|
||||||
case "off":
|
case "off":
|
||||||
if ForceUseModules {
|
if LoaderState.ForceUseModules {
|
||||||
base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
|
base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
|
||||||
}
|
}
|
||||||
mustUseModules = false
|
mustUseModules = false
|
||||||
|
|
|
@ -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
|
// This must be done before modload.Init, but we need to call work.BuildInit
|
||||||
// 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.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NoRoot
|
modload.RootMode = modload.NoRoot
|
||||||
modload.AllowMissingModuleImports()
|
modload.AllowMissingModuleImports()
|
||||||
modload.Init()
|
modload.Init()
|
||||||
|
|
|
@ -353,7 +353,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.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NoRoot
|
modload.RootMode = modload.NoRoot
|
||||||
modload.Init()
|
modload.Init()
|
||||||
|
|
||||||
|
@ -692,7 +692,7 @@ func maybeSwitchForGoInstallVersion(minVers string) {
|
||||||
// command lines if we add new flags in the future.
|
// command lines if we add new flags in the future.
|
||||||
|
|
||||||
// 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.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NoRoot
|
modload.RootMode = modload.NoRoot
|
||||||
modload.Init()
|
modload.Init()
|
||||||
defer modload.Reset()
|
defer modload.Reset()
|
||||||
|
|
|
@ -859,7 +859,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.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.RootMode = modload.NoRoot
|
modload.RootMode = modload.NoRoot
|
||||||
modload.AllowMissingModuleImports()
|
modload.AllowMissingModuleImports()
|
||||||
modload.Init()
|
modload.Init()
|
||||||
|
|
|
@ -46,7 +46,7 @@ func init() {
|
||||||
func runInit(ctx context.Context, cmd *base.Command, args []string) {
|
func runInit(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
modload.InitWorkfile()
|
modload.InitWorkfile()
|
||||||
|
|
||||||
modload.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
|
|
||||||
gowork := modload.WorkFilePath()
|
gowork := modload.WorkFilePath()
|
||||||
if gowork == "" {
|
if gowork == "" {
|
||||||
|
|
|
@ -48,7 +48,7 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func runSync(ctx context.Context, cmd *base.Command, args []string) {
|
func runSync(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
modload.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.InitWorkfile()
|
modload.InitWorkfile()
|
||||||
if modload.WorkFilePath() == "" {
|
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)")
|
base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using GOWORK environment variable)")
|
||||||
|
|
|
@ -61,7 +61,7 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func runUse(ctx context.Context, cmd *base.Command, args []string) {
|
func runUse(ctx context.Context, cmd *base.Command, args []string) {
|
||||||
modload.ForceUseModules = true
|
modload.LoaderState.ForceUseModules = true
|
||||||
modload.InitWorkfile()
|
modload.InitWorkfile()
|
||||||
gowork := modload.WorkFilePath()
|
gowork := modload.WorkFilePath()
|
||||||
if gowork == "" {
|
if gowork == "" {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue