]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: inject State parameter into `bug.runBug`
authorIan Alexander <jitsu@google.com>
Thu, 2 Oct 2025 03:01:55 +0000 (23:01 -0400)
committerIan Alexander <jitsu@google.com>
Mon, 20 Oct 2025 19:50:01 +0000 (12:50 -0700)
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>
src/cmd/go/internal/bug/bug.go
src/cmd/go/internal/envcmd/env.go
src/cmd/go/internal/modcmd/edit.go
src/cmd/go/internal/modget/get.go
src/cmd/go/internal/modload/init.go

index 4e9ae1e9b499cac8e3f6168876c09da78c6e1856..ccd8dee95e69b3b851ac29b08a29a576bace4296 100644 (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)
 }
index 13708ae170c1d84d4085134718d9275bb23af99f..517722a4265eddd3e283cd2807fc8f9758562ea2 100644 (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)
index 041b4432bfd4f0f48008cbe8c20d719d4821d9b5..d5774e98d76cce057fe7dee84422baa653344607 100644 (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 != "" {
index 141e1708fa6dedbd3ecb9dc0dfaf7c1450d15bdd..b26406ee1d0fa20377febf880eeb86479846568e 100644 (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})
index 20751528862ce268bf2e9df05cf1190cc81d013e..383ea20e3e673a2bffaa1f3bfac47c75ac64503d 100644 (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()))
 }