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]
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
}
}
// 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)
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)
}
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)
}
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()
// 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
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 {
}()
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)