mirror of
https://github.com/golang/go.git
synced 2026-02-06 09:50:02 +00:00
cmd/go: inject State parameter into tool.runTool
This command modifies the call tree starting at `tool.runTool` 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/tool rf 'inject modload.LoaderState runTool' cd .. ./rf-cleanup.zsh Change-Id: Icd1ce189f7dad421eaa2bd43d53ceaf443c5405e Reviewed-on: https://go-review.googlesource.com/c/go/+/710302 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-by: Michael Matloob <matloob@google.com>
This commit is contained in:
parent
441e7194a4
commit
a420aa221e
1 changed files with 21 additions and 21 deletions
|
|
@ -80,7 +80,7 @@ func init() {
|
|||
func runTool(ctx context.Context, cmd *base.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
counter.Inc("go/subcommand:tool")
|
||||
listTools(ctx)
|
||||
listTools(modload.LoaderState, ctx)
|
||||
return
|
||||
}
|
||||
toolName := args[0]
|
||||
|
|
@ -108,14 +108,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(ctx, toolName, tool, args[1:])
|
||||
buildAndRunBuiltinTool(modload.LoaderState, ctx, toolName, tool, args[1:])
|
||||
return
|
||||
}
|
||||
|
||||
// Try to build and run mod tool.
|
||||
tool := loadModTool(ctx, toolName)
|
||||
tool := loadModTool(modload.LoaderState, ctx, toolName)
|
||||
if tool != "" {
|
||||
buildAndRunModtool(ctx, toolName, tool, args[1:])
|
||||
buildAndRunModtool(modload.LoaderState, ctx, toolName, tool, args[1:])
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -132,7 +132,7 @@ func runTool(ctx context.Context, cmd *base.Command, args []string) {
|
|||
}
|
||||
|
||||
// listTools prints a list of the available tools in the tools directory.
|
||||
func listTools(ctx context.Context) {
|
||||
func listTools(loaderstate *modload.State, ctx context.Context) {
|
||||
f, err := os.Open(build.ToolDir)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "go: no tool directory: %s\n", err)
|
||||
|
|
@ -161,9 +161,9 @@ func listTools(ctx context.Context) {
|
|||
fmt.Println(name)
|
||||
}
|
||||
|
||||
modload.InitWorkfile(modload.LoaderState)
|
||||
modload.LoadModFile(modload.LoaderState, ctx)
|
||||
modTools := slices.Sorted(maps.Keys(modload.LoaderState.MainModules.Tools()))
|
||||
modload.InitWorkfile(loaderstate)
|
||||
modload.LoadModFile(loaderstate, ctx)
|
||||
modTools := slices.Sorted(maps.Keys(loaderstate.MainModules.Tools()))
|
||||
for _, tool := range modTools {
|
||||
fmt.Println(tool)
|
||||
}
|
||||
|
|
@ -251,12 +251,12 @@ func loadBuiltinTool(toolName string) string {
|
|||
return cmdTool
|
||||
}
|
||||
|
||||
func loadModTool(ctx context.Context, name string) string {
|
||||
modload.InitWorkfile(modload.LoaderState)
|
||||
modload.LoadModFile(modload.LoaderState, ctx)
|
||||
func loadModTool(loaderstate *modload.State, ctx context.Context, name string) string {
|
||||
modload.InitWorkfile(loaderstate)
|
||||
modload.LoadModFile(loaderstate, ctx)
|
||||
|
||||
matches := []string{}
|
||||
for tool := range modload.LoaderState.MainModules.Tools() {
|
||||
for tool := range loaderstate.MainModules.Tools() {
|
||||
if tool == name || defaultExecName(tool) == name {
|
||||
matches = append(matches, tool)
|
||||
}
|
||||
|
|
@ -300,7 +300,7 @@ func builtTool(runAction *work.Action) string {
|
|||
return linkAction.BuiltTarget()
|
||||
}
|
||||
|
||||
func buildAndRunBuiltinTool(ctx context.Context, toolName, tool string, args []string) {
|
||||
func buildAndRunBuiltinTool(loaderstate *modload.State, ctx context.Context, toolName, tool string, args []string) {
|
||||
// Override GOOS and GOARCH for the build to build the tool using
|
||||
// the same GOOS and GOARCH as this go command.
|
||||
cfg.ForceHost()
|
||||
|
|
@ -308,17 +308,17 @@ func buildAndRunBuiltinTool(ctx context.Context, toolName, tool string, args []s
|
|||
// Ignore go.mod and go.work: we don't need them, and we want to be able
|
||||
// to run the tool even if there's an issue with the module or workspace the
|
||||
// user happens to be in.
|
||||
modload.LoaderState.RootMode = modload.NoRoot
|
||||
loaderstate.RootMode = modload.NoRoot
|
||||
|
||||
runFunc := func(b *work.Builder, ctx context.Context, a *work.Action) error {
|
||||
cmdline := str.StringList(builtTool(a), a.Args)
|
||||
return runBuiltTool(toolName, nil, cmdline)
|
||||
}
|
||||
|
||||
buildAndRunTool(ctx, tool, args, runFunc)
|
||||
buildAndRunTool(loaderstate, ctx, tool, args, runFunc)
|
||||
}
|
||||
|
||||
func buildAndRunModtool(ctx context.Context, toolName, tool string, args []string) {
|
||||
func buildAndRunModtool(loaderstate *modload.State, ctx context.Context, toolName, tool string, args []string) {
|
||||
runFunc := func(b *work.Builder, ctx context.Context, a *work.Action) error {
|
||||
// Use the ExecCmd to run the binary, as go run does. ExecCmd allows users
|
||||
// to provide a runner to run the binary, for example a simulator for binaries
|
||||
|
|
@ -332,11 +332,11 @@ func buildAndRunModtool(ctx context.Context, toolName, tool string, args []strin
|
|||
return runBuiltTool(toolName, env, cmdline)
|
||||
}
|
||||
|
||||
buildAndRunTool(ctx, tool, args, runFunc)
|
||||
buildAndRunTool(loaderstate, ctx, tool, args, runFunc)
|
||||
}
|
||||
|
||||
func buildAndRunTool(ctx context.Context, tool string, args []string, runTool work.ActorFunc) {
|
||||
work.BuildInit(modload.LoaderState)
|
||||
func buildAndRunTool(loaderstate *modload.State, ctx context.Context, tool string, args []string, runTool work.ActorFunc) {
|
||||
work.BuildInit(loaderstate)
|
||||
b := work.NewBuilder("")
|
||||
defer func() {
|
||||
if err := b.Close(); err != nil {
|
||||
|
|
@ -345,11 +345,11 @@ func buildAndRunTool(ctx context.Context, tool string, args []string, runTool wo
|
|||
}()
|
||||
|
||||
pkgOpts := load.PackageOpts{MainOnly: true}
|
||||
p := load.PackagesAndErrors(modload.LoaderState, ctx, pkgOpts, []string{tool})[0]
|
||||
p := load.PackagesAndErrors(loaderstate, ctx, pkgOpts, []string{tool})[0]
|
||||
p.Internal.OmitDebug = true
|
||||
p.Internal.ExeName = p.DefaultExecName()
|
||||
|
||||
a1 := b.LinkAction(modload.LoaderState, work.ModeBuild, work.ModeBuild, p)
|
||||
a1 := b.LinkAction(loaderstate, work.ModeBuild, work.ModeBuild, p)
|
||||
a1.CacheExecutable = true
|
||||
a := &work.Action{Mode: "go tool", Actor: runTool, Args: args, Deps: []*work.Action{a1}}
|
||||
b.Do(ctx, a)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue