cmd/go: inject State parameter into bug.runBug

This command modifies the call tree starting at `bug.runBug` to inject
a `State` parameter to every function that is currently using the
global `modload.LoaderState` variable.  By explicilty passing a
`State` parameter, we can begin to eliminate the usage of the global
`modload.LoaderState`.

This commit is part of the overall effort to eliminate global
modloader state.

[git-generate]
cd src/cmd/go/internal/bug
rf 'inject modload.LoaderState runBug'
cd ..
./rf-cleanup.zsh

Change-Id: Idf87733f586a8aae0779132f54a8d988e2551bae
Reviewed-on: https://go-review.googlesource.com/c/go/+/709982
Reviewed-by: Michael Matloob <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Ian Alexander 2025-10-01 23:01:55 -04:00
parent 8d0bef7ffe
commit 58a8fdb6cf
5 changed files with 22 additions and 22 deletions

View file

@ -51,7 +51,7 @@ func runBug(ctx context.Context, cmd *base.Command, args []string) {
buf.WriteString(bugHeader)
printGoVersion(&buf)
buf.WriteString("### Does this issue reproduce with the latest release?\n\n\n")
printEnvDetails(&buf)
printEnvDetails(modload.LoaderState, &buf)
buf.WriteString(bugFooter)
body := buf.String()
@ -92,20 +92,20 @@ func printGoVersion(w io.Writer) {
fmt.Fprintf(w, "\n")
}
func printEnvDetails(w io.Writer) {
func printEnvDetails(loaderstate *modload.State, w io.Writer) {
fmt.Fprintf(w, "### What operating system and processor architecture are you using (`go env`)?\n\n")
fmt.Fprintf(w, "<details><summary><code>go env</code> Output</summary><br><pre>\n")
fmt.Fprintf(w, "$ go env\n")
printGoEnv(w)
printGoEnv(loaderstate, w)
printGoDetails(w)
printOSDetails(w)
printCDetails(w)
fmt.Fprintf(w, "</pre></details>\n\n")
}
func printGoEnv(w io.Writer) {
func printGoEnv(loaderstate *modload.State, w io.Writer) {
env := envcmd.MkEnv()
env = append(env, envcmd.ExtraEnvVars()...)
env = append(env, envcmd.ExtraEnvVars(loaderstate)...)
env = append(env, envcmd.ExtraEnvVarsCostly()...)
envcmd.PrintEnv(w, env, false)
}

View file

@ -189,16 +189,16 @@ func findEnv(env []cfg.EnvVar, name string) string {
}
// ExtraEnvVars returns environment variables that should not leak into child processes.
func ExtraEnvVars() []cfg.EnvVar {
func ExtraEnvVars(loaderstate *modload.State) []cfg.EnvVar {
gomod := ""
modload.Init(modload.LoaderState)
if modload.HasModRoot(modload.LoaderState) {
gomod = modload.ModFilePath()
} else if modload.Enabled(modload.LoaderState) {
modload.Init(loaderstate)
if modload.HasModRoot(loaderstate) {
gomod = modload.ModFilePath(loaderstate)
} else if modload.Enabled(loaderstate) {
gomod = os.DevNull
}
modload.InitWorkfile(modload.LoaderState)
gowork := modload.WorkFilePath(modload.LoaderState)
modload.InitWorkfile(loaderstate)
gowork := modload.WorkFilePath(loaderstate)
// As a special case, if a user set off explicitly, report that in GOWORK.
if cfg.Getenv("GOWORK") == "off" {
gowork = "off"
@ -306,7 +306,7 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) {
}
env := cfg.CmdEnv
env = append(env, ExtraEnvVars()...)
env = append(env, ExtraEnvVars(modload.LoaderState)...)
if err := fsys.Init(); err != nil {
base.Fatal(err)

View file

@ -232,7 +232,7 @@ func runEdit(ctx context.Context, cmd *base.Command, args []string) {
if len(args) == 1 {
gomod = args[0]
} else {
gomod = modload.ModFilePath()
gomod = modload.ModFilePath(modload.LoaderState)
}
if *editModule != "" {

View file

@ -768,7 +768,7 @@ func (r *resolver) performLocalQueries(ctx context.Context) {
// restricted to matching packages in the main module.
pkgPattern, mainModule := modload.LoaderState.MainModules.DirImportPath(modload.LoaderState, ctx, q.pattern)
if pkgPattern == "." {
modload.MustHaveModRoot()
modload.MustHaveModRoot(modload.LoaderState)
versions := modload.LoaderState.MainModules.Versions()
modRoots := make([]string, 0, len(versions))
for _, m := range versions {
@ -791,7 +791,7 @@ func (r *resolver) performLocalQueries(ctx context.Context) {
return errSet(fmt.Errorf("no package to get in current directory"))
}
if !q.isWildcard() {
modload.MustHaveModRoot()
modload.MustHaveModRoot(modload.LoaderState)
return errSet(fmt.Errorf("%s%s is not a package in module rooted at %s", q.pattern, absDetail, modload.LoaderState.MainModules.ModRoot(mainModule)))
}
search.WarnUnmatched([]*search.Match{match})

View file

@ -661,18 +661,18 @@ func HasModRoot(loaderstate *State) bool {
// MustHaveModRoot checks that a main module or main modules are present,
// and calls base.Fatalf if there are no main modules.
func MustHaveModRoot() {
Init(LoaderState)
if !HasModRoot(LoaderState) {
die(LoaderState)
func MustHaveModRoot(loaderstate *State) {
Init(loaderstate)
if !HasModRoot(loaderstate) {
die(loaderstate)
}
}
// ModFilePath returns the path that would be used for the go.mod
// file, if in module mode. ModFilePath calls base.Fatalf if there is no main
// module, even if -modfile is set.
func ModFilePath() string {
MustHaveModRoot()
func ModFilePath(loaderstate *State) string {
MustHaveModRoot(loaderstate)
return modFilePath(findModuleRoot(base.Cwd()))
}