From a86ea80197ed3bf0f276638a9ce079cbd2071d83 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Thu, 21 Nov 2024 13:16:43 -0500 Subject: [PATCH] cmd/go/internal/tool: set Internal.ExeName on tool's package While the cached name of an executable is set based on the base name of the package path, the executable produced as the output of link doesn't have ExeName set on it and is just called a.out (with a .exe suffix on Windows). Set ExeName so that the first time the binary is run, from the directory link is run in, it has the right name for ps. For #48429 Change-Id: Ic049304ec6fd5b23c2f5aaaf91aa58d79fe5a7ba Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest,gotip-windows-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/go/+/630695 Reviewed-by: Conrad Irwin Reviewed-by: Hongxiang Jiang LUCI-TryBot-Result: Go LUCI --- src/cmd/go/internal/tool/tool.go | 1 + src/cmd/go/testdata/script/tool_exename.txt | 32 +++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/cmd/go/testdata/script/tool_exename.txt diff --git a/src/cmd/go/internal/tool/tool.go b/src/cmd/go/internal/tool/tool.go index f07bdf5087..36bf983872 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 0000000000..dc289b4764 --- /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 -- 2.48.1