diff --git a/src/cmd/go/internal/tool/tool.go b/src/cmd/go/internal/tool/tool.go index f07bdf50873461..36bf9838720586 100644 --- a/src/cmd/go/internal/tool/tool.go +++ b/src/cmd/go/internal/tool/tool.go @@ -287,6 +287,7 @@ func buildAndRunModtool(ctx context.Context, tool string, args []string) { pkgOpts := load.PackageOpts{MainOnly: true} p := load.PackagesAndErrors(ctx, pkgOpts, []string{tool})[0] p.Internal.OmitDebug = true + p.Internal.ExeName = path.Base(p.ImportPath) a1 := b.LinkAction(work.ModeBuild, work.ModeBuild, p) a1.CacheExecutable = true diff --git a/src/cmd/go/testdata/script/tool_exename.txt b/src/cmd/go/testdata/script/tool_exename.txt new file mode 100644 index 00000000000000..dc289b47644cbc --- /dev/null +++ b/src/cmd/go/testdata/script/tool_exename.txt @@ -0,0 +1,32 @@ +[short] skip 'runs go build' + +# First run: executable for bar is not cached. +# Make sure it's not called a.out +go tool bar +stdout 'my name is: bar'$GOEXE +! stdout 'a.out' + +# Second run: executable is cached. Make sure it +# has the right name. +go tool bar +stdout 'my name is: bar'$GOEXE +! stdout 'a.out' + +-- go.mod -- +module example.com/foo + +go 1.24 + +tool example.com/foo/bar +-- bar/bar.go -- +package main + +import ( + "fmt" + "os" + "path/filepath" +) + +func main() { + fmt.Println("my name is:", filepath.Base(os.Args[0])) +} \ No newline at end of file