]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: use local state object in `tool.runTool`
authorIan Alexander <jitsu@google.com>
Thu, 9 Oct 2025 00:23:24 +0000 (20:23 -0400)
committerIan Alexander <jitsu@google.com>
Sat, 25 Oct 2025 01:09:41 +0000 (18:09 -0700)
This commit modifies `tool.runTool` to construct a new modload.State
object using the new constructor instead of the current global
`modload.LoaderState` variable.

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

[git-generate]
cd src/cmd/go/internal/tool
rf '
  add tool.go:/func runTool\(/-0 var moduleLoaderState *modload.State
  ex {
    import "cmd/go/internal/modload";
    modload.LoaderState -> moduleLoaderState
  }
  add runTool://+0 moduleLoaderState := modload.NewState()
  rm tool.go:/var moduleLoaderState \*modload.State/
'

Change-Id: I0aff1bc2b57d973c258510dc52fb877fa824355c
Reviewed-on: https://go-review.googlesource.com/c/go/+/711128
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/tool/tool.go

index 7ffc50fc87a4e40a078e5423ff73742ce6178d0f..e283c7354f5a34eddc1898dc72913b7c646c6ed6 100644 (file)
@@ -78,9 +78,10 @@ func init() {
 }
 
 func runTool(ctx context.Context, cmd *base.Command, args []string) {
+       moduleLoaderState := modload.NewState()
        if len(args) == 0 {
                counter.Inc("go/subcommand:tool")
-               listTools(modload.LoaderState, ctx)
+               listTools(moduleLoaderState, ctx)
                return
        }
        toolName := args[0]
@@ -108,14 +109,14 @@ func runTool(ctx context.Context, cmd *base.Command, args []string) {
                if tool := loadBuiltinTool(toolName); tool != "" {
                        // Increment a counter for the tool subcommand with the tool name.
                        counter.Inc("go/subcommand:tool-" + toolName)
-                       buildAndRunBuiltinTool(modload.LoaderState, ctx, toolName, tool, args[1:])
+                       buildAndRunBuiltinTool(moduleLoaderState, ctx, toolName, tool, args[1:])
                        return
                }
 
                // Try to build and run mod tool.
-               tool := loadModTool(modload.LoaderState, ctx, toolName)
+               tool := loadModTool(moduleLoaderState, ctx, toolName)
                if tool != "" {
-                       buildAndRunModtool(modload.LoaderState, ctx, toolName, tool, args[1:])
+                       buildAndRunModtool(moduleLoaderState, ctx, toolName, tool, args[1:])
                        return
                }